【问题标题】:Hibernate TransactionException: nested transactions not supportedHibernate TransactionException:不支持嵌套事务
【发布时间】: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


【解决方案1】:

如果您之前的事务尚未回滚,则可能会出现此问题。现在,您应该重新启动服务器(Glassfish 或其他您使用的服务器)并重试。

为避免以后发生这种情况,您可能需要在 catch 块中手动回滚它:

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

【讨论】:

  • 很有可能。具体来说,我想说负载测试在其他地方触发了一个错误,该错误被一个懒惰编写的异常处理程序(想想catch (SQLException ex) {})吞下,然后导致应用程序在事务已经打开时访问此代码 - 尽管可能在中止挂起的回滚状态。
  • 首先我没有使用服务器。这是一个独立的应用程序。还有一些数据库方法,我使用 Hibernate.initialize(o.getLinkedBean) 来获取关联的 bean 对象。这是来自同一个转换。这是罪魁祸首吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-30
相关资源
最近更新 更多