Archive for the ‘Hibernate’ Category

JBoss JNDI datasource not releasing connections with Spring/Hibernate

Saturday, October 24th, 2009

I recently had a problem with my application not releasing the database connections when I switched to using JNDI. This was a JEE application that used Spring and Hibernate. Here are the versions that I was using:

  • JBoss 4.0.4
  • JDK 1.5
  • Spring 2.5.5
  • Hibernate 3.2.6

Solution

In my Spring applicationContext.xml file, I added the datasource JNDI entry for “Kramerica”. I referenced it in the Hibernate session factory bean:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="java:comp/env/jdbc/Kramerica"/>
</bean>
 
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
  <property name="dataSource" ref="dataSource"/>
  <property name="hibernateProperties">
    <props>
	<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
	<prop key="hibernate.show_sql">false</prop>
	<prop key="hibernate.connection.release_mode">after_transaction</prop>
    </props>
  </property>
</bean>

The key entry is the property “hibernate.connection.release_mode”.
This property specifies when Hibernate should release JDBC connections.

By default, a JDBC connection is held until the session is explicitly closed or disconnected. For an application server JTA datasource, use “after_statement” to aggressively release connections after every JDBC call. For a non-JTA connection, it often makes sense to release the connection at the end of each transaction, by using “after_transaction”.

You can also set it to “auto”, which will choose “after_statement” for the JTA and CMT transaction strategies and “after_transaction” for the JDBC transaction strategy.

You can find more detail on the Hibernate connection settings at the Hibernate site. Happy coding!

Using Hibernate Validator for your Java classes

Friday, June 12th, 2009

This article discusses how to use the Hibernate Validator for your Java classes. (more…)

ClassNotFound errors - Hibernate 3.1 and Spring 1.2

Sunday, June 7th, 2009

I recently had a project that incorporated Hibernate and Spring. MyEclipse 5.0 was the IDE, and the app server was JBoss 4.0.4. We encountered a few problems trying to get the correct combination of Hibernate, Spring, and JBoss that would play well together. (more…)