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?
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:
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:
- 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.
- 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.
- 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.