<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

    主站蜘蛛池模板: 最近中文字幕免费mv在线视频| 国产成人免费网站| 亚洲黄色在线观看网站| ww在线观视频免费观看| 亚洲七久久之综合七久久| 国产成人亚洲综合无码| 巨波霸乳在线永久免费视频| 看亚洲a级一级毛片| 亚洲精品无码不卡| 国产日产成人免费视频在线观看| a级男女仿爱免费视频| 亚洲综合一区无码精品| 中文字幕亚洲乱码熟女一区二区| 亚洲视频在线免费观看| 偷自拍亚洲视频在线观看| 亚洲国产精品不卡在线电影| 国产美女精品视频免费观看| 野花香高清视频在线观看免费| 亚洲成人高清在线观看| 亚洲精品一级无码中文字幕| 日韩免费一区二区三区在线播放| 一二三四在线观看免费中文在线观看| 亚洲国产精品专区| 亚洲日本va中文字幕久久| 免费av欧美国产在钱| 成在线人免费无码高潮喷水| 亚洲熟妇久久精品| 少妇中文字幕乱码亚洲影视| 亚洲国产专区一区| 午夜无遮挡羞羞漫画免费| 免费观看91视频| 日本精品久久久久久久久免费 | 国产在线精品一区免费香蕉| 亚洲乱理伦片在线观看中字| 日木av无码专区亚洲av毛片| 色噜噜亚洲精品中文字幕| 成人毛片18女人毛片免费视频未 | 国产男女猛烈无遮挡免费网站| 国产精品成人观看视频免费| 91视频免费网站| 一区二区三区免费高清视频|