<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, 評(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-08-24 15:24 Binary 閱讀(232) 評(píng)論(0)  編輯  收藏 所屬分類: Hibernate

    主站蜘蛛池模板: 亚洲a∨无码精品色午夜| 亚洲国产精品久久久久婷婷老年 | 四虎影视www四虎免费| 我的小后妈韩剧在线看免费高清版| 曰批视频免费40分钟试看天天| 亚洲伊人久久综合影院| 青娱乐在线视频免费观看| 特级aaaaaaaaa毛片免费视频| 四虎成人免费网址在线| 亚洲国产成人综合精品| 最好免费观看韩国+日本| 精品国产免费一区二区| 亚洲欧美成人一区二区三区| fc2免费人成为视频| 亚洲欧洲国产成人综合在线观看 | 自拍偷自拍亚洲精品情侣| 亚洲最新黄色网址| 久久久久av无码免费网| 亚洲毛片免费观看| 一级做a爰性色毛片免费| 国产亚洲精品成人AA片新蒲金| 国产一级黄片儿免费看| 亚洲另类激情综合偷自拍| 四虎1515hh永久久免费| 亚洲日韩国产欧美一区二区三区| 宅男666在线永久免费观看| 一级一级一片免费高清| 亚洲成人激情在线| 男人天堂免费视频| 国产精品国产自线拍免费软件| 爱情岛论坛免费视频| 亚洲精品美女久久久久99| 久久免费看黄a级毛片| 亚洲日韩精品国产一区二区三区| 四虎影视永久免费观看| 中文字幕免费不卡二区| 亚洲欧洲日本国产| 四虎影视免费永久在线观看| 日本视频在线观看永久免费| 亚洲熟女www一区二区三区| www.黄色免费网站|