Announcement Announcement Module
Collapse
No announcement yet.
Classes extending HibernateDaoSupport not closing connection Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Re: Classes extending HibernateDaoSupport not closing connec

    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.

    Comment


    • #3
      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.

      Comment


      • #4
        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

        Comment

        Working...
        X