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
          最近在寫(xiě)操作CLOB的過(guò)程中總是報(bào)這個(gè)異常,數(shù)據(jù)去插入進(jìn)去了.


          先看看SessionFactory.getCurrentSession與openSession的區(qū)別
          1. 如果使用的是getCurrentSession來(lái)創(chuàng)建session的話(huà),在commit后,session就自動(dòng)被關(guān)閉了,
              也就是不用再session.close()了。但是如果使用的是openSession方法創(chuàng)建的session的話(huà),
              那么必須顯示的關(guān)閉session,也就是調(diào)用session.close()方法。這樣commit后,session并沒(méi)有關(guān)閉
          /*2. getCurrentSession的使用可以參見(jiàn)hibernate\hibernate-3.2\doc\tutorial\src項(xiàng)目
          3. 使用SessionFactory.getCurrentSession()需要在hibernate.cfg.xml中如下配置:
             * 如果采用jdbc獨(dú)立引用程序配置如下:
              <property name="hibernate.current_session_context_class">thread</property>
             * 如果采用了JTA事務(wù)配置如下
              <property name="hibernate.current_session_context_class">jta</property>*/

          開(kāi)始用的:

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

          好多文章里都說(shuō)只需在Spring配置<bean id="sessionFactory"...---><property name="hibernateProperties">   中加入 <prop key="hibernate.current_session_context_class">thread</prop>就可以解決,試了沒(méi)用.

          修改后的:

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

          Transaction tran=session.beginTransaction();

          ......

          finally
             {  
              session.close();  
             }

          問(wèn)題解決

          posted on 2009-09-11 17:06 super_nini 閱讀(9747) 評(píng)論(6)  編輯  收藏

          FeedBack:
          # re: 轉(zhuǎn):Could not commit Hibernate 問(wèn)題,spring 事物 提交前session關(guān)閉問(wèn)題
          2009-10-28 14:22 | 楚門(mén)
          真是太感謝了……
          弄了半天,感覺(jué)不盡……  回復(fù)  更多評(píng)論
            
          # re: 轉(zhuǎn):Could not commit Hibernate 問(wèn)題,spring 事物 提交前session關(guān)閉問(wèn)題
          2010-03-02 17:52 | 王成飛
          好文章,就是沒(méi)有理由的頂
          感激不盡....  回復(fù)  更多評(píng)論
            
          # re: 轉(zhuǎn):Could not commit Hibernate 問(wèn)題,spring 事物 提交前session關(guān)閉問(wèn)題
          2010-03-15 14:46 | jspxcgy007
          好貼必須頂!  回復(fù)  更多評(píng)論
            
          # re: 轉(zhuǎn):Could not commit Hibernate 問(wèn)題,spring 事物 提交前session關(guān)閉問(wèn)題
          2010-11-09 11:32 | chenj
          頂,樓主真是強(qiáng)啊  回復(fù)  更多評(píng)論
            
          # re: 轉(zhuǎn):Could not commit Hibernate 問(wèn)題,spring 事物 提交前session關(guān)閉問(wèn)題[未登錄](méi)
          2011-12-02 01:18 | liu
          是對(duì)的,解決了 強(qiáng)  回復(fù)  更多評(píng)論
            
          # re: 轉(zhuǎn):Could not commit Hibernate 問(wèn)題,spring 事物 提交前session關(guān)閉問(wèn)題
          2013-01-24 15:41 | 枷鎖
          贊……………………  回復(fù)  更多評(píng)論
            

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2010年3月>
          28123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          相冊(cè)

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 高碑店市| 临猗县| 蚌埠市| 邯郸市| 顺昌县| 高邑县| 肇庆市| 盘锦市| 北碚区| 东阿县| 吴川市| 大悟县| 毕节市| 宜宾县| 珠海市| 阳江市| 金华市| 绍兴县| 肃南| 蓬安县| 罗田县| 蛟河市| 无极县| 宾川县| 莆田市| 托克逊县| 常宁市| 大同市| 江北区| 清新县| 巴马| 长宁区| 达州市| 堆龙德庆县| 石屏县| 富蕴县| 原平市| 巴林左旗| 九台市| 吉林省| 左权县|