What is the difference between a Session and a Connection in Hibernate?

亡梦爱人 提交于 2019-12-03 07:30:02

问题


I want to clear the basic 3 points,

Does beginning a new database transaction on an old session obtains a new connection and resumes the session?

Does committing a database transaction disconnects a session from the JDBC connection and returns the connection to the pool?

From Hibernate Documentation, earlier versions of Hibernate required explicit disconnection and reconnection of a Session. These methods are deprecated, asbeginning and ending a transaction has the same effect. How do they have the same effect?


回答1:


A Hibernate Session is just a transactional write-behind cache that translate entity state transitions into DML statements. The Hibernate Session can be connected or disconnected from the database. When it is disconnected, it cannot flush current pending entity state changes to the underlying database.

There are multiple ways to associate a Hibernate Session to a database transaction:

  • session-per-request (the session is bound to the life-cycle of a single logical @Transaction and one physical database transaction)
  • long conversation (the session can span to multiple @Transaction operations, hence there are multiple database transactions involved)

When it comes to database transactions, there are two different approaches:

  • RESOURCE_LOCAL transactions, using a single DataSource will always bind a physical database transaction to a Hibernate Session (within the context of a single logical transaction, meaning that you can still implement long conversations to span over multiple such logical transactions).

  • JTA, using multiple DataSources. JTA state that connections should be aggressively released after each statement, but it practice you still get the same JDBC Connection handle within the context of a single logical transaction.

Now back to your questions:

  1. Does beginning a new database transaction on an old session obtains a new connection and resumes the session?

Yes. The Hibernate session is reconnected and flushing/committing can proceed.

  1. Does committing a database transaction disconnects a session from the JDBC connection and returns the connection to the pool?

By default, when you commit a transaction, the Session is closed and the underlying connection is closed. If you use connection pooling, the database connection will indeed return to the pool.

  1. From Hibernate Documentation, earlier versions of Hibernate required explicit disconnection and reconnection of a Session. These methods are deprecated, as beginning and ending a transaction has the same effect. How do they have the same effect?

These methods are deprecated because the connection management is now controlled by the connection release modes.



来源:https://stackoverflow.com/questions/28486850/what-is-the-difference-between-a-session-and-a-connection-in-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!