开发者社区 问答 正文

spring aop 事务回滚失败(求助)? 400 报错

spring aop 事务回滚失败(求助)? 400 报错

在SSH下搭建的环境,但是就是事务回滚不成功,手动控制事务却成功。。。

声明式事务:

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


<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="REQUIRED" /><!--之前是NOT_SUPPORT -->
<tx:method name="find*" read-only="true" propagation="REQUIRED" /><!--之前是NOT_SUPPORT -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<!--默认其他方法都是REQUIRED -->
<tx:method name="*" />
</tx:attributes>
</tx:advice>


<aop:config>
<aop:pointcut id="txPoint" expression="execution(* com.xeon.mis.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint" />
</aop:config>
测试:

public class TestDao {
public void saveDemo() {
Demo a = new Demo();
a.setName("a");
Demo b = new Demo();
b.setName("b");
Session session = HibernateUtils.getSession();
session.save(a);
if (1 == 1)
throw new RuntimeException("运行时异常");//运行时异常
session.save(b);
HibernateUtils.closeSession();
}
}
觉得很难找到问题所在。

展开
收起
爱吃鱼的程序员 2020-06-01 14:26:42 748 分享 版权
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    这个dao你明显自己管理session了嘛。

    spring的transation要求datasource也必须是spring管理并从spring里面获得。

    ######回复 @稀薄空气 :已解决,多谢######麻烦告诉下具体需要怎么修改,这个困扰我很久了######Testdao的package跟你拦截的package匹配不?
    2020-06-01 14:26:43
    赞同 展开评论