posts - 64,  comments - 9,  trackbacks - 0

org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
最近在写操作CLOB的过程中总是报这个异常,数据去插入进去了.


先看看SessionFactory.getCurrentSession与openSession的区别
1. 如果使用的是getCurrentSession来创建session的话,在commit后,session就自动被关闭了,
    也就是不用再session.close()了。但是如果使用的是openSession方法创建的session的话,
    那么必须显示的关闭session,也就是调用session.close()方法。这样commit后,session并没有关闭
/*2. getCurrentSession的使用可以参见hibernate\hibernate-3.2\doc\tutorial\src项目
3. 使用SessionFactory.getCurrentSession()需要在hibernate.cfg.xml中如下配置:
   * 如果采用jdbc独立引用程序配置如下:
    <property name="hibernate.current_session_context_class">thread</property>
   * 如果采用了JTA事务配置如下
    <property name="hibernate.current_session_context_class">jta</property>*/

开始用的:

Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
Transaction tran=session.beginTransaction();

好多文章里都说只需在Spring配置<bean id="sessionFactory"...---><property name="hibernateProperties">   中加入 <prop key="hibernate.current_session_context_class">thread</prop>就可以解决,试了没用.

修改后的:

Session session = this.getHibernateTemplate().getSessionFactory().openSession();

Transaction tran=session.beginTransaction();

......

finally
   {  
    session.close();  
   }

问题解决

posted on 2009-09-11 17:06 super_nini 阅读(9724) 评论(6)  编辑  收藏

FeedBack:
# re: 转:Could not commit Hibernate 问题,spring 事物 提交前session关闭问题
2009-10-28 14:22 | 楚门
真是太感谢了……
弄了半天,感觉不尽……  回复  更多评论
  
# re: 转:Could not commit Hibernate 问题,spring 事物 提交前session关闭问题
2010-03-02 17:52 | 王成飞
好文章,就是没有理由的顶
感激不尽....  回复  更多评论
  
# re: 转:Could not commit Hibernate 问题,spring 事物 提交前session关闭问题
2010-03-15 14:46 | jspxcgy007
好贴必须顶!  回复  更多评论
  
# re: 转:Could not commit Hibernate 问题,spring 事物 提交前session关闭问题
2010-11-09 11:32 | chenj
顶,楼主真是强啊  回复  更多评论
  
# re: 转:Could not commit Hibernate 问题,spring 事物 提交前session关闭问题[未登录]
2011-12-02 01:18 | liu
是对的,解决了 强  回复  更多评论
  
# re: 转:Could not commit Hibernate 问题,spring 事物 提交前session关闭问题
2013-01-24 15:41 | 枷锁
赞……………………  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
<2010年11月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿

随笔档案

文章档案

相册

搜索

  •  

最新评论

阅读排行榜

评论排行榜