为什么我用过滤器管理事务根本不回滚?

颖辉小居 2015-11-09 02:29:05
事务过滤器
package yh.position.Filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import yh.position.util.HibernateUtils;

/**
* 用于进行Hibernate事务处理的Servlet过滤器
*
* @author zyh 2015-10-21
*/
public class TransactionFilter implements Filter {
private static Logger log = Logger.getLogger(TransactionFilter.class);

/**
* 过滤器的主要方法 用于实现Hibernate事务的开始和提交
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

Session session = HibernateUtils.getSession();
Transaction tx = session.getTransaction();
if (!tx.isInitiator()) {
tx.begin();
}
System.out.println("----开启事物----");
try {
// 开始一个新的事务
log.info("Starting a database transaction");
// tx.begin();不注释就报错:nested transactions not supported
log.info("Request Path:\t"
+ ((HttpServletRequest) request).getServletPath());

// Call the next filter (continue request processing)
chain.doFilter(request, response);

// 提交事务
log.info("Committing the database transaction");
if (tx.isInitiator()) {
tx.commit();

}

System.out.println("----事物提交----");
} catch (Throwable ex) {
ex.printStackTrace();
try {
// 回滚事务
log.info("Trying to rollback database transaction after exception");
if (tx.isInitiator()) {
tx.rollback();
System.out.println("----回滚事务----");
}
} catch (Throwable rbEx) {
log.error("Could not rollback transaction after exception!",
rbEx);
}
// 抛出异常
throw new ServletException(ex);
} finally {
log.info("session closed");
HibernateUtils.closeSession();
}
}

/**
* Servlet过滤器的初始化方法 可以读取配置文件中设置的配置参数
*/
public void init(FilterConfig filterConfig) throws ServletException {
}

/**
* Servlet的销毁方法 用于释放过滤器所申请的资源
*/
public void destroy() {
}
}

Action
package yh.position.action.police;

import yh.position.action.BaseAction;
import yh.position.dao.impl.PoliceDaoImpl;
import yh.position.model.Police;

public class PoliceUpdatePwdAction extends BaseAction {

private static final long serialVersionUID = 1L;
private String newPwd;

public String getNewPwd() {
return newPwd;
}

public void setNewPwd(String newPwd) {
this.newPwd = newPwd;
}

@Override
public String execute() throws Exception {
int policeId = currPoliceId();

Police police = new PoliceDaoImpl().get(policeId);
police.setPwd(newPwd);
new PoliceDaoImpl().save(police);
code = 1;
message = "修改成功!";

int err = 1 / 0;

returnJson();
return SUCCESS;
}
}

结果jsp上显示了 除数为0的错误。可是控制台打印了事务的开启和提交,修改的数据也保存到数据库了。没有回滚的打印。
...全文
162 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
颖辉小居 2015-11-10
  • 打赏
  • 举报
回复
web的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name></display-name>
  <!-- 编码过滤器 -->
  <filter>
    <filter-name>charsetFilter</filter-name>
    <filter-class>yh.position.Filter.CharsetFilter</filter-class>
    <init-param>
      <param-name>codingType</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>charsetFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!--登陆过滤器 -->
  <filter>       
         <filter-name>loginFilter</filter-name>       
         <filter-class>yh.position.Filter.LoginFilter</filter-class>       
    </filter>       
      <filter-mapping>    
         <filter-name>loginFilter</filter-name>    
         <url-pattern>/*</url-pattern>    
  </filter-mapping> 
  <!-- 事务过滤器  --> 
  <filter>
    <description>事务的过滤器</description>
    <filter-name>TransactionFilter</filter-name>
    <filter-class>yh.position.Filter.TransactionFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>TransactionFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- session监听器 -->
  <listener>
    <listener-class>yh.position.listener.OnlineUserListener</listener-class>
  </listener>
    <!-- struts2过滤器 -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
颖辉小居 2015-11-09
  • 打赏
  • 举报
回复
自己顶,Spring 对hibernate4 好像不再提供事务支持了。
颖辉小居 2015-11-09
  • 打赏
  • 举报
回复
求求大家 帮帮忙啊,我用的是hibernate4 ,不会用spring管理他的事务

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧