@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();
抛出异常后事务没有回滚,数据库中还是保存了数据了,是哪里少配置了什么吗?
解决方案 »
- 急!jsp页面遇到的一个问题。
- 问个问题,关于JAVA中类做为方法参数的问题
- struts 显示中文小问题
- struts <logic:iterate>遍历中 nested 属性显示不出来,请各位大大帮忙看看?
- jspsmartupload实现上传时的问题(关于表单问题)!!高手指点!!!
- 谁能给我一份使用JasperReport的例程?不是jsp的。 它自带的那些不够用。
- login.jsp来验证用户名和密码,TOMCAT总是报错,不知道怎么回事?
- ejb初学问题
- jboss开发ejb中,使用vector出现问题!
- 求助:runtime.exec(cmd)调用oracle8的数据库导入功能imp有问题
- 关于java&DOM生成xml文件的问题
- 关于匹配正则表达式中的元字符
就是不知道为什么加了try...catch以后就不能回滚,去掉以后可以
只有当你的操作抛出运行时异常的时候,spring才会帮你回滚
而你自己把异常给处理了,spring自然就不帮你忙了hibernate模板的一大作用就是不用自己捕获异常并处理了
spring自己帮你处理了