hibernate.connection.release_mode
In this section, you will learn about hibernate configuration parameter hibernate.connection.release_mode.
Prior to Hibernate 3.x , JDBC connection management means that session gets the connection when first time it requires it and this session should maintain this connection until the session was closed.
After the introduction of Hibernate 3.x, the new feature introduced is connection release modes which tells a session how to deal with its JDBC connections.
The different release modes which is denoted by enumerated values of org.hibernate.ConnectionReleaseMode are given below :
- ON_CLOSE
This release mode has legacy behavior as : The session gets the connection when first time it requires it and this session should maintain this connection until the session was closed.
- AFTER_TRANSACTION
It releases the connection after org.hibernate.Transaction commit or rollback.
- AFTER_STATEMENT
This is also known as aggressive release. After execution of every statement, it releases connection. But it skipped the connection releasing. if the resources related to session are still open. One of the example of this situation is when you use org.hibernate.ScrollableResults.
Which release mode should be used can be set through configuration parameter hibernate.connection.release_mode. The possible value of this parameter and related connection mode is given below :
- auto : It is the default value. It releases the value
according to the value returned by the method
org.hibernate.transaction.TransactionFactory.getDefaultReleaseMode().
If you are using JTATransactionFactory, it returns ConnectionReleaseMode.AFTER_STATEMENT.
- on_close : It uses ConnectionReleaseMode.ON_CLOSE. But
its use is not recommended since it has the legacy behavior.
- after_transaction : It uses
ConnectionReleaseMode.AFTER_TRANSACTION. It is strongly recommended
that this setting should not be use in JTA environments.
- after_statement : It uses the ConnectionReleaseMode.AFTER_STATEMENT. It is recommended to check the whether it supports this setting( supportsAggressiveRelease() ). If not, the release mode is reset to ConnectionReleaseMode.AFTER_TRANSACTION.
EXAMPLE
There are two ways of setting configuration parameter :
XML deployment descriptor : hibernate.cfg.xml
A sample configuration file hibernate.cfg.xml is given below :
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://192.168.10.13:3306/anky</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.pool_size">1</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="current_session_context_class">thread</property> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">validate</property> <property name="hibernate.connection.release_mode">auto</property> <mapping class="net.roseindia.WorkerDetail"/> <mapping class="net.roseindia.Worker"/> </session-factory> </hibernate-configuration>