简体   繁体   English

HIbernate:正确执行所有事务后,“不支持嵌套事务”

[英]HIbernate: “Nested Transactions not supported” when all transactions properly committed

I have read a few posts on this topic: eg, here and here 我已经阅读了有关该主题的几篇文章:例如, 这里这里

In my case, I am certain that all transactions are committed and that all exceptions are handled with a rollback. 就我而言,我确信所有事务都已提交,并且所有异常都通过回滚处理。

Currently, I am building the model layer for a web app (Spring/Hibernate/Postgres). 目前,我正在为Web应用程序(Spring / Hibernate / Postgres)构建模型层。 I am using junit to rigorously test as I build. 在构建时,我正在使用junit进行严格测试。 For some reason, on one delete test I repeatedly got the "nested transaction not supported" exception. 由于某种原因,在一个删除测试中,我反复遇到“不支持嵌套事务”异常。

Session session=HibernateDAO.getSessionFactory().getCurrentSession();

    try
    {   

        session.getTransaction().begin();

        List<TestUnitType> result = session.createQuery("from TestUnitType").list();



        TestUnitType itemToDelete = result.get(0);


        session.delete(itemToDelete);
        session.getTransaction().commit();

    } catch (Exception e)
    {
        TestSS.getLogger().error(e.toString(), e);

        session.getTransaction().rollback();
        fail(e.toString());
    }

The exception is thrown when calling the transaction prior to the query. 在查询之前调用事务时会引发异常。 The previous method definitely closed the transaction as it is couched in the same syntax as this method (and I carefully debugged). 前面的方法肯定关闭了事务,因为它使用的语法与此方法相同(我对此进行了仔细调试)。

I resolved the issue by creating a kludge method and using it to get the session: 我通过创建kludge方法并使用它来获取会话来解决了该问题:

public static Session resetCurrentSession()
{
    Session oldsession = getSessionFactory().getCurrentSession();
    if(oldsession.isOpen())
    {
        oldsession.close();
    }
    return getSessionFactory().getCurrentSession();
}

All unit tests now run and pass, the data in the database is clean and I see no side effects of my kludge. 现在,所有单元测试都可以运行并通过,数据库中的数据是干净的,我看不到kludge的副作用。 Nonetheless, I am uncomfortable using a workaround in unit tests. 尽管如此,我在单元测试中使用变通方法感到不舒服。 No fix is safe if the programmer does not know the cause of the problem behind it. 如果程序员不知道问题背后的原因,那么没有修复是安全的。

can someone advise on the possible cause of the exception? 有人可以就异常的可能原因提供建议吗?

The answer was junit related, not hibernate related. 答案与junit有关,与休眠无关。

It seems that assert statements in junit will exit a method, so if the assert statement is prior to the commit, then the commit does not happen. 似乎junit中的assert语句将退出方法,因此,如果assert语句在提交之前,则不会发生提交。

This is no doubt a beginner error in junit, but may be of use to other beginners. 毫无疑问,这是junit中的初学者错误,但可能对其他初学者有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不支持休眠、嵌套事务 - Hibernate, nested transactions not supported 休眠时不支持嵌套事务 - nested transactions not supported When insert a record by hibernate Hibernate TransactionException:不支持嵌套事务 - Hibernate TransactionException: nested transactions not supported Hibernate errro:不支持嵌套事务 - Hibernate errro : nested transactions not supported 线程和事务:不支持嵌套事务 - Threads and transactions : nested transactions not supported Hibernate org.hibernate.TransactionException:jaxrs不支持嵌套事务 - Hibernate org.hibernate.TransactionException: nested transactions not supported on jaxrs 删除时出现“org.hibernate.TransactionException:不支持嵌套事务”错误 - Getting “org.hibernate.TransactionException: nested transactions not supported” error when deleting 线程“ main” org.hibernate.TransactionException中的异常:不支持嵌套事务 - Exception in thread “main” org.hibernate.TransactionException: nested transactions not supported Hibernate 4.1.9(最新的最终版本)报告`不支持嵌套事务` - Hibernate 4.1.9 (latest final build) reporting `nested transactions not supported` “org.hibernate.TransactionException:不支持嵌套事务”但我没有嵌套事务 - “org.hibernate.TransactionException: nested transactions not supported” but I didn't nest transactions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM