@Transactional
public class Dao implements DaoIn{
private HibernateTemplate template;
public void save(Student s){
try{
template.save(s);
throw new RuntimeException();
} catch(RuntimeException e) {
e.printStackTrace();

}
public HibernateTemplate getTemplate() {
return template;
}
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
}
Actionpublic class SpringAction extends ActionSupport{
private Student stu = new Student();
private DaoIn dao;
public String execute()throws Exception{
stu.setName("111");
stu.setStuclassid(8);
dao.save(stu);
//this.wait();
return SUCCESS;
}//省略get,set
applicationContext.xml<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean><bean id="SpringAction" class="test.web.SpringStruts2Hibernate.Action.SpringAction"
scope="prototype">
<property name="dao" ref="dao" />
</bean> <bean id="dao" class="test.web.SpringStruts2Hibernate.DAOImpl.Dao">
<property name="template" ref="hibernateTemplate" />
</bean>
<!--省略数据源的配置信息-->
为什么throw new RuntimeException();
抛出异常后事务没有回滚,数据库中还是保存了数据了,是哪里少配置了什么吗?

解决方案 »

  1.   

    去掉try...catch就可以回滚了,难道异常不需要捕获?
      

  2.   

    哎,可惜没人回答啊,我自己搞定了,用XML,注解都试过了,可以回滚了。
    就是不知道为什么加了try...catch以后就不能回滚,去掉以后可以
      

  3.   

    用hibernate模板的时候是不用捕获异常的
    只有当你的操作抛出运行时异常的时候,spring才会帮你回滚
    而你自己把异常给处理了,spring自然就不帮你忙了hibernate模板的一大作用就是不用自己捕获异常并处理了
    spring自己帮你处理了