Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package by.st.opt.payments.dao.util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.apache.log4j.Logger; /** * * @author Mumish */ public class HibernateUtil { private static HibernateUtil util = null; private static Logger log = Logger.getLogger(HibernateUtil.class); private SessionFactory sessionFactory = null; private final ThreadLocal sessions = new ThreadLocal(); private HibernateUtil() { try { sessionFactory = new Configuration().configure().setNamingStrategy(new CustomNamingStrategy()) .buildSessionFactory(); } catch (Throwable ex) { log.error("Initial SessionFactory creation failed." + ex); System.exit(0); } } public Session getSession() { Session session = (Session) sessions.get(); if (session == null) { session = sessionFactory.openSession(); sessions.set(session); } return session; } public static synchronized HibernateUtil getHibernateUtil() { if (util == null) { util = new HibernateUtil(); } return util; } }