package de.tgunkel.de.hibernate3;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class ManagerHibernateSession
{

 private static final SessionFactory sessionFactory;
 public static final ThreadLocal session = new ThreadLocal();

 static
 {
  try
  {
   sessionFactory = new Configuration().configure().buildSessionFactory();
  }
  catch (Throwable e)
  {
   throw new ExceptionInInitializerError(e);
  }
 }

 public static Session currentSession() throws HibernateException
 {
  Session s = (Session) session.get();
  if (s == null)
  {
   s = sessionFactory.openSession();
   session.set(s);
  }
  return s;
 }

 public static void closeSession() throws HibernateException
 {
  Session s = (Session) session.get();
  session.set(null);
  if (s != null) s.close();
 }
  
 public static void hsqlCleanup(Session s)
 {
  try { s.connection().createStatement().execute("SHUTDOWN"); }
  catch (Exception e)
  {
   e.printStackTrace();
  }
 }

}
