码迷,mamicode.com
首页 > Web开发 > 详细

HIbernateUtil

时间:2016-04-21 01:33:04      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

package com.car.util;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

public class HibernateUtil {

   private static final ThreadLocal<Session> sessionTL = new ThreadLocal<Session>();  

  private static Configuration configuration;    

  private final static SessionFactory sessionFactory;  

  static {  

    try {  

    configuration = new Configuration().configure();  

    sessionFactory = configuration.buildSessionFactory();  

      } catch (Exception e) {    

    e.printStackTrace();   

     throw new ExceptionInInitializerError(e);

    }

 }  

 public static Session currentSession(){

    Session session = sessionTL.get();

    if (session==null) {

       session = sessionFactory.openSession();

       sessionTL.set(session);  

   }  

   return session;

    }

   public static void closeSession(){  

      Session session = sessionTL.get();

    sessionTL.set(null);

    session.close();

   }  

}

HIbernateUtil

标签:

原文地址:http://www.cnblogs.com/Mindreader/p/5415115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!