2
votes

I am using Spring(3.1.4 release) with HibernateTemplate(3.6.7.Final) and Spring Data JPA(1.3.0.RELEASE). I have defined two transaction managers, one for Hibernate and another for Spring Data JPA, in my configuration file. Both the transaction managers are using different data source.

<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="transactionTemplate"      class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="transactionManager" />
</bean>

I want some methods in my code to use Hibernate's transaction manager and some methods to use Spring data JPA's transaction manager. (Spring Doc link - 10.5.6.2 Multiple Transaction Managers with @Transactional)

I have defined annotation driven transaction in my configuration file, which by default is picking Hibernate's transaction template

<tx:annotation-driven />

For running methods with Spring Data JPA's transaction manager I am mentioning name of transaction manager in my method definition, i.e. @Transactional(value = "jpaTransactionManager") , but after inserting the record in DB the thread gets hanged and further code is not called.

@Transactional(value = "jpaTransactionManager")
public void transactionMethod()
{
Object obj = createObj();
repository.save(obj); //data is inserted in DB here but after that thread is getting hanged here and below business logic is not called

// Some business logic
}

Methods using Hibernate's transaction manager are working fine, but methods using Spring Data JPA's transaction manager is not working.

I have tried following things without any success

  • Using two with both transaction managers.
  • Annotating @Transactional(value = "jpaTransactionManager") both in class level and method level.

Really appreciate any help in above issue.

1

1 Answers

0
votes

Try explicitly naming the transaction managers. Use something besides the default "transactionManager" for both, and update all bean references to explicitly state which transactionManager they need. Also, I think you'll need two "" declarations.

Something like this:

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

<bean id="transactionTemplate"      class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="hibernateTransactionManager" />
</bean>