JPA Multiple Transaction Managers

前端 未结 2 1462
庸人自扰
庸人自扰 2020-12-16 00:59

I have one applicationContext.xml file, and it has two org.springframework.orm.jpa.JpaTransactionManager (each with its own persistence unit, different databases) configured

相关标签:
2条回答
  • 2020-12-16 01:40

    The only situation in which you can have two Spring transaction managers is if you never have both transactions open at one time. This is not intrinsically to do with distributed transactions - the same restrictions apply even if you want the two datasources to have completely separate (but potentially overlapping in time) transaction lifecyles.

    Internally Spring's transaction managers all use Spring's TransactionSynchronizationManager which keeps a bunch of critical state in static ThreadLocal variables, so transaction managers are guaranteed to stomp all over each other's state.

    0 讨论(0)
  • 2020-12-16 01:49

    I guess you have 2 choices

    If your use-cases never require updates to both databases within the same transaction, then you can use two JpaTransactionManagers, but I'm not sure you will be able to use the @Transactional approach? In this case, you would need to fallback on the older mechanism of using a simple TransactionProxyFactoryBean to define transaction boundaries, eg:

    <bean id="firstRealService" class="com.acme.FirstServiceImpl"/>
    <bean id="firstService"  
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager" ref="firstJpaTm"/>
        <property name="target" ref="firstRealService"/>
        <property name="transactionAttributes">
            <props>
               <prop key="insert*">PROPAGATION_REQUIRED</prop>
               <prop key="update*">PROPAGATION_REQUIRED</prop>
               <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
            </props>
        </property>
    </bean>
    <!-- similar for your second service -->
    

    If you are require a transaction spanning both databases, then you will need to use a JTA transaction manager. The API states:

    This transaction manager is appropriate for applications that use a single JPA EntityManagerFactory for transactional data access. JTA (usually through JtaTransactionManager) is necessary for accessing multiple transactional resources within the same transaction. Note that you need to configure your JPA provider accordingly in order to make it participate in JTA transactions.

    What this means is that you will need to provide a JTA transaction manager. In our application, we use config similar to the following:

    <tx:annotation-driven transaction-manager="txManager"/>
    
    <bean id="txManager" 
        class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManagerName" value="appserver/jndi/path" />
    </bean>
    

    If you are deploying within an appserver, then the spring JtaTransactionManager needs to do a lookup to the real XA-compliant JTA transaction manager provided by the appserver. However, you can also use a standalone JTA transaction manager (but I haven't tried this myself yet)

    As for configuring the Jpa persistence provider, I'm not that familiar. What JPA persistence provider are you using?

    The code above is based on our approach, where we were using native Hibernate as opposed to Hibernate's JPA implementation. In this case, we were able to get rid of the two HibernateTransactionManager beans, and simply ensure that both SessionFactories were injected with the same JTA TM, and then use the tx:annotation-driven element.

    Hope this helps

    0 讨论(0)
提交回复
热议问题