Does Hibernate's Session.close() automatically rollback uncommitted transactions?

后端 未结 1 1937
渐次进展
渐次进展 2021-02-07 11:12

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

If the Session throws an exception, including any SQLException,

1条回答
  •  醉酒成梦
    2021-02-07 11:54

    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.

    0 讨论(0)
提交回复
热议问题