異常:Transaction not successfully started

異常:org.hibernate.TransactionException: Transaction not successfully started

錯誤代碼:
service層
        Session session = HibernateUtils.getCurrentSession();
        //....數據庫操作語句(調用dao層)
        Transaction tx = session.beginTransaction();
        tx.commit();
dao層
    Session session = HibernateUtils.getCurrentSession();
        //....數據庫操作語句 
        Transaction tx = session.beginTransaction();
        tx.commit();

異常詳情:

![](http://i2.51cto.com/images/blog/201811/01/b3a475128fe693546c7d8d34cefac421.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

一翻檢查分析後發現原因:

原來是我在同一個線程中創建了兩個事務對象,
這違背了事務ACID特性中的 Isolation隔離性。

解決方案:

只要把dao層重複的事務代碼去掉即可,如下:
    Transaction tx = session.beginTransaction();
    tx.commit();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章