Results 1 to 4 of 4

Thread: Classes extending HibernateDaoSupport not closing connection

  1. #1
    Join Date
    Sep 2004
    Posts
    602

    Default Classes extending HibernateDaoSupport not closing connection

    We have some pretty basic hibernate classes that extend HibernateDaoSupport. These are running within session beans and get their connections from a Websphere datasource.

    Here's an example of spring/hibernate code within a method call

    List list = getHibernateTemplate().find("from SecuredProperty a where a.customerID = ? and a.securityID = ?",new Object[] { customerId, securityId, }, new Type[] { Hibernate.BIG_DECIMAL,
    Hibernate.BIG_DECIMAL, });

    Basically the problem we are having is that when something calls this code, none of the JDBC connections are getting closed, so the connection pool rapidly runs out of connections, and then the applciation grinds to a halt.

    What controls how Spring closes a connection ? What do we have to force a connection close (i.e. return the connection to the pool) after a hibernate call ?

    Shown below is our applicationcontext file:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
        "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    
    	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    		<property name="jndiName">
    			<value>java&#58;comp/env/jdbc/BROKER</value>
    		</property>
    
    	</bean> 
    
    	<!-- Hibernate SessionFactory -->
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    
    		<property name="mappingResources">
    			<list>
    				<value>net/targetgroup/broker/application/SecuredProperty.hbm.xml</value>
    				<value>net/targetgroup/broker/address/Address.hbm.xml</value>
    				<value>net/targetgroup/broker/lender/Lender.hbm.xml</value>
    				<value>net/targetgroup/broker/lender/adapters/LenderAdapter.hbm.xml</value>
    				<value>net/targetgroup/broker/notes/Notes.hbm.xml</value>	
    			
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="use_outer_join">true</prop>
    				<prop key="hibernate.dialect">net.sf.hibernate.dialect.DB2400Dialect</prop>
    				<prop key="show_sql">true</prop>
    				<prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
    			</props>
    		</property>
    
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    
    	</bean>
    
    	<!-- Transaction manager for a single Hibernate SessionFactory &#40;alternative to JTA&#41; -->
    	<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref local="sessionFactory" />
    		</property>
    	</bean>
    
    
    
    	<bean id="addressDAO" class="net.targetgroup.broker.address.AddressDAOHibernate">
    		<property name="sessionFactory">
    			<ref local="sessionFactory" />
    		</property>
    	</bean>
    
    
    
    	<bean id="customerAddressDAO" class="net.targetgroup.broker.customer.CustomerAddressDAOHibernate">
    		<property name="sessionFactory">
    			<ref local="sessionFactory" />
    		</property>
    	</bean>
    
    	<bean id="applicationDAO" class="net.targetgroup.broker.application.ApplicationDAOHibernate">
    		<property name="sessionFactory">
    			<ref local="sessionFactory" />
    		</property>
    	</bean>
    
    	<bean id="lenderDAO" class="net.targetgroup.broker.lender.LenderDAOHibernate">
    		<property name="sessionFactory">
    			<ref local="sessionFactory" />
    		</property>
    	</bean>
    
    	<bean id="notesDAO" class="net.targetgroup.broker.notes.NotesDAOHibernate">
    		<property name="sessionFactory">
    			<ref local="sessionFactory" />
    		</property>
    	</bean> 
    
    	<bean id="securedPropertyDAO" class="net.targetgroup.broker.application.SecuredPropertyDAOHibernate">
    		<property name="sessionFactory">
    			<ref local="sessionFactory" />
    		</property>
    	</bean>
    
    
    </beans>

  2. #2
    Join Date
    Sep 2004
    Posts
    602

    Default Re: Classes extending HibernateDaoSupport not closing connec

    Quote Originally Posted by Paul Newport
    We have some pretty basic hibernate classes that extend HibernateDaoSupport. These are running within session beans and get their connections from a Websphere datasource.
    To answer my own question, I now suspect that the reason being is that the EJB method being called is not part of a transaction, so the connections never get freed up.

  3. #3
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    It's Hibernate that ends up opening the connection, and Hibernate that actually closes it. This is tied to the lifetime of the Session.

    Now transactions are only related to things in that if you have a transaction the normal setup is that the Session lifetime is scoped to the transaction. In your case, if you have no transactions and thus no transaction synchronization going on, then each HibernateTemplate call is going to create and destroy its own Session, and that will result in a connection open and close.

    You should be able to see Session lifetimes by looking at your log, as long as you have Debug level information turned on.
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  4. #4
    Join Date
    Sep 2004
    Posts
    602

    Default

    Quote Originally Posted by Colin Sampaleanu
    In your case, if you have no transactions and thus no transaction synchronization going on, then each HibernateTemplate call is going to create and destroy its own Session, and that will result in a connection open and close.

    You should be able to see Session lifetimes by looking at your log, as long as you have Debug level information turned on.
    Thanks for that. I'll look at the debug logs then (which is another issue in itself, see here:

    http://www.webspherepower.com/issues.../00001281.html

Similar Threads

  1. Connection is not closing
    By anieshuk in forum Data
    Replies: 7
    Last Post: Aug 25th, 2005, 06:42 PM
  2. Replies: 6
    Last Post: May 25th, 2005, 01:56 AM
  3. BatchSqlUpdate closing the Connection
    By jonnygraham in forum Data
    Replies: 1
    Last Post: Mar 7th, 2005, 07:27 PM
  4. Extending Spring classes
    By emarceta in forum Architecture
    Replies: 5
    Last Post: Jan 19th, 2005, 12:46 AM
  5. Replies: 12
    Last Post: Oct 28th, 2004, 11:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •