首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

请大家分析一下这个HibernateUtil有什么有关问题

2011-11-20 
请大家分析一下这个HibernateUtil有什么问题?packagecom.studorm.daoimportnet.sf.hibernate.HibernateEx

请大家分析一下这个HibernateUtil有什么问题?
package   com.studorm.dao;

import   net.sf.hibernate.HibernateException;  
import   net.sf.hibernate.Session;  
import   net.sf.hibernate.Transaction;  
import   net.sf.hibernate.cfg.Configuration;  
 
public   class   HibernateUtil   {  
 
          private   static   String   CONFIG_FILE_LOCATION   =   "/hibernate.cfg.xml ";  
 
        /**//**   Holds   a   single   instance   of   Session   */  
        private   static   final   ThreadLocal   threadLocal   =   new   ThreadLocal();  
 
        private   static   final   ThreadLocal   threadTransaction   =   new   ThreadLocal();  
 
        /**//**   The   single   instance   of   hibernate   configuration   */  
        private   static   final   Configuration   cfg   =   new   Configuration();  
 
        /**//**   The   single   instance   of   hibernate   SessionFactory   */  
        private   static   net.sf.hibernate.SessionFactory   sessionFactory;  
 
        /**//**  
          *   Returns   the   ThreadLocal   Session   instance.   Lazy   initialize   the  
          *   <code> SessionFactory </code>   if   needed.  
          *  
          *   @return   Session  
          *   @throws   HibernateException  
          */  
        public   static   Session   getSession()   throws   HibernateException   {  
                Session   session   =   (Session)   threadLocal.get();  
 
                if   (session   ==   null)   {  
                        if   (sessionFactory   ==   null)   {  
                                try   {  
                                        cfg.configure(CONFIG_FILE_LOCATION);  
                                        sessionFactory   =   cfg.buildSessionFactory();  
                                }   catch   (Exception   e)   {  
                                        System.err.println( "%%%%   Error   Creating   SessionFactory   %%%% ");  
                                        e.printStackTrace();  


                                }  
                        }  
                        session   =   sessionFactory.openSession();  
                        threadLocal.set(session);  
                }  
                return   session;  
        }  
 
        /**//**  
          *   Close   the   single   hibernate   session   instance.  
          *  
          *   @throws   HibernateException  
          */  
        public   static   void   closeSession()   throws   HibernateException   {  
                Session   session   =   (Session)   threadLocal.get();  
                threadLocal.set(null);  
                if   (session   !=   null)   {  
                        session.close();  
                }  
        }  
 
        /**//**  
          *   Default   constructor.  
          */  
        private   HibernateUtil()   {  
        }  
 
        public   static   void   beginTransaction()   throws   HibernateException   {  
                Transaction   tx   =   (Transaction)   threadTransaction.get();  
                try   {  
                        if   (tx   ==   null)   {  
                                tx   =   getSession().beginTransaction();  
                                threadTransaction.set(tx);  
                        }  
                }   catch   (HibernateException   ex)   {  
                        throw   new   HibernateException(ex.toString());  
                }  
        }  
 
        public   static   void   commitTransaction()   throws   HibernateException   {  
                Transaction   tx   =   (Transaction)   threadTransaction.get();  


                try   {  
                        if   (tx   !=   null   &&   !tx.wasCommitted()   &&   !tx.wasRolledBack())  
                                tx.commit();  
                        threadTransaction.set(null);  
                }   catch   (HibernateException   ex)   {  
                        rollbackTransaction();  
                        throw   new   HibernateException(ex.toString());  
                }  
        }  
 
        public   static   void   rollbackTransaction()   throws   HibernateException   {  
                Transaction   tx   =   (Transaction)   threadTransaction.get();  
                try   {  
                        threadTransaction.set(null);  
                        if   (tx   !=   null   &&   !tx.wasCommitted()   &&   !tx.wasRolledBack())   {  
                                tx.rollback();  
                        }  
                }   catch   (HibernateException   ex)   {  
                        throw   new   HibernateException(ex.toString());  
                }   finally   {  
                        closeSession();  
                }  
        }  
}

[解决办法]
请大家分析分析一下!

热点排行