在filter中關閉session
来源:互联网 发布:网络言论自由案例2017 编辑:程序博客网 时间:2024/04/19 20:57
原文:http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=805
利用Thread-Specific Storage撰寫一個HibernateUtil
HibernateSessionUtil.java
import java.io.Serializable;import net.sf.hibernate.HibernateException;import net.sf.hibernate.Session;import net.sf.hibernate.SessionFactory;import net.sf.hibernate.Transaction;public class HibernateSessionUtil implements Serializable{ public static final ThreadLocal tLocalsess = new ThreadLocal(); public static final ThreadLocal tLocaltx = new ThreadLocal(); /* * getting the thread-safe session for using */ public static Session currentSession(){ Session session = (Session) tLocalsess.get(); //open a new one, if none can be found. try{ if (session == null){ session = openSession(); tLocalsess.set(session); } }catch (HibernateException e){ throw new InfrastructureException(e); } return session; } /* * closing the thread-safe session */ public static void closeSession(){ Session session = (Session) tLocalsess.get(); tLocalsess.set(null); try{ if (session != null && session.isOpen()){ session.close(); } }catch (HibernateException e){ throw new InfrastructureException(e); } } /* * begin the transaction */ public static void beginTransaction(){ Transaction tx = (Transaction) tLocaltx.get(); try{ if (tx == null){ tx = currentSession().beginTransaction(); tLocaltx.set(tx); } }catch (HibernateException e){ throw new InfrastructureException(e); } } /* * close the transaction */ public static void commitTransaction(){ Transaction tx = (Transaction) tLocaltx.get(); try{ if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) tx.commit(); tLocaltx.set(null); }catch (HibernateException e){ throw new InfrastructureException(e); } } /* * for rollbacking */ public static void rollbackTransaction(){ Transaction tx = (Transaction) tLocaltx.get(); try{ tLocaltx.set(null); if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()){ tx.rollback(); } }catch (HibernateException e){ throw new InfrastructureException(e); } } private static Session openSession() throws HibernateException{ return getSessionFactory().openSession(); } private static SessionFactory getSessionFactory() throws HibernateException{ return SingletonSessionFactory.getInstance(); }}
filter中的程式碼如下
HibernateSessionCloser.java
public class HibernateSessionCloser implements Filter{ protected FilterConfig filterConfig = null; public void init(FilterConfig filterConfig)throws ServletException{this.filterConfig = filterConfig; } public void destroy(){ this.filterConfig = null; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException { try{ chain.doFilter(request, response); } finally{ try{ HibernateSessionUtil.commitTransaction(); }catch (InfrastructureException e){ HibernateSessionUtil.rollbackTransaction(); }finally{ HibernateSessionUtil.closeSession(); } } }}
然後在操作資料庫之前加上
HibernateSessionUtil.beginTransaction();HibernateSessionUtil.currentSession();//取得Session
- 在filter中關閉session
- 在filter里关闭session
- 在filter中关闭session
- session Filter
- User Session Filter实现
- 9.13 session,filter
- grails session 验证 filter
- Struts与Hibernate整合将Session和Transaction放在Filter中
- 如何在Service层获取用户session中保存的用户信息的方法(Filter+ThreadLocal)
- Open-Session-In-View-Filter
- 在MyEclipse创建Filter
- 用filter 实现当session失效
- Filter对权限和session的控制。
- Session超时Filter控制页面调转
- servlet,filter,sessionListener,cookie,session操作
- struts 中filter使用控制session
- servlet,filter,sessionListener,cookie,session操作
- HPROSE for java 过滤器 filter + 重写session
- 溢出专题……缓冲区溢出原理
- 程序和算法的真谛
- Exceptional C++ Style之15
- scanline
- winRAR做开机密码
- 在filter中關閉session
- 电脑主板的相关知识
- 不要哀叹
- 不知道何去何从!
- MCSE on Microsoft Windows Server 2003 Certification Requirements
- 跟我一起写 Makefile
- 女人如茶
- 原来JScript中的关键字'var'还是有文章的 (zt)
- Java Web应用产品安装程序制作方案