[事物唯一性]spring AOP 事物回滚不同步

项目架构 Hinbernate + Spring

使用spring声明式事物管理,配置如下:
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="HibernateSessionFactory" />
</bean>
<aop:config>
<aop:pointcut id="moaService" expression="execution(public * com.teamsun.moa.service.Impl..*.*(..))" />
<aop:pointcut id="sdfService" expression="execution(public * com.teamsun.sdf.service.impl..*.*(..))" />
<aop:pointcut id="frameWorkService" expression="execution(public * com.teamsun.web.framework.service.impl..*.*(..))" />
<aop:pointcut id="formService" expression="execution(public * com.teamsun.web.framework.form.service.impl..*.*(..))" />
<aop:pointcut id="layoutService" expression="execution(public * com.teamsun.web.framework.layout.service.impl..*.*(..))" />
<aop:pointcut id="systemService" expression="execution(public * com.teamsun.web.framework.system.service.impl..*.*(..))" />
<aop:pointcut id="toolsService" expression="execution(public * com.teamsun.web.framework.tools.service.impl..*.*(..))" />
<aop:pointcut id="widgetService" expression="execution(public * com.teamsun.web.framework.widget.service.impl..*.*(..))" />
<aop:pointcut id="workflowService" expression="execution(public * com.teamsun.web.framework.workflow.service.impl..*.*(..))" />

<aop:advisor pointcut-ref="moaService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="sdfService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="frameWorkService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="formService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="layoutService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="systemService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="toolsService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="widgetService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="workflowService" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true" />
<tx:method name="get*" read-only="true" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
利用Aop切面管理所有业务service类,进行事物管理。

但service层在调用多个dao操作时,无法完成事物同步回滚。
DAO_A.save(a);
DAO_B.save(a);
当其中一个dao执行出错时,hinbernate不会立即抛出异常,而且等所有代码执行完毕后在抛出异常。

这时其中一个dao层的sql还会执行。不能同步回滚,不解。

利用手动回滚,可正常同步回滚事物。

DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);;
/HibernateTransactionManager txManager = new HibernateTransactionManager();
TransactionStatus status = txManager.getTransaction(def);;
txManager.rollback(status);

请spring高手解释原因何在?是否配置有问题?

注:不使用TransactionProxyFactoryBean代理配置形式。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值