Hibernate的Session.close()会自动回滚未提交的事务吗? [英] Does Hibernate's Session.close() automatically rollback uncommitted transactions?

查看:215
本文介绍了Hibernate的Session.close()会自动回滚未提交的事务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Hibernate文档,并且遇到以下引用:
$ b


如果Session引发异常,包括任何SQLException,立即回滚数据库事务,调用Session.close()并丢弃Session实例。 Session的某些方法不会使会话保持一致的状态。 Hibernate引发的异常可以视为可恢复。通过在finally块中调用close()来确保Session被关闭。

就我而言,这一切都是有道理的,但它确实让我想知道关闭会话的效果是什么既没有承诺也没有回滚的交易?



例如,考虑以下几点:

  session = getSessionFactory()。的openSession(); 
session.beginTransaction();
session.save(carObject);
//session.getTransaction()。commit();
session.close();

当commit被注释掉了,并且这里没有回滚时,session.close的期望行为是什么)?它是否会自动回滚该提交,它是否会留下挂起事务?等等。



(我明白这显然不是好的做法 - 我只是想让我的头脑更深入了解底层概念。)

解决方案

我已经完成了一些挖掘Hibernate的工作: 保持它们的生命周期在某种程度上独立于JDBC连接。当关闭Hibernate的 Session 时,连接被释放 释放连接的确切含义取决于获得了连接

<如果连接是手动提供的(例如,通过 sessionFactory.openStatelessSession(连接)),您将在调用时返回可能未完成的事务的连接 session .close() 在其他情况下调用 session.close()通常会最终在调用 connection.close()


没有自动会话刷新或事务提交/回滚由Hibernate完成。 JPA的 EntityManager



所以最终会发生什么取决于您的连接提供者/数据源。使用 C3PO任何未完成的事务将在连接返回到池时回滚。另一方面,如果您已经管理了JTA连接,那么实际的事务处理可能完全超出了您的应用程序的范围。


I'm reading the Hibernate documentation at present and I came across the following quote:

If the Session throws an exception, including any SQLException, immediately rollback the database transaction, call Session.close() and discard the Session instance. Certain methods of Session will not leave the session in a consistent state. No exception thrown by Hibernate can be treated as recoverable. Ensure that the Session will be closed by calling close() in a finally block.

This all makes sense as far as I'm concerned, but it does make me wonder what the effect of closing a session is with a transaction neither committed nor rolled back?

E.g consider the following:

  session = getSessionFactory().openSession();
  session.beginTransaction();
  session.save(carObject);
  //session.getTransaction().commit();
  session.close();

With commit commented out, and no rollback called here, what is the expected behaviour of session.close()? Does it simply rollback that commit automatically, does it leave a 'hanging' transaction? etc.

(I understand this obviously wouldn't be good practice - I'm just trying to get my head around the underlying concepts a bit more.)

解决方案

I've done a bit of digging into Hibernate:

Persistence sessions keep their life-cycle somewhat independent from JDBC connections. When you close Hibernate's Session the connection is released. Exact meaning of "releasing connection" depends on how the connection was obtained in the first place:

  • if the connection was provided manually (e.g. via sessionFactory.openStatelessSession(connection)) you will get your connection with possibly unfinished transaction back when calling session.close()
  • in other cases calling session.close() will usually end up in calling connection.close()

No automatic session flushing or transaction commit / rollback is made by Hibernate. The same states for the JPA's EntityManager.

So what happens in the end depends on your connection provider / data source. With C3PO any unfinished transaction will be rolled-back when the connection is returned to the pool. On the other hand if you have managed JTA connection then the actual transaction handling might be completely out of scope to your application.

这篇关于Hibernate的Session.close()会自动回滚未提交的事务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆