<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 閱讀(475) 評論(0)  編輯  收藏 所屬分類: Hibernate

    主站蜘蛛池模板: 香港经典a毛片免费观看看| 亚洲免费视频播放| 羞羞视频免费网站日本| 日韩a级毛片免费观看| 亚洲日韩精品无码专区| 免费无码AV片在线观看软件| 亚洲一区在线视频观看| 一二三四在线观看免费高清中文在线观看| 亚洲经典在线中文字幕| 久久国产免费福利永久| jlzzjlzz亚洲jzjzjz| 日韩吃奶摸下AA片免费观看| 亚洲中文无码av永久| 好爽…又高潮了免费毛片| 亚洲AV无码国产一区二区三区| 全部免费a级毛片| 中文永久免费观看网站| 亚洲人成电影亚洲人成9999网| 日本一卡精品视频免费| 亚洲五月综合网色九月色| 最近2019中文字幕免费看最新| 亚洲AV永久无码精品一福利 | 亚洲av专区无码观看精品天堂 | 免费人成在线视频| 99亚洲乱人伦aⅴ精品| 区久久AAA片69亚洲| 久久免费视频精品| 2020天堂在线亚洲精品专区| 天天摸夜夜摸成人免费视频| 极品美女一级毛片免费| 久久亚洲精品人成综合网| 免费精品一区二区三区在线观看| 黄色一级视频免费| 久久精品国产亚洲AV麻豆不卡| 丁香花免费完整高清观看| 人人爽人人爽人人片av免费| 久久精品a亚洲国产v高清不卡| 日本人的色道www免费一区| 中文字幕无码毛片免费看| 亚洲av无码一区二区三区观看| 免费无码又爽又刺激高潮|