/**
* edu.princeton.wordnet.pojos.app
* SessionFactoryUtil.java
*
* This class garanties that only one single SessionFactory
* is instantiated and that the configuration is done thread safe as
* singleton. Actually it only wraps the Hibernate SessionFactory.
* You are free to use any kind of JTA or Thread transactionFactories.
*
* @author hennebrueder
* @author Bernard Bou
*/
package edu.princeton.wordnet.pojos.app;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
/**
* @author bbou
*/
public class SessionFactoryUtil
{
/**
* The single instance of hibernate SessionFactory
*/
private static org.hibernate.SessionFactory sessionFactory;
/**
* Disable contructor to guaranty a single instance
*/
private SessionFactoryUtil()
{
// do nothing
}
static
{
// Annotation and XML
SessionFactoryUtil.sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
// XML only
// sessionFactory = new Configuration().configure().buildSessionFactory();
}
public static SessionFactory getInstance()
{
return SessionFactoryUtil.sessionFactory;
}
/**
* Returns a session from the session context. If there is no session in the context it opens a session, stores it in the context and returns it. This
* factory is intended to be used with a hibernate.cfg.xml including the following property thread
* This would return the current open session or if this does not exist, will create a new session
*
* @return the session
*/
public Session getCurrentSession()
{
return SessionFactoryUtil.sessionFactory.getCurrentSession();
}
/**
* closes the session factory
*/
public static void close()
{
if (SessionFactoryUtil.sessionFactory != null)
{
SessionFactoryUtil.sessionFactory.close();
}
SessionFactoryUtil.sessionFactory = null;
}
}