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

    主站蜘蛛池模板: 久久一本岛在免费线观看2020| 免费人成大片在线观看播放电影 | 亚洲国产综合无码一区| 免费在线观看亚洲| 国产精品四虎在线观看免费 | 亚洲一区在线免费观看| 亚洲首页在线观看| 久久久久av无码免费网| 亚洲a级在线观看| 免费的一级黄色片| 色噜噜的亚洲男人的天堂| 日本中文一区二区三区亚洲| 深夜特黄a级毛片免费播放| JLZZJLZZ亚洲乱熟无码| 成全视频高清免费观看电视剧| 亚洲av无码不卡| 日韩在线免费视频| 亚洲精品乱码久久久久久V| 亚洲高清成人一区二区三区| 三年在线观看免费观看完整版中文| 亚洲AV无码一区东京热| 91成年人免费视频| 亚洲综合一区无码精品| 国产不卡免费视频| 三级黄色免费观看| 亚洲1234区乱码| mm1313亚洲精品无码又大又粗| a级毛片视频免费观看| 亚洲精品国产第1页| 日本免费一本天堂在线| 最近中文字幕免费大全| 亚洲理论片在线中文字幕| 国产视频精品免费| 国产日韩AV免费无码一区二区| 亚洲国产精品成人精品软件| 免费乱理伦在线播放| 久久国产精品萌白酱免费| 亚洲成在人线在线播放无码| 国产亚洲av片在线观看播放| 动漫黄网站免费永久在线观看| 国产偷国产偷亚洲高清在线|