spring+hibernate配置的事务不回滚
我用ssh组合的一个小程序,在事物处理中,我专门给事物设置了一个错误要他抛出异常。但是怎么试都不能让事物回滚,请教一下。以下是代码
public boolean executeTransaction()
{
try
{
User user = new User();
user.setAge(111);
user.setFirstName("能");
user.setLastName("不能");
this.userDAO.addUser(user);
user = this.userDAO.findUserById(2);
user
.setLastName("星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星星");
this.userDAO.updateUser(user);
return true;
} catch (Exception ex)
{
//TransactionAspectSupport.currentTransactionStatus()
//.setRollbackOnly();
throw new
RuntimeException();
}
}
其中save是正确的,update是错误的。但是执行后没有将save回滚。(若数据正确则正常执行)请教大家,在网上看了很多解决办法都不行。下面是配置文件:
<bean id="trsactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="transactionDAO" class="com.test.DAO.impl.TransactionImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
<property name="userDAO" ref="userDao"></property>
</bean>
<bean id="userDao" class="com.test.DAO.impl.UserImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="trsactionManager"></property>
<property name="target" ref="transactionDAO"></property>
<property name="transactionAttributes">
<props>
<prop key="execute*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="trsactionService" class="com.test.service.impl.TransactionServiceImpl">
<property name="transactionDAO" ref="transactionDAO"></property>
</bean>
------解决方案--------------------路过帮顶下~
------解决方案--------------------你过不是遗留系统,你是学习的话,请使用spring2.x的事务配制方法,这个已经过时了
------解决方案--------------------XML code
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="before" class="com.aptech.aop.Before"></bean>
<bean id="after" class="com.aptech.aop.After"></bean>
<bean id="around" class="com.aptech.aop.Around"></bean>
<bean id="person" class="com.aptech.aop.Person"></bean>
<bean id="sbService" class="com.aptech.aop.Other"></bean>
<!-- 通过pointcut来指定哪些方法需要织入通知 -->
<!-- 以s开头的业务方法之前织入before通知 -->
<bean id="pointcut" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedNames">
<list>
<value>s*</value>
</list>
</property>
<property name="advice">
<ref bean="before"/>
</property>
</bean>
<!-- 配置代理工厂 spring 1.2 -->
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 目标对象 -->
<property name="target">
<ref bean="person"/>
</property>
<!-- 目标对象实现的接口 -->
<property name="proxyInterfaces">
<list>
<value>com.aptech.aop.IPerson</value>
</list>
</property>
<!-- 指定advice或者pointcut -->
<property name="interceptorNames">
<list>
<value>pointcut</value>
</list>
</property>
</bean>
<!-- 自动代理 spring 2.0 -->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>person</value> <!-- id值,不是类名 -->
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>around</value>
</list>
</property>
</bean>
</beans>