Atomikos Forum

Share connection between JdbcTemplate and HibernateTemplate

When using a mix of JdbcTemplate based DAOs and HibernateTemplate based DAOs, the Spring HibernateTransactionManager allows me to share the connection between those two templates. This means that when the two types of DAOs are used within the same transaction, they share the connection to the DB (and in our specific Oracle case, the Oracle session).

Below is the Spring config for this setup:

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan">
            <list>
                <value>...</value>
            </list>
        </property>
        <property name="annotatedPackages">
            <list>
                <value>...</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.transaction.factory_class">com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory
                </prop>
                <prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup
                </prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.cache.use_structured_entries">true</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
            </props>
        </property>
    </bean>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>

<bean id="dataSource"
      class="com.atomikos.jdbc.AtomikosDataSourceBean"
      init-method="init" destroy-method="close">
      <property name="uniqueResourceName"><value>XA_MNA</value></property>
      <property name="xaDataSourceClassName">
        <value>oracle.jdbc.xa.client.OracleXADataSource</value>
      </property>
      <property name="xaProperties">
                <props>
                        <prop key="user">...</prop>
                        <prop key="password">...</prop>
                        <prop key="URL">...</prop>
                </props>
      </property>         
      <property name="poolSize" value="3"/>
  </bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

<tx:annotation-driven />

Now I want to migrate from the HibernateTransactionManager to Atomikos. This basically means I define the following transactionManager, replacing the Hibernate one.

<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
        init-method="init" destroy-method="close">
        <property name="forceShutdown" value="false" />
    </bean>

    <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
        <property name="transactionTimeout" value="300" />
    </bean>
    
    <bean id="transactionManager"
        class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManager" ref="atomikosTransactionManager" />
        <property name="userTransaction" ref="atomikosUserTransaction" />
    </bean>

Now I notice the JdbcTemplate and the HibernateTemplate no longer share the connection, and consequently no longer share the Oracle session.

Is it possible that the JdbcTemplate and the HibernateTemplate still share the connnection using the Atomikos transaction manager? Something I might have overlooked?

Thanks for the feedback!
Rik Gruwez (2) Send private email
Thursday, June 09, 2011
 
 
To share the same connection from our pools, that connection would have to be 'closed' (i.e., marked as reusable in the pool) by either template... You can configure Hibernate to close connections early I guess, but not sure about the JdbcTemplate...

HTH
Guy Pardon Send private email
Thursday, June 09, 2011
 
 
Hmm, that makes it rather complicated. Apparently, this is an issue that was already reported in Spring back in 2006, but it remains unfixed as of today.

https://jira.springsource.org/browse/SPR-1976
Rik Gruwez (2) Send private email
Tuesday, June 14, 2011
 
 
Not sure how we could improve this on our end. Any suggestions?

Thanks
Guy Pardon Send private email
Tuesday, June 14, 2011
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics