2
votes

I was working on an spring application before which uses HibernateTransactionManager for transactions with queries/updates using jdbc template and works fine as well. Below was the code used

<bean id="sybaseDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="MySybaseDS"/>
    <property name="lookupOnStartup" value="false"/>
    <property name="cache" value="true" />
    <property name="proxyInterface"  value="javax.sql.DataSource" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="sybaseDataSource" />
    <property name="mappingResources">
        <list>..........</list>
    </property>
    <property name="hibernateProperties">
        <props>
             <prop key="hibernate.dialect">
                org.hibernate.dialect.SybaseAnywhereDialect
             </prop>
             <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<tx:annotation-driven transaction-manager="txManager"/>

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

I am creating a new application and confused now whether to use the HibernateTransactionManager or JpaTransactionManager or both(possible?).

The application will use both HibernateTemplate and JdbcTemplate(for bulk updates) for database operations and need to be transactional.

I mean, can/should I use HibernateTransactionManager for transactions with jdbctemplate and HibernateTemplate database operations? Will there be any performance bottleneck of using one transaction manager over another?

2

2 Answers

1
votes

The short answer is that it will work. See also this thread for a description of the low-level details.

1
votes

If the application is JPA based (an entity manager factory is configured), then the JpaTransactionManager should be used.

This transaction manager binds an entity manager to the thread during the duration of the transaction.

The HibernateTransactionManager is for applications that don't use JPA but use instead use Hibernate directly (a session factory is configured).

This transaction manager binds a hibernate session to the thread instead of an entity manager.

They do basically the same, it's just one handles a Session while the other handles an EntityManager. Their choice is simply dictated by the fact that JPA is used or not.

Both transaction managers are compatible with the JdbcTemplate, according to their respective javadocs.

There are no performance consequences of using one transaction manager versus the other.

Internally the entity manager uses a Hibernate session, so the end result is the same: a hibernate session is binded to the thread directly or indirectly during the duration of the transaction.