<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-09-01 13:51 Binary 閱讀(468) 評論(0)  編輯  收藏 所屬分類: Hibernate

    主站蜘蛛池模板: 美女视频黄a视频全免费网站色 | 无码国产精品一区二区免费vr| 一区二区在线免费视频| 香蕉国产在线观看免费| 97av免费视频| 毛片免费全部免费观看| 国产免费人成在线视频| 国产av无码专区亚洲国产精品| 亚洲第一中文字幕| 亚洲精品伊人久久久久| 污污的视频在线免费观看| 在线成人精品国产区免费| 岛国大片免费在线观看| 亚洲色偷偷综合亚洲AVYP| 亚洲毛片一级带毛片基地| 美女视频黄视大全视频免费的| 免费观看四虎精品国产永久| 特级无码毛片免费视频| 久久久久亚洲av毛片大| 亚洲一区二区三区国产精华液| jzzjzz免费观看大片免费| 五月婷婷综合免费| 亚洲色婷婷综合开心网| 中国极品美軳免费观看| 成人免费毛片观看| 在线播放亚洲精品| 四虎1515hh永久久免费| 国产亚洲精品国看不卡| 免费在线看污视频| 日本三级2019在线观看免费| 久久精品国产亚洲AV蜜臀色欲 | 一级女性全黄生活片免费看| 久久综合亚洲色HEZYO国产| 久久久精品免费国产四虎| 亚洲欧洲校园自拍都市| 精品国产麻豆免费人成网站| 亚洲精品综合久久中文字幕| 日韩免费观看一级毛片看看| 亚洲综合久久成人69| 成人免费视频一区二区三区| 一级毛片免费不卡直观看|