Update of /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model/db
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8047
Added Files:
HibernateUtil.java
Log Message:
Controller of the Hibernate session
--- NEW FILE: HibernateUtil.java ---
//---------------------------------------------------------------------------------
// $Id: HibernateUtil.java,v 1.1 2005/07/28 06:50:19 jews Exp $
//
// Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net)
// Released under the Lesser GNU Public License v2.1
//---------------------------------------------------------------------------------
package net.sourceforge.bprocessor.model.db;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.apache.log4j.Logger;
/**
* Hibernate utility class
*/
public class HibernateUtil {
/** The logger */
private static Logger log = Logger.getLogger(HibernateUtil.class);
/** The singleton */
private static HibernateUtil instance;
/** The session factory */
private static SessionFactory sf;
/** The session */
private static final ThreadLocal TL = new ThreadLocal();
/**
* Constructor
*/
private HibernateUtil() {
try {
sf = new Configuration().configure().buildSessionFactory();
} catch (Exception e) {
log.fatal("Exception", e);
}
}
/**
* Get the instance
* @return The instance
*/
public static synchronized HibernateUtil getInstance() {
if (instance == null) {
instance = new HibernateUtil();
}
return instance;
}
/**
* Get the current session
* @return The session
*/
public Session currentSession() {
Session s = (Session)TL.get();
if (s == null) {
s = sf.openSession();
TL.set(s);
}
return s;
}
/**
* Close the current session
*/
public void closeSession() {
Session s = (Session)TL.get();
if (s != null) {
s.close();
}
TL.set(null);
}
}
|