Skip to content

support for multiple transaction managers with @Transactional / <tx:annotation-driven> [SPR-3955] #8635

Closed

Description

@spring-projects-issues

Brad C. opened SPR-3955 and commented

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.


Affects: 2.0.7

Issue Links:

Referenced from: commits 0698947

20 votes, 24 watchers

Activity

  1. spring-projects-issues commented on Oct 3, 2007

    @spring-projects-issues
    CollaboratorAuthor

    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 @Transactional annotation.

    This bug is listed as minor since their are several other options for declarative transactions (just none that work with annotations).

  2. spring-projects-issues commented on Nov 26, 2007

    @spring-projects-issues
    CollaboratorAuthor

    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 @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" />

    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.

  3. spring-projects-issues commented on Feb 8, 2009

    @spring-projects-issues
    CollaboratorAuthor

    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. :-)

  4. spring-projects-issues commented on Mar 16, 2009

    @spring-projects-issues
    CollaboratorAuthor

    Keywan Ghadami commented

    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.

    <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>

  5. spring-projects-issues commented on Apr 9, 2009

    @spring-projects-issues
    CollaboratorAuthor

    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.

  6. spring-projects-issues commented on Apr 30, 2009

    @spring-projects-issues
    CollaboratorAuthor

    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. @Transactional is more specific so that transaction manager should overrule any other transaction manager config, I think.

  7. spring-projects-issues commented on May 8, 2009

    @spring-projects-issues
    CollaboratorAuthor

    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 @Transactional now. Imagine a service class like the following...

    public class TransactionalService {

    @Transactional("order")
    public void setSomething(String name) {
        ...
    }
    
    @Transactional("account")
    public void doSomething() {
        ...
    }
    

    }

    These qualifiers are identical to autowiring qualifiers in that they may be used for narrowing among a set of PlatformTransactionManager beans:

    <tx:annotation-driven/>
    
    <bean id="transactionManager1" class="org.springframework.jdbc.DataSourceTransactionManager">
        ...
        <qualifier value="order"/>
    </bean>
    
    <bean id="transactionManager2" class="org.springframework.jdbc.DataSourceTransactionManager">
        ...
        <qualifier value="account"/>
    </bean>
    

    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.

    <bean id="transactionManager" class="org.springframework.jdbc.DataSourceTransactionManager">
        ...
    </bean>
    
    <bean id="transactionManager2" class="org.springframework.jdbc.DataSourceTransactionManager">
        ...
        <qualifier value="account"/>
    </bean>
    

    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() {
        ...
    }
    

    }

    <bean id="orderTransactionManager" class="org.springframework.jdbc.DataSourceTransactionManager">
        ...
    </bean>
    
    <bean id="accountTransactionManager" class="org.springframework.jdbc.DataSourceTransactionManager">
        ...
    </bean>
    

    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...

    Let me know what you think.

    Juergen

  8. spring-projects-issues commented on May 8, 2009

    @spring-projects-issues
    CollaboratorAuthor

    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!

  9. spring-projects-issues commented on May 8, 2009

    @spring-projects-issues
    CollaboratorAuthor

    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

  10. spring-projects-issues commented on May 8, 2009

    @spring-projects-issues
    CollaboratorAuthor

    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

  11. spring-projects-issues commented on May 26, 2009

    @spring-projects-issues
    CollaboratorAuthor

    John Hayward commented

    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.

    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

  12. spring-projects-issues commented on Sep 21, 2009

    @spring-projects-issues
    CollaboratorAuthor

    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?

  13. spring-projects-issues commented on Sep 22, 2009

    @spring-projects-issues
    CollaboratorAuthor

    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

  14. spring-projects-issues commented on Sep 22, 2009

    @spring-projects-issues
    CollaboratorAuthor

    Michael Pilquist commented

    Juergen, Thanks so much!

  15. spring-projects-issues commented on Dec 20, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Mike Youngstrom commented

    With the meta annotation option could I theoretically use both meta annotations on a single method?

    @OrderTx
    @AccountTx
    public void setSomething(String name) { ... }

    or do

    @OrderTx
    @Transactional //pointing to default transactionManager
    public void setSomething(String name) { ... }

    And have both transaction managers create and commit transactions?

  16. spring-projects-issues commented on Apr 16, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Brad Cupit commented

    Nice thinking @Mike Youngstrom, 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.

  17. added
    in: data
    type: enhancement
    has: votes-jira
    on Jan 11, 2019
  18. added this to the 3.0 M4 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

has: votes-jirain: datatype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions