我的applicationContext.xml配置如下:
<?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="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/bbs</value>
</property>
<property name="username" value="root" />
<property name="password" value="123" />
</bean> <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property> <property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:pojo</value>
</list>
</property>
</bean> <bean id="loginDao" class="dao.LoginDao">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!--配置事务管理器--> <bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property> </bean> <bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!--事务拦截器bean需要依赖注入一个事物管理器-->
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes"> <props>

    <prop key="del*">PROPAGATION_REQUIRED</prop>
    
<prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="search*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="remove*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="list*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="count*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property>
</bean> <!--定义BeanNameAutoproxyCreator-->
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!--指定对满足哪些bean name的bean自动生成业务事务-->
<property name="beanNames">
<!--下面是所有需要自动创建事务代理的bean-->
<list>
<value>loginDao</value>
</list>
</property>
<!--下面定义BeanNameAutoProxyCreator所需的事务拦截器--> <property name="interceptorNames">
<list>
<!--此处可以增加其它新的Interceptor-->
<value>transactionInterceptor</value>
</list>
</property>
</bean>
</beans>
Dao接口是:
public interface IDao {
public abstract void save(Object entity);
public abstract void delete(Object entity);
public abstract void update(Object entity);
public List<?> find(String queryString,Object[] value);
}
Dao是:
public class LoginDao extends HibernateDaoSupport implements IDao { public void save(Object entity) {
getHibernateTemplate().save(entity);
} public void delete(Object entity) {
getHibernateTemplate().delete(entity);
} @SuppressWarnings("unchecked")
public List<Object> find(String queryString,Object[] value) {
return (List<Object>)getHibernateTemplate().find(queryString, value);
} public void update(Object entity) {
getHibernateTemplate().update(entity);
}
}
在这个action中我想测试事务回滚;
public class Forumdelete extends ActionSupport{
public IDao loginDao;
public int id;

public IDao getLoginDao() {
return loginDao;
}
public void setLoginDao(IDao loginDao) {
this.loginDao = loginDao;
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String execute() throws Exception{
String hql1 = "from Forum fm where fm.Id=?";
Object[] value1= new Object[] {this.id};
List<Forum> i = (List<Forum>) loginDao.find(hql1, value1);
loginDao.delete(i.get(0));
String hql2="from Topic tp where tp.Id=?";
Object[] value2=new Object[]{i.get(0).getUsernametopic().iterator().next().getId()};
List<Topic> ii=(List<Topic>) loginDao.find(hql2, value2);//此处抛出异常
loginDao.delete(ii);
String hql3="from Reponse rp where tp.Topicid=?";
Object[] value3=new Object[]{ii.get(0).getId()};
List<Reponse> iii=(List<Reponse>) loginDao.find(hql3, value3);
loginDao.delete(iii);
return SUCCESS;
}}为什么不回滚?

解决方案 »

  1.   

    <prop key="del*">PROPAGATION_REQUIRED,-Exception</prop>
      

  2.   

    <value>loginDao</value>?LoginDao?名字对么?要不你写成:*DAO这不就回滚了!
      

  3.   

    你还有一个错误,就是你事务是用在dao上的。没有用在action中的,所以你在action抛异常,肯定不会回滚。
      

  4.   

    3楼说的问题,我知道了,现在我还有一个问题就是可以不可以把事务的open 和 commit放在action中来管理。
      

  5.   

    你应该加一个事务边界层,service层,或者叫做manager层。
      

  6.   

    Spring的事务我一般都配在service层(biz层),并且在这个层中不能够try如果自己try了 Spring就会认为你已经处理了异常,就会正常commit了..在食物管理层异常throws不要自己try