How to avoid nested transactions not supported error?

前端 未结 5 1794
借酒劲吻你
借酒劲吻你 2020-12-08 05:15

I need to make sure many concurrent users be able to access the database. Although after each commit I close the session but sometimes my code runs into following error, but

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 05:45

    You probably have begun a transaction, and trying to begin another one without having committed or rollbacked the previous one. The idiom when using programmatic transaction demarcation is the following one:

    Transaction transaction = null;
        try {
          session = HibernateUtil.getSession();
          transaction  = session.beginTransaction();
           ...   to do ....
          transaction.commit();
        }
        catch (RuntimeException e) {
            transaction.rollback();
            throw e;
        }
    

    Add the following property in your Hibernate.cfg.xml

     thread
    

提交回复
热议问题