You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
support for multiple transaction managers with @Transactional / <tx:annotation-driven> [SPR-3955] #8635
has: votes-jiraIssues migrated from JIRA with more than 10 votes at the time of importin: dataIssues in data modules (jdbc, orm, oxm, tx)type: enhancementA general enhancement
the @Transactional annotation / tx:annotation-driven feature only supports one transaction manager. The tx:annotation-driven xml configuration does permit naming a specific transaction manager for use, and the XSD schema permits multiple tx:annotation-driven elements, however there is no way to specify which transaction manager to use in the @Transactional annotation.
This is a proposal to add an optional field to the @Transactional annotation for specifying the bean name of the transaction manager to use. If a bean name is not specified, then it assumes there is only one tx:annotation-driven element in the xml and uses that transaction manager.
Of course, any better ideas to support multiple transaction managers with @Transactional are more than welcome.
I suspect most Spring apps do not use multiple transaction managers, even if they have multiple DataSources, SessionFactories, etc.. I imagine there would be one JTA transaction manager for doing XA transactions across the DataSources. The application I'm working on needs to connect to two databases (therefore two DataSources and two SessionFactories) however one of the databases will only have read operations, so there's never a need for XA transactions or two-phase commits. Two transaction managers will suit our needs just fine, as opposed to setting up JTA transactions (and either deploying in an Application Server or using a JTA implementation with Tomcat).
The only problem is that two transaction managers aren't compatible with the @Transactional annotation.
This bug is listed as minor since their are several other options for declarative transactions (just none that work with annotations).
this appears to have got a bit worse in the versions subsequent to 2.0.7.
Up to and including 2.0.7 you can actually have multiple tx:annotation-driven elements as long as you are happy to have the @transactional annotation to apply to both.
With 2.5 (and it turns out also 2.1-m3, 2.1-m4, 2.5.rc-1 and 2.5-rc2) only the first tx-annotation-driven element is recognised.
The example I was working with has
<tx:annotation-driven
transaction-manager="jmsTransactionManager"
order="100"
proxy-target-class="true" />
<tx:annotation-driven
transaction-manager="jdbcTransactionManager"
order="200"
proxy-target-class="true" />
Unfortunately it appears broken after spring 2.0.7 as only one of the TransactionInterceptor instances gets created.
I can shuffle the order of the two elements to get either of the instantiated but cannot get both instantiated any more.
I would like to have multible TransactionManager but i would like @Transactional to stay without a name attribute.
To get more flexibility the tx:annotation-driven configuration could be extended to specify the packages and classes to be warped by a proxy.
+1 on Keywan's suggestion, if tx:annotation-driven/ tag was able to target specific classes/packages the knowledge of which transaction manager bean should be used is kept out of the code.
We're also having this problem with multiple TX managers and using @Transactional. Currently we fixed it by using @TransactionConfiguration, but since this class is in the spring-test package, we now have to add that package to production code.
I would prefer a 'transactionManager' property to be added to @Transactional, but I can see the advantage of choosing the TX based on the package. It should be possible for both options to coexist. Only a matter of determining the precedence. @Transactional is more specific so that transaction manager should overrule any other transaction manager config, I think.
The default tx:annotation-driven target bean name "transactionManager" is still used if no specifically qualified PlatformTransactionManager bean has been found. So e.g. if the first of the two transaction manager beans above wouldn't have the <qualifier> element attached it, you could simply name it "transactionManager" and rely on it as the default transaction manager being chosen.
Like with autowiring, qualifier values can also match against bean names... provided that your qualifier values are actually unique among all bean names. In the example above, you can see that qualifier values only really have to be unique among PlatformTransactionManager beans, since they are only used for differentiating among those. Nevertheless, the following would work as well:
public class TransactionalService {
@Transactional("orderTransactionManager")
public void setSomething(String name) {
...
}
@Transactional("accountTransactionManager")
public void doSomething() {
...
}
Now, you might wonder whether you have to repeat the @Transactional qualifier value for each and every annotated transactional method? Spring 3.0 M3's meta-annotation model comes to the rescue, allowing you to define custom shortcut annotations for your specific use cases (here defining qualifiers but potentially also propagation behavior, rollback rules, timeouts etc):
@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Transactional("order")
public @interface OrderTx {
}
@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Transactional("account")
public @interface AccountTx {
}
public class TransactionalService {
@OrderTx
public void setSomething(String name) {
...
}
@AccountTx
public void doSomething() {
...
}
}
Overall, this is not only more flexible than package based differentiation; it is also similar to other solutions in Spring 3.0. Package names, as suggested, would be fine as a basic differentiator but smell more like AOP pointcuts combined with the tx:advice model. And qualifiers are not quite as hard-coded as a "transactionManagerBeanName" attribute on @Transactional either...
I think that's going to work great. I'm still not too sure about adding implementation- or deployment-specific data into annotations, but the qualifiers do add an extra level of indirection so it should be ok in practice. The meta-annotation facility looks excellent :-) Thanks, Juergen!
Thanks for the immediate feedback, Pavel! A first cut of this is committed in the meantime. It's going to see a bit of further polishing but functionally it should be complete already.
Juergen,
Looks very good. This setup would work very good for us.
AOP would indeed be a good option for package based transaction management so I don't see the need to add something extra to your proposed solution.
We operate in an environment with 2-3 datasources; for each we use a baseDAO. So our class (and the @Transactional annotation on it) is the same.
We instantiate 3 of these DAO's and assign a different datasource to each. It might be nicer to be able to specify the transaction manager on a per-instance (not per-class) basis.
It appears that the changes to TransactionAspectSupport.java result in a slight behavior change. Previously, if TransactionAspectSupport had a null bean factory, the result of the transaction advice was nullified. With the changes from this issue, the result is an exception like this:
java.lang.IllegalStateException: Cannot retrieve PlatformTransactionManager beans from non-listable BeanFactory: null
at org.springframework.transaction.interceptor.TransactionAspectSupport.determineTransactionManager(TransactionAspectSupport.java:278)
While it might seem strange to run code that has been woven with the transaction advice without tx:annotation-driven mode="aspectj", this is very useful in both unit test environments and when the author of the code does not know if the user will be in a aspectj enabled application context.
Would it be possible to restore the original behavior or have a way to toggle it on/off?
Michael, I've restored the original behavior of simply skipping transactional processing in such a case, which should hopefully work fine for your purposes.
John, instance-specific transaction manager hints are not supported at this point. It's not obvious how to do this in an elegant fashion when using annotations... Injecting a PlatformTransactionManager would mean that we'd have to pick it from a field based on some naming convention, which feels strange.
has: votes-jiraIssues migrated from JIRA with more than 10 votes at the time of importin: dataIssues in data modules (jdbc, orm, oxm, tx)type: enhancementA general enhancement
Activity
spring-projects-issues commentedon Oct 3, 2007
Brad C. commented
I suspect most Spring apps do not use multiple transaction managers, even if they have multiple DataSources, SessionFactories, etc.. I imagine there would be one JTA transaction manager for doing XA transactions across the DataSources. The application I'm working on needs to connect to two databases (therefore two DataSources and two SessionFactories) however one of the databases will only have read operations, so there's never a need for XA transactions or two-phase commits. Two transaction managers will suit our needs just fine, as opposed to setting up JTA transactions (and either deploying in an Application Server or using a JTA implementation with Tomcat).
The only problem is that two transaction managers aren't compatible with the
@Transactionalannotation.This bug is listed as minor since their are several other options for declarative transactions (just none that work with annotations).
spring-projects-issues commentedon Nov 26, 2007
Laurie Chan commented
this appears to have got a bit worse in the versions subsequent to 2.0.7.
Up to and including 2.0.7 you can actually have multiple tx:annotation-driven elements as long as you are happy to have the
@transactionalannotation to apply to both.With 2.5 (and it turns out also 2.1-m3, 2.1-m4, 2.5.rc-1 and 2.5-rc2) only the first tx-annotation-driven element is recognised.
The example I was working with has
<tx:annotation-driven
transaction-manager="jmsTransactionManager"
order="100"
proxy-target-class="true" />
<tx:annotation-driven
transaction-manager="jdbcTransactionManager"
order="200"
proxy-target-class="true" />
This was going to rely on a combination of the nested transaction-interceptors and resubmission/retry logic along the lines discussed here: http://dev2dev.bea.com/pub/a/2006/01/custom-mdb-processing.html?page=1
to give some of the characteristics of a rela XA transaction manager.
Unfortunately it appears broken after spring 2.0.7 as only one of the TransactionInterceptor instances gets created.
I can shuffle the order of the two elements to get either of the instantiated but cannot get both instantiated any more.
spring-projects-issues commentedon Feb 8, 2009
Kyrill Alyoshin commented
It sure would be nice to be able to specify under which transaction manager a bean should run. In my current project I have 4 data sources. :-)
spring-projects-issues commentedon Mar 16, 2009
Keywan Ghadami commented
I would like to have multible TransactionManager but i would like
@Transactionalto stay without a name attribute.To get more flexibility the tx:annotation-driven configuration could be extended to specify the packages and classes to be warped by a proxy.
<tx:annotation-driven transaction-manager="jpaTransactionManager1"
<tx:package name="com.comp.modul1.x" />
<tx:package name="com.comp.modul1.y" />
</tx:annotation>
<tx:annotation-driven transaction-manager="jpaTransactionManager2"
<tx:package name="com.comp.modul2" />
</tx:annotation>
spring-projects-issues commentedon Apr 9, 2009
Pavel Tcholakov commented
+1 on Keywan's suggestion, if tx:annotation-driven/ tag was able to target specific classes/packages the knowledge of which transaction manager bean should be used is kept out of the code.
spring-projects-issues commentedon Apr 30, 2009
Joost den Boer commented
We're also having this problem with multiple TX managers and using
@Transactional. Currently we fixed it by using@TransactionConfiguration, but since this class is in the spring-test package, we now have to add that package to production code.I would prefer a 'transactionManager' property to be added to
@Transactional, but I can see the advantage of choosing the TX based on the package. It should be possible for both options to coexist. Only a matter of determining the precedence.@Transactionalis more specific so that transaction manager should overrule any other transaction manager config, I think.spring-projects-issues commentedon May 8, 2009
Juergen Hoeller commented
For Spring 3.0 RC1, I've implemented the following solution (still to be polished before committing):
There is a new qualifier value attribute on
@Transactionalnow. Imagine a service class like the following...public class TransactionalService {
}
These qualifiers are identical to autowiring qualifiers in that they may be used for narrowing among a set of PlatformTransactionManager beans:
The default tx:annotation-driven target bean name "transactionManager" is still used if no specifically qualified PlatformTransactionManager bean has been found. So e.g. if the first of the two transaction manager beans above wouldn't have the <qualifier> element attached it, you could simply name it "transactionManager" and rely on it as the default transaction manager being chosen.
Like with autowiring, qualifier values can also match against bean names... provided that your qualifier values are actually unique among all bean names. In the example above, you can see that qualifier values only really have to be unique among PlatformTransactionManager beans, since they are only used for differentiating among those. Nevertheless, the following would work as well:
public class TransactionalService {
}
Now, you might wonder whether you have to repeat the
@Transactionalqualifier value for each and every annotated transactional method? Spring 3.0 M3's meta-annotation model comes to the rescue, allowing you to define custom shortcut annotations for your specific use cases (here defining qualifiers but potentially also propagation behavior, rollback rules, timeouts etc):@Target({ElementType.METHOD, ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Transactional("order")public
@interfaceOrderTx {}
@Target({ElementType.METHOD, ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Transactional("account")public
@interfaceAccountTx {}
public class TransactionalService {
}
Overall, this is not only more flexible than package based differentiation; it is also similar to other solutions in Spring 3.0. Package names, as suggested, would be fine as a basic differentiator but smell more like AOP pointcuts combined with the tx:advice model. And qualifiers are not quite as hard-coded as a "transactionManagerBeanName" attribute on
@Transactionaleither...Let me know what you think.
Juergen
spring-projects-issues commentedon May 8, 2009
Pavel Tcholakov commented
I think that's going to work great. I'm still not too sure about adding implementation- or deployment-specific data into annotations, but the qualifiers do add an extra level of indirection so it should be ok in practice. The meta-annotation facility looks excellent :-) Thanks, Juergen!
spring-projects-issues commentedon May 8, 2009
Juergen Hoeller commented
Thanks for the immediate feedback, Pavel! A first cut of this is committed in the meantime. It's going to see a bit of further polishing but functionally it should be complete already.
Juergen
spring-projects-issues commentedon May 8, 2009
Joost den Boer commented
Juergen,
Looks very good. This setup would work very good for us.
AOP would indeed be a good option for package based transaction management so I don't see the need to add something extra to your proposed solution.
Joost
spring-projects-issues commentedon May 26, 2009
John Hayward commented
We operate in an environment with 2-3 datasources; for each we use a baseDAO. So our class (and the
@Transactionalannotation on it) is the same.We instantiate 3 of these DAO's and assign a different datasource to each. It might be nicer to be able to specify the transaction manager on a per-instance (not per-class) basis.
Is that possible?
<bean id="localDAO" class="com.company.dao.hibernate.BaseDAOHibernate">
<property name="sessionFactory" ref="localSessionFactory" />
<property name="transactionManager" ref="localTxManager" />
</bean>
<bean id="remoteDAO" class="com.company.dao.hibernate.BaseDAOHibernate">
<property name="sessionFactory" ref="remoteSessionFactory" />
<property name="transactionManager" ref="remoteTxManager" />
</bean>
-John
spring-projects-issues commentedon Sep 21, 2009
Michael Pilquist commented
It appears that the changes to TransactionAspectSupport.java result in a slight behavior change. Previously, if TransactionAspectSupport had a null bean factory, the result of the transaction advice was nullified. With the changes from this issue, the result is an exception like this:
java.lang.IllegalStateException: Cannot retrieve PlatformTransactionManager beans from non-listable BeanFactory: null
at org.springframework.transaction.interceptor.TransactionAspectSupport.determineTransactionManager(TransactionAspectSupport.java:278)
While it might seem strange to run code that has been woven with the transaction advice without tx:annotation-driven mode="aspectj", this is very useful in both unit test environments and when the author of the code does not know if the user will be in a aspectj enabled application context.
Would it be possible to restore the original behavior or have a way to toggle it on/off?
spring-projects-issues commentedon Sep 22, 2009
Juergen Hoeller commented
Michael, I've restored the original behavior of simply skipping transactional processing in such a case, which should hopefully work fine for your purposes.
John, instance-specific transaction manager hints are not supported at this point. It's not obvious how to do this in an elegant fashion when using annotations... Injecting a PlatformTransactionManager would mean that we'd have to pick it from a field based on some naming convention, which feels strange.
Juergen
spring-projects-issues commentedon Sep 22, 2009
Michael Pilquist commented
Juergen, Thanks so much!
spring-projects-issues commentedon Dec 20, 2011
Mike Youngstrom commented
With the meta annotation option could I theoretically use both meta annotations on a single method?
@OrderTx@AccountTxpublic void setSomething(String name) { ... }
or do
@OrderTx@Transactional//pointing to default transactionManagerpublic void setSomething(String name) { ... }
And have both transaction managers create and commit transactions?
spring-projects-issues commentedon Apr 16, 2013
Brad Cupit commented
Nice thinking
@MikeYoungstrom, but unfortunately it doesn't work.I tried two meta annotations, one per TransactionManager, and applied them both to a method. Spring picked one and ignored the other.
As a workaround, you can setup AOP rules to apply both transactions managers to the same method, but that means ditching
@Transactional.