<p>I have one applicationContext.xml file, and it has two org.springframework.orm.jpa.JpaTransactionManager (each with its own persistence unit, different databases) configured in a Spring middleware custom application.
<br><br>I want to use annotation based transactions (@Transactional), to not mess around with TransactionStatus commit, save, and rollback.<br><br>
A coworker mentioned that something gets confused doing this when there are multiple transaction managers, even though the context file is set configured correctly (the references go to the correct persistence unit.
Anyone ever see an issue?</p>

<hr>

<p>In your config, would you have two transaction managers?
Would you have txManager1 and txManager2?<br><br>
That's what I have with JPA, two different Spring beans that are transaction managers.</p>

[{'answer_id': 78479, 'author': 'toolkit', 'author_id': 3295, 'author_profile': 'https://Stackoverflow.com/users/3295', 'pm_score': 4, 'selected': True, 'text': '<p>I guess you have 2 choices</p>\n\n<p>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 <a href="http://static.springframework.org/spring/docs/2.5.5/api/org/springframework/transaction/interceptor/TransactionProxyFactoryBean.html" rel="noreferrer">TransactionProxyFactoryBean</a> to define transaction boundaries, eg:</p>\n\n<pre><code>&lt;bean id="firstRealService" class="com.acme.FirstServiceImpl"/&gt;\n&lt;bean id="firstService"  \n    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"&gt;\n    &lt;property name="transactionManager" ref="firstJpaTm"/&gt;\n    &lt;property name="target" ref="firstRealService"/&gt;\n    &lt;property name="transactionAttributes"&gt;\n        &lt;props&gt;\n           &lt;prop key="insert*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;\n           &lt;prop key="update*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;\n           &lt;prop key="*"&gt;PROPAGATION_REQUIRED,readOnly&lt;/prop&gt;\n        &lt;/props&gt;\n    &lt;/property&gt;\n&lt;/bean&gt;\n&lt;!-- similar for your second service --&gt;\n</code></pre>\n\n<p>If you are require a transaction spanning both databases, then you will need to use a JTA transaction manager. The <a href="http://static.springframework.org/spring/docs/2.5.5/api/org/springframework/orm/jpa/JpaTransactionManager.html" rel="noreferrer">API</a> states:</p>\n\n<blockquote>\n  <p>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.</p>\n</blockquote>\n\n<p>What this means is that you will need to provide a JTA transaction manager. In our application, we use config similar to the following:</p>\n\n<pre><code>&lt;tx:annotation-driven transaction-manager="txManager"/&gt;\n\n&lt;bean id="txManager" \n    class="org.springframework.transaction.jta.JtaTransactionManager"&gt;\n    &lt;property name="transactionManagerName" value="appserver/jndi/path" /&gt;\n&lt;/bean&gt;\n</code></pre>\n\n<p>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)</p>\n\n<p>As for configuring the Jpa persistence provider, I\'m not that familiar. What JPA persistence provider are you using?</p>\n\n<p>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.</p>\n\n<p>Hope this helps</p>\n'}, {'answer_id': 280875, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n\n<p>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.</p>\n"}]
['https://Stackoverflow.com/questions/75700', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/13143/']