13
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="data.emf" />
</bean>

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


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

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


In my service layer, can I use @Transactional(name="transactionManager2"); to identify which transaction manager I use if I have multiple transaction managers?

informatik01
  • 16,038
  • 10
  • 74
  • 104
cometta
  • 35,071
  • 77
  • 215
  • 324

2 Answers2

25

You can specify which tx manager to use with @Transactional using the value attribute:

A qualifier value for the specified transaction.

May be used to determine the target transaction manager, matching the qualifier value (or the bean name) of a specific PlatformTransactionManager bean definition.

For example:

@Transactional("txManager1");

Alternatively, you can use the more explicit TransactionProxyFactoryBean, which gives you finer-grained control over what objects gets proxied by what tx managers. This still uses the annotations, but it doesn't auto-detect beans, it's configured explicitly on a bean-by-bean basis.

This normally isn't an issue, but it's not wise to have multiple transaction managers unless you have a very good reason to do so. If you find yourself needing two tx managers, it's usually better to see if you can make do with one. For example, if you have two data sources configured in your app server, you can incorporate both in a single JtaTransactionManager, rather than two seperate JpaTransactionManager or DataSourceTransactionmanagers.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • please comment on my post at http://stackoverflow.com/questions/1962509/which-sessionfactory-should-be-use-for-transactionmanager – cometta Dec 26 '09 at 02:59
  • i read from http://stackoverflow.com/questions/1902997/multiple-database-with-springhibernatejpa , it's using multiple org.springframework.orm.jpa.JpaTransactionManager and will be pointed by @transactional("name") . so this way is ok? – cometta Dec 26 '09 at 03:36
  • 1
    @skaffman, how can I associate two data sources to the same transactionmanager if I am using DataSourceTransactionManager? – tsunade21 Mar 18 '11 at 11:25
  • 2
    @skaffman, I am working with jdbc templates and org.apache.commons.dbcp.BasicDataSource, what should I do to have transactionManager for two datasources? Thanks again – tsunade21 Mar 18 '11 at 11:41
  • 4
    @tsunade21: You open up a new question. – skaffman Mar 18 '11 at 11:56
  • 1
    [Spring JTA multiple resource transactions in Tomcat with Atomikos example](http://www.javacodegeeks.com/2013/07/spring-jta-multiple-resource-transactions-in-tomcat-with-atomikos-example.html) – anasanjaria Jan 14 '14 at 17:31
  • @skaffman Are you aware of any pros/cons regarding performance when using JTA instead Local JpaTransactionManager? – svlada May 10 '15 at 07:09
  • Note you have to use `org.springframework.transaction.annotation` in this case, not `javax.*`. – membersound Nov 20 '17 at 14:26
2

More on the need for more than one transaction manager. You might be trying to do nested or separate transactions in sequence -- then you can use different propagation settings. You can achieve that with configuration using single transaction manager see Transaction propagation.

Zoran Regvart
  • 4,630
  • 22
  • 35