How to avoid nested transactions not supported error?

前端 未结 5 1786
借酒劲吻你
借酒劲吻你 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:25

    I faced a similar issue : org.hibernate.TransactionException: nested transactions not supported

    In my case, I opened a session and tried saving . Without commit, I called another method and called session.beginTransaction(); So it throw the error. In order to avoid, I sent the session object as a method parameter and just called session.save instead of again doing begin. Practically used the session object. It worked for me!

    0 讨论(0)
  • 2020-12-08 05:31

    I was facing same problem , i solved my problem by putting tr.commit(); function after every transaction. It happens only when you start any transaction without closing previous transaction.

    session = HibernateUtil.getSession();
    
    session.getTransaction().begin();       OR session.beginTransaction();
    
    ...   to do ....
    
    session.getTransaction().commit();
    
    session.close();   OR HibernateUtil.closeSession();
    

    Looks like you are also doing same mistake.

    0 讨论(0)
  • 2020-12-08 05:35

    In your "My code" snippet, there might be some problems:

    1. In case of an exception, there is no finally block to close the session
    2. You are calling session.close(), but this is different from HibernateUtils.closeSession(). So the ThreadLocal is not cleared.
    3. There is no catch block for exceptions; as a consequence there is no rollback.
    4. Do you rethrow exceptions or are they (silently) ignored?

    If there is an exception in the "to do" block after begin(), the transaction remains open, and ThreadLocal is not cleared.

    Your code may work fine normally, but under high load there might be (SQL lock) timeouts etc., and in this case, once in a while, an exception will be thrown.

    So check each snippet for correct exception handling:

    final Session session = HibernateUtil.getSession();
    try {
      final Transaction transaction = session.beginTransaction();
      try {
        // The real work is here
        transaction.commit();
      } catch (Exception ex) {
        // Log the exception here
        transaction.rollback();
        throw ex;
      }
    } finally {
      HibernatilUtil.closeSession();
    }
    

    You could add some "book-keeping" code to HibernateUtil.getSession() and HibernateUtil.closeSession(): Log each access, including the thread's name. Eventually one or multiple "gets" by the same thread must be followed by a "close".

    In your case I would even consider to have only one "get", and pass the session around as long as your thread is doing its unit of work: This way it's possibly easier to find the problem.


    There is another question on SO which reports a similar problem: Hibernate 4.1.9 (latest final build) reporting `nested transactions not supported.

    You could add some code after commit() to check, if the transaction has been really completed (by calling wasCommitted()).

    A quote from the Javadoc of wasCommitted():

    This method could return false even after successful invocation of commit(). As an example, JTA based strategies no-op on commit() calls if they did not start the transaction; in that case, they also report wasCommitted() as false.

    0 讨论(0)
  • 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

     <prop key="hibernate.current_session_context_class">thread</prop>
    
    0 讨论(0)
  • 2020-12-08 05:47

    Use session.beginTransaction() instead of session.getTransaction().begin() in your code. You need to begin a new unit of work, so beginTransaction will begin a new transaction. So your code will look like:

    session = HibernateUtil.getSession();
    Transaction transaction = session.beginTransaction();
           ...   to do ....
    transaction.commit();
    

    Click Here to get more information about beginTransaction(); method.

    I think that will resolve your issue. Please let me know if issue persists.

    0 讨论(0)
提交回复
热议问题