HibernateUtil.java

http://www.cnblogs.com/beyond_beyond/archive/2005/04/27/146435.html

 

  1.  import net.sf.hibernate.HibernateException; 
  2. import net.sf.hibernate.Session; 
  3. import net.sf.hibernate.Transaction; 
  4. import net.sf.hibernate.cfg.Configuration; 
  5. public class HibernateUtil { 
  6.      private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml"
  7.     /**//** Holds a single instance of Session */ 
  8.     private static final ThreadLocal threadLocal = new ThreadLocal(); 
  9.     private static final ThreadLocal threadTransaction = new ThreadLocal(); 
  10.     /**//** The single instance of hibernate configuration */ 
  11.     private static final Configuration cfg = new Configuration(); 
  12.     /**//** The single instance of hibernate SessionFactory */ 
  13.     private static net.sf.hibernate.SessionFactory sessionFactory; 
  14.     /**//** 
  15.      * Returns the ThreadLocal Session instance. Lazy initialize the 
  16.      * <code>SessionFactory</code> if needed. 
  17.      * 
  18.      * @return Session 
  19.      * @throws HibernateException 
  20.      */ 
  21.     public static Session getSession() throws HibernateException { 
  22.         Session session = (Session) threadLocal.get(); 
  23.         if (session == null) { 
  24.             if (sessionFactory == null) { 
  25.                 try { 
  26.                     cfg.configure(CONFIG_FILE_LOCATION); 
  27.                     sessionFactory = cfg.buildSessionFactory(); 
  28.                 } catch (Exception e) { 
  29.                     System.err.println("%%%% Error Creating SessionFactory %%%%"); 
  30.                     e.printStackTrace(); 
  31.                 } 
  32.             } 
  33.             session = sessionFactory.openSession(); 
  34.             threadLocal.set(session); 
  35.         } 
  36.         return session; 
  37.     } 
  38.     /**//** 
  39.      * Close the single hibernate session instance. 
  40.      * 
  41.      * @throws HibernateException 
  42.      */ 
  43.     public static void closeSession() throws HibernateException { 
  44.         Session session = (Session) threadLocal.get(); 
  45.         threadLocal.set(null); 
  46.         if (session != null) { 
  47.             session.close(); 
  48.         } 
  49.     } 
  50.     /**//** 
  51.      * Default constructor. 
  52.      */ 
  53.     private HibernateUtil() { 
  54.     } 
  55.     public static void beginTransaction() throws HibernateException { 
  56.         Transaction tx = (Transaction) threadTransaction.get(); 
  57.         try { 
  58.             if (tx == null) { 
  59.                 tx = getSession().beginTransaction(); 
  60.                 threadTransaction.set(tx); 
  61.             } 
  62.         } catch (HibernateException ex) { 
  63.             throw new HibernateException(ex.toString()); 
  64.         } 
  65.     } 
  66.     public static void commitTransaction() throws HibernateException { 
  67.         Transaction tx = (Transaction) threadTransaction.get(); 
  68.         try { 
  69.             if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) 
  70.                 tx.commit(); 
  71.             threadTransaction.set(null); 
  72.         } catch (HibernateException ex) { 
  73.             rollbackTransaction(); 
  74.             throw new HibernateException(ex.toString()); 
  75.         } 
  76.     } 
  77.     public static void rollbackTransaction() throws HibernateException { 
  78.         Transaction tx = (Transaction) threadTransaction.get(); 
  79.         try { 
  80.             threadTransaction.set(null); 
  81.             if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) { 
  82.                 tx.rollback(); 
  83.             } 
  84.         } catch (HibernateException ex) { 
  85.             throw new HibernateException(ex.toString()); 
  86.         } finally { 
  87.             closeSession(); 
  88.         } 
  89.     } 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值