Vincent

          Vicent's blog
          隨筆 - 74, 文章 - 0, 評(píng)論 - 5, 引用 - 0
          數(shù)據(jù)加載中……

          在filter中關(guān)閉session

          利用Thread-Specific Storage撰寫一個(gè)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
          {
          publicstaticfinal ThreadLocal tLocalsess = new ThreadLocal();

          publicstaticfinal ThreadLocal tLocaltx = new ThreadLocal();

          /*
          * getting the thread-safe session for using
          */
          publicstatic 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){
          thrownew InfrastructureException(e);
          }
          return session;
          }

          /*
          * closing the thread-safe session
          */
          publicstatic void closeSession(){

          Session session = (Session) tLocalsess.get();
          tLocalsess.set(null);
          try{
          if (session != null && session.isOpen()){
          session.close();
          }

          }catch (HibernateException e){
          thrownew InfrastructureException(e);
          }
          }

          /*
          * begin the transaction
          */
          publicstatic void beginTransaction(){
          Transaction tx = (Transaction) tLocaltx.get();
          try{
          if (tx == null){
          tx = currentSession().beginTransaction();
          tLocaltx.set(tx);
          }
          }catch (HibernateException e){
          thrownew InfrastructureException(e);
          }
          }

          /*
          * close the transaction
          */
          publicstatic void commitTransaction(){
          Transaction tx = (Transaction) tLocaltx.get();
          try{
          if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
          tx.commit();
          tLocaltx.set(null);
          }catch (HibernateException e){
          thrownew InfrastructureException(e);
          }
          }

          /*
          * for rollbacking
          */
          publicstatic void rollbackTransaction(){
          Transaction tx = (Transaction) tLocaltx.get();
          try{
          tLocaltx.set(null);
          if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()){
          tx.rollback();
          }
          }catch (HibernateException e){
          thrownew InfrastructureException(e);
          }
          }

          privatestatic Session openSession() throws HibernateException{
          return getSessionFactory().openSession();
          }

          privatestatic 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

          posted on 2006-09-01 13:51 Binary 閱讀(470) 評(píng)論(0)  編輯  收藏 所屬分類: Hibernate

          主站蜘蛛池模板: 玛曲县| 甘肃省| 玉山县| 石屏县| 始兴县| 施甸县| 高陵县| 长白| 吴旗县| 青龙| 镇宁| 兴宁市| 巴中市| 黄浦区| 石家庄市| 工布江达县| 铜川市| 南江县| 浦江县| 太谷县| 新安县| 顺平县| 安宁市| 高雄市| 泰顺县| 读书| 望奎县| 武冈市| 秦安县| 铁岭市| 万安县| 镇远县| 大渡口区| 隆子县| 金秀| 呼图壁县| 海口市| 宿州市| 宽城| 定陶县| 昌乐县|