<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Vincent

    Vicent's blog
    隨筆 - 74, 文章 - 0, 評論 - 5, 引用 - 0
    數據加載中……

    在filter中關閉session

    利用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
    {
    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-08-24 15:24 Binary 閱讀(228) 評論(0)  編輯  收藏 所屬分類: Hibernate

    主站蜘蛛池模板: 亚洲精品美女久久久久9999| 亚洲不卡无码av中文字幕| 亚洲AV无码一区二区乱孑伦AS| 国产亚洲综合视频| 亚洲国产天堂久久久久久| 在线观看亚洲视频| 免费人成网站在线高清| 男女交性无遮挡免费视频| 免费人成在线观看网站视频| 国产成人亚洲毛片| 亚洲视频一区二区| 中文字幕无码毛片免费看 | 午夜成人免费视频| 亚洲AV永久无码天堂影院| 日产乱码一卡二卡三免费| 在线观看亚洲专区| 亚洲中文字幕无码一区| 99爱免费观看视频在线| 亚洲伊人久久大香线焦| 日韩毛片免费在线观看| 成人免费网站视频www| 久久精品亚洲一区二区| 久久免费看黄a级毛片| 久久久国产亚洲精品| 丁香花在线视频观看免费| 亚洲AV无码一区东京热| 51在线视频免费观看视频| 精品亚洲成A人无码成A在线观看| 在线观看免费a∨网站| 一级特黄录像免费播放中文版| 人人狠狠综合久久亚洲88| 亚洲精品视频在线观看免费| 久久亚洲国产成人影院| 亚洲中文无韩国r级电影| 亚洲欧洲免费视频| 亚洲日本成本人观看| 在线观看亚洲精品福利片| 四虎最新永久免费视频| 无人视频在线观看免费播放影院 | 久久久久亚洲av无码专区喷水| 欧美a级成人网站免费|