Monday, May 21, 2012

Hibernate Connection Release Modes

Hibernate Connection Release Modes:

The different release modes that are supported by Hibernate are as follows:

1. on_close: The Hibernate session obtains a connection when it first needs to perform some JDBC access and maintains that connection until the session is closed.

2. after_transaction: uses ConnectionReleaseMode.AFTER_TRANSACTION. This setting should not be used in JTA environments. Also note that with ConnectionReleaseMode.AFTER_TRANSACTION, if a session is considered to be in auto-commit mode, connections will be released as if the release mode were AFTER_STATEMENT.

3. after_statement: uses ConnectionReleaseMode.AFTER_STATEMENT. Additionally, the configured ConnectionProvider is consulted to see if it supports this setting (supportsAggressiveRelease()). If not, the release mode is reset to ConnectionReleaseMode.AFTER_TRANSACTION. This setting is only safe in environments where we can either re-acquire the same underlying JDBC connection each time you make a call into ConnectionProvider.getConnection() or in auto-commit environments where it does not matter if we re-establish the same connection

Thursday, May 10, 2012

Hibernate Logging

We can use two ways of hibernate configuration for debugging the sql queries generated by hibernate framework.

1. Using log4j.properties

# Hibernate logging options (INFO only shows startup messages)
Log4j.logger.org.hiernate=INFO

Log4j.rootLogger=INFO, console

#The below logger statement is equivalent to hibernate.show_sql=true
log4j.logger.org.hibernate.SQL = DEBUG

# Log JDBC bind parameter runtime arguments.
# The below logger prints the values binded to sql query and the response from the query.
log4j.logger.org.hibernate.type = TRACE

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=IPDMS[%d{yyyy-MM-dd HH:mm:ss}] %5p (%F:%L) %m%n

2. Hibernate configuration

<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>