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