【问题标题】:Spring declarative transaction management : multiple pointcutsSpring 声明式事务管理:多个切入点
【发布时间】:2011-10-06 12:25:31
【问题描述】:

我知道快到周末了,但仍然值得一试 :)

我需要使用多个事务管理器,因此使用声明式事务管理而不是使用 tx:annotation-driven 对我来说是有意义的。但是,我在各种包中都有服务类,并且以下配置不起作用:

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

谁能告诉我为什么只有第一个切入点有效而第二个无效? com.app.services 包中的服务方法在事务上下文中执行,但 com.app.campaigns.services(及其下的子包)中的服务方法抛出 UnsupportedException。 请让我摆脱这种痛苦!非常感谢!

PS:应用使用Spring 2.5.6

【问题讨论】:

  • 你能发布错误的堆栈跟踪吗?

标签: java hibernate spring transactions aop


【解决方案1】:

我需要使用多个事务管理器,因为它使 对我来说,使用声明式事务管理而不是 使用 tx:annotation-driven。

这两者都可以称为“声明式”事务管理。不过,更重要的是,您仍然可以将注释驱动的事务与多个 tx 管理器一起使用。只需将经理的名称或限定词命名为"value" attribute of the annotation。使用此 XML:

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

以下任何一项都应该有效:

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

【讨论】:

  • 我应该提到我需要这个才能与 Spring 2.5.6 一起使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-18
  • 1970-01-01
  • 1970-01-01
  • 2013-02-19
  • 2011-04-12
  • 1970-01-01
  • 2011-11-04
相关资源
最近更新 更多