Kabira Speaking
Kabira Speaking

Reputation: 237

Issue with Multiple Hibernate Transaction Manager

I am working with Spring 3.2.5.Release and Hibernate 4.2.7.Final. I am deploying my ear on Websphere 7 server. I have two application context files and each has its own sessionFactory of type

   <bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">....</bean>


   <bean id="sessionFactory1"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" lazy-init="true">... </bean> 

The issue is with multiple HibernateTransactionManager. I am defining two transaction managers as

   <bean id="transactionManager"
     class="org.springframework.orm.hibernate4.HibernateTransactionManager">
     <property name="sessionFactory" ref="sessionFactory" />
     <qualifier value="transactionManager"/>
   </bean>

and

   <bean id="txManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory1" />
    <qualifier value="txManager"/>
</bean>

In one of the context files I have mentioned this

    <tx:annotation-driven/>
<bean
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

However, during runtime of this code,

    loggingManager.persistLog(loadLog); // using txManager
    .....
    persistenceService.persist(dataEntity);  // using transactionManager

I get the below exception

    org.springframework.transaction.IllegalTransactionStateException: No existing transaction found for transaction marked with propagation 'mandatory'

Please note that the first transaction is success. I see the id generated for loadLog. I've annoted my Service layer with

    @Transactional(value="txManager") // with respective transaction manager

And my DAO layer with

    @Transactional(propagation = Propagation.MANDATORY) 

Is there anything that could be done to enable both the transaction managers?

Upvotes: 0

Views: 2230

Answers (1)

Manuel Jordan
Manuel Jordan

Reputation: 16271

The service is using the txManager

@Transactional(value="txManager") 

That service is calling a repository or dao, it must belong or runs within the service transaction too. You must do the following:

@Transactional(value="txManager" propagation = Propagation.MANDATORY) 

Why is Propagation.MANDATORY? Why not Propagation.REQUIRED? Or just only

@Transactional(value="txManager") 

Since Propagation.REQUIRED is used used by default.

BTW consider use only @Transactional if your @Transactional services would be working either with txManager or transactionManager

Upvotes: 1

Related Questions