【发布时间】:2015-06-26 09:57:41
【问题描述】:
我收到以下异常:
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)
显示此异常的方法是:
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;
}
我在负载测试时遇到了这个问题。此外,我最近还添加了一些增加 Web 服务调用次数的功能。我还创建了一个继续调用 DB 的维护线程。所以关键是数据库调用的数量增加了 20 倍。
我正在使用 PostgreSQL。这也是我的休眠设置:
<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>
从我的 Hibernate 设置中可以看出,我使用的是默认的 Hibernate 连接池。是否有可能我的连接已满,因此我的过渡没有释放或关闭。 另外我没有使用回滚机制。 还有 hibernate.current_session_context_class=thread,所以每个 rest 调用都在创建新线程。显示此异常的方法是第一个从 rest API 调用的方法。因此,对于一个线程,它如何抛出嵌套异常。 提前致谢。
【问题讨论】:
-
可能和之前的操作有关。您是否妥善管理了之前的交易?
标签: java hibernate web-services postgresql transactions