spring事务遇到异常不回滚

yanweimin5 yanweimin5 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
测试代码:
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentDAOI studao = (StudentDAOI)app.getBean("studentDaoProxy");
studao.save(student);

save方法:
public void save(Student student) {
getHibernateTemplate().save(student);
throw new RuntimeException("aaaaaa");
}

配置如下:

<bean id="studentDaoProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- <property name="proxyTargetClass">-->
<!-- <value>true</value>-->
<!-- </property>-->
<property name="proxyInterfaces">
<value>com.jwgl.vo.StudentDAOI</value>
</property>
<property name="target">
<ref bean="StudentDAO" />
</property>
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="save">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

不抛异常的时候能够正常插入数据: 抛出运行时异常是报错如下:

Exception in thread "main" java.lang.RuntimeException: aaaaaa
at com.jwgl.vo.StudentDAO.save(StudentDAO.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy1.save(Unknown Source)
at com.jwgl.test.StudentDaoTest.main(StudentDaoTest.java:24)
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
yanweimin5
Java EE 67,535 成员 225,843 内容