简体   繁体   中英

Spring declarative transaction management : multiple pointcuts

I know its almost weekend but still worth trying:)

I need to use multiple transaction managers due to which it makes sense for me to go with declarative transactions management instead of using tx:annotation-driven. However, I have service classes in various packages and the following config does not work:

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="dataSource" ref="ds" />
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="svcPointcut1" expression="execution(* com.app.services.*.*(..))"/>
    <aop:pointcut id="svcPointcut2" expression="execution(* com.app.campaigns.services..*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="svcPointcut1" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="svcPointcut2" />
</aop:config>

Could someone please tell me why only first pointcut works and the second one doesn't? The service methods in com.app.services package execute in the context of a transaction but the service methods in com.app.campaigns.services (and the sub-package below it) throw UnsupportedException. Please get me out of this misery! Thanks a ton!

PS: The application uses Spring 2.5.6

I need to use multiple transaction managers due to which it makes sense for me to go with declarative transactions management instead of using tx:annotation-driven.

Both of those qualify as "declarative" transaction management. More importantly, though, you can still use annotation-driven transactions with multiple tx managers. Just give the name or qualifier of the manager as the "value" attribute of the annotation . With this XML:

<bean id="project1TransactionManager" class="...TransactionManager">
    <qualifier value="project1"/>
</bean>
<bean id="project2TransactionManager" class="...TransactionManager">
    <qualifier value="project2"/>
</bean>

Any of the following should work:

@Transactional("project1")
@Transactional("project1TransactionManager")
@Transactional("project2")
@Transactional("project2TransactionManager")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM