本站首页    管理页面    写新日志    退出


«January 2026»
123
45678910
11121314151617
18192021222324
25262728293031


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7694838
建立时间:2006年5月29日




[Hibernate]在filter中關閉session
软件技术

lhwork 发表于 2006/8/20 11:10:36

利用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


阅读全文(1750) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.672 second(s), page refreshed 144825572 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号