日期:2014-05-20  浏览次数:20851 次

怎么不执行catch里的语句?根本没进去
[code=Java][/code]
//自己写的HibernateSessionFilter过滤器
public class HibernateSessionFilter implements Filter {

public void destroy() {

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
try {
chain.doFilter(request, response);
Session session = HibernateSessionUtil.getCurrentSession();
if (session != null) {
session.getTransaction().commit();//如果用到Session才提交事务
System.out.println("成功提交事务");
}
}catch (RuntimeException e) {
Session session = HibernateSessionUtil.getCurrentSession();
if (session != null) {
session.getTransaction().rollback();// 如果用到Session才回滚事务
}
System.out.println("回滚事务");
throw e;
} finally {
HibernateSessionUtil.closeAndRemoveCurrentSessionIfNotNull();
}
}

public void init(FilterConfig arg0) throws ServletException {

}

}


//Struts2的Action进行测试,看写的事务管理是否行得通

public class StudentAction {

public String add(){
StudentDao studentDao=new StudentDaoImpl();
studentDao.add(new Student());
int i=1/0;//设置一个异常
studentDao.add(new Student());
return null;
}
}

为什么两个add方法不是绑在一个线程里?并且把设置的异常放到两个add()之前也没用,还是进不到catch语句,MyEclipse里没有抛出异常

------解决方案--------------------
你没有触发异常吧,
------解决方案--------------------
RuntimeException异常大断电,估计是没有RuntimeException异常。要不就换Exception
------解决方案--------------------
RuntimeException =》 Exception