简体   繁体   中英

Hibernate TransactionException: nested transactions not supported

I am getting following exception:

2015-06-23 12:34:30.015 ERROR findRequest:224  - get failed
org.hibernate.TransactionException: nested transactions not supported
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:154)
at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1435)
at sun.reflect.GeneratedMethodAccessor127.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:356)
at com.sun.proxy.$Proxy5.beginTransaction(Unknown Source)

The method which is showing this exception is:

public List findRequest() {

    try {
        Transaction trans=sessionFactory.getCurrentSession().beginTransaction();
        Query q = sessionFactory.getCurrentSession().createQuery("query");
        q.setString("reqStatus", reqStatus);
        List result = q.list();
        trans.commit();           
        return result;
    } catch (RuntimeException re) {
        LOGGER.error("get failed", re);            
    }
    return null;
}

I am getting this issue while load testing. Also I recently added some functionality where number of web service calls increases. Also I created one maintenance thread which continue to call DB. So the point is number of DB calls increases 20 times.

I am using PostgreSQL. Also this is my hibernate setting:

<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.password">******</property>
    <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/******</property>
    <property name="hibernate.connection.username">******</property>
    <property name="hibernate.default_schema">******</property>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="hibernate.search.autoregister_listeners">false</property>
    <property name="hibernate.show_sql">false</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.enable_lazy_load_no_trans">true</property>

As you can see from my Hibernate setting I am using default Hibernate connection pool. Is it possible my connection are full and because of that my transition not released or closed. Also I am not using rollback mechanism. Also hibernate.current_session_context_class=thread, so every rest call is creating new thread. And the method which is showing this exception is the first one called from rest API. So for one thread how can it throw nested exception. Thanks in advance.

This issue may arise if your previous transaction has not been rolled back. For now, you should restart your server (Glassfish or whatever else you use) and try again.

To avoid this in the future, you may want to manually roll back it in the catch block:

catch (RuntimeException re) {
        trans.rollback();
        LOGGER.error("get failed", re);            
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM