zoukankan      html  css  js  c++  java
  • 一次请求在同一个事务实现

    HiberSessionFilter过滤器代码:

    [java] view plaincopyprint?
    1. package www.csdn.dbshop.filter;  
    2.   
    3. import java.io.IOException;  
    4.   
    5. import javax.servlet.Filter;  
    6. import javax.servlet.FilterChain;  
    7. import javax.servlet.FilterConfig;  
    8. import javax.servlet.ServletException;  
    9. import javax.servlet.ServletRequest;  
    10. import javax.servlet.ServletResponse;  
    11. import javax.servlet.http.HttpServletRequest;  
    12.   
    13. import org.hibernate.Transaction;  
    14.   
    15. import www.csdn.dbshop.util.HiberSessionFactory;  
    16.   
    17. public class HiberSessionFilter implements Filter {  
    18.   
    19.     public void destroy() {  
    20.   
    21.     }  
    22.   
    23.     public void doFilter(ServletRequest request, ServletResponse response,  
    24.             FilterChain chain) throws IOException, ServletException {  
    25.   
    26.         HttpServletRequest req = (HttpServletRequest) request;  
    27.         // 获取当前请求的URI路径,例如/shoping/csdn/MemberAction_query.action   
    28.         String requesturi = req.getRequestURI();  
    29.         // 截取字符串,获取‘_’之后的字符串   
    30.         String str = requesturi.substring(requesturi.indexOf("_") + 1);  
    31.         // 截取 _名字 如过以 add ,update ,delete 开始 就添加事务处理,把有过事务的处理 都删除   
    32.         // 根据获取‘_’之后的字符串,截取‘.’之前的字符串,即:就是URI中_到.之间的字符串,即:Action中的方法名   
    33.         String uri = str.substring(0, str.indexOf("."));  
    34.         // 判断截取的字符,如果是CUD则进行开启事务的操作,反之,不用开启事务   
    35.         if ("addObject".equals(uri) || "updateObject".equals(uri)  
    36.                 || "deleteObject".equals(uri) || "reg".equals(uri)  
    37.                 || "register".equals(uri) || "buy".equals(uri)) {  
    38.   
    39.             Transaction ts = null;  
    40.             try {  
    41.                 ts = HiberSessionFactory.getSession().beginTransaction();  
    42.                 chain.doFilter(request, response);  
    43.                 ts.commit();  
    44.             } catch (Exception e) {  
    45.                 if (ts != null) {  
    46.                     ts.rollback();  
    47.                 }  
    48.                 throw new RuntimeException(e);  
    49.             } finally {  
    50.                 // www.cdtarena.com关闭session   
    51.                 HiberSessionFactory.closeSession();  
    52.             }  
    53.         } else {  
    54.             try {  
    55.                 chain.doFilter(request, response);  
    56.             } catch (Exception e) {  
    57.                 throw new RuntimeException(e);  
    58.             } finally {  
    59.                 // 关闭session   
    60.                 HiberSessionFactory.closeSession();  
    61.             }  
    62.         }  
    63.     }  
    64.   
    65.     public void init(FilterConfig filterConfig) throws ServletException {  
    66.         // TODO Auto-generated method stub   
    67.   
    68.     }  
    69.   
    70. }  

    web.xml代码:

    [html] view plaincopyprint?
    1. <!-- sessionclose过滤器 -->  
    2.   <filter>  
    3.    <filter-name>HiberSessionFilter</filter-name>  
    4.    <filter-class>www.csdn.dbshop.filter.HiberSessionFilter</filter-class>  
    5.  </filter>  
    6.    
    7.  <filter-mapping>  
    8.    <filter-name>HiberSessionFilter</filter-name>  
    9.    <url-pattern>*.action</url-pattern>  
    10.  </filter-mapping>  
     

    注意:在Struts2中,过滤器最好放在Struts2过滤器之前。
    转自:http://www.cdtarena.com/javapx/201304/8394.html

  • 相关阅读:
    Objective-C面向对象-对象和类
    eImage(仅两行代码实现输出从数据库中查询到的二进制字段)标签
    yii第三方插件snoopy配置
    iOS应用崩溃日志分析 iOS应用崩溃日志揭秘
    斯坦福《机器学习》Lesson6感想———1、函数间隔和几何间隔
    openstack 中国联盟公开课參会总结
    【翻译自mos文章】多租户中的service管理
    谷歌浏览器訪问不了啦,oh shit!
    从程序员转向淘宝店主的探索
    Python爬取韩寒所有新浪博客
  • 原文地址:https://www.cnblogs.com/cdtarena/p/3040466.html
Copyright © 2011-2022 走看看