<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国产成人免费观看视频| 亚洲AV日韩精品一区二区三区| 国产精品无码亚洲精品2021| 日韩成人免费aa在线看| 亚洲熟女乱色一区二区三区 | 久久综合日韩亚洲精品色| 两个人看的www高清免费视频| 国产日韩亚洲大尺度高清| 久久国产免费观看精品| 亚洲综合婷婷久久| 男女免费观看在线爽爽爽视频 | 国产精品免费精品自在线观看| 亚洲色图校园春色| 99在线视频免费观看视频 | 亚洲自国产拍揄拍| 日韩免费观看视频| 五月天国产成人AV免费观看| 亚洲精品无码乱码成人| 2020久久精品国产免费| 亚洲av乱码一区二区三区按摩| 亚洲片国产一区一级在线观看| 永久免费AV无码网站国产 | 亚洲av片在线观看| 中文字幕人成人乱码亚洲电影| 全免费a级毛片免费看| 亚洲av永久无码嘿嘿嘿| 国产乱子伦精品免费无码专区| a毛片成人免费全部播放| 337p日本欧洲亚洲大胆艺术| 国产高清免费在线| 国产做国产爱免费视频| 国产精品亚洲综合五月天| 亚洲国产精品碰碰| 亚洲一级免费毛片| 又粗又长又爽又长黄免费视频| 亚洲蜜芽在线精品一区| 又粗又大又长又爽免费视频|