I have problems with the number of threads in my tomcat Server. Everytime I open a database connection through the HibernateUtil Class a new thread is started and this thread is never closed anymore. So on every click a new Thread is started and this causes Tomcat to crash after same 1000 requests and I get a outofMemoryError...
I'm opening the Connection through the database with the following code:
Code:
Session session = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
....
tx.commit();
And finally I'm closing it again:
Code:
HibernateUtil.closeSession();
Do I have forgotten something or what is wrong?
My System: RedHat Enterprise, Tomcat 5.5.9, JRE 1.4.2_10
Hibernate version:
3.1.2
My HibernateUtil CLass:
Code:
public class HibernateUtil {
private static Logger logger = Logger.getLogger(HibernateUtil.class);
private static final SessionFactory sessionFactory;
/**
* Anlegen einer Sessionfactory. Dabei wird das Hibernate.cfg.xml ausgelesen
*/
static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
logger.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
/**
* Öffnet eine neue Hibernate Session, falls noch nicht vorhanden
*
* @return Hibernate Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
/**
* Schließen der Hibernate Session
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}
[/code]