spring事务遇到异常不回滚
测试代码:
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)
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)
...全文
相关推荐
Spring异常捕获且回滚事务解决方案
主要介绍了Spring异常捕获且回滚事务解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
spring事务异常回滚实例解析
主要介绍了spring事务异常回滚实例解析,具有一定借鉴价值,需要的朋友可以参考下
spring 简单实例 事务回滚
自己做的一个简单的spring运用实例,主要是关于事务回滚的一个小例子,希望对还在纠结事务回滚的童靴有点帮助。。。
Spring事务管理只对出现运行期异常进行回滚
Spring的事务管理默认只对出现运行期异常(java.lang.RuntimeException及其子类)进行回滚,需要了解更多Spring事务方面的知识,可详看本
java事务回滚失败问题分析
主要介绍了java事务回滚失败问题分析,具有一定借鉴价值,需要的朋友可以参考下


