Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
2.1.6
Full stack trace of any exception that occurs:
None
I integrated the HibernateFilter from the Hibernate 2 version of Caveat Emptor into our application and came across a strange issue with multithreading (I have searched the forum for similar problems/fixes and not seen any)
I have a consumer (implements Runnable) that does database access/updating). This is managed by a singleton that is created when needed. Our web application is written using Struts and things to process may be added to the consumer through the interface. The issue I get since trying out the HibernateFilter is that Tomcat is unable to handle any requests (at all) while the consumer thread is processing. Originally, if you kicked off a consumer request Tomcat would hang when you try to hit it with another request but I was able to get it to the state it's in now by committing the transaction and closing the session.
Threads aren't really my forte, so if I've made some kind of schoolboy error please be gentle. I thought that, surely, since the consumer is running it its own thread it would have its own Session and Transaction. Does anyone have any idea why it would be causing Tomcat to hang until the Session/Transaction are closed? I kick off the consumer like this:
Code:
consumer = new Consumer();
consumerThread = new Thread(consumer);
consumerThread.setDaemon(true);
consumerThread.start();
This is a very sanitised version of my Consumer:
Code:
public class Consumer implements Runnable {
private Queue queue;
public Consumer() {
queue = new LinkedList();
}
public void addRequest(Request request) {
synchronized (queue) {
queue.add(request);
queue.notify();
}
}
public void remove(Request request) {
synchronized (queue) {
queue.remove(request);
queue.notify();
}
}
public void run() {
try {
while (true) {
synchronized (queue) {
while (queue.isEmpty()) {
queue.wait();
}
}
request = (Request) queue.poll();
try {
// do some hibernate stuff
//
} catch (Exception e) {
// handle the error
//
} finally {
try {
HibernateSessionFactory.commitTransaction();
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
// this has been logged already
//
}
}
}
} catch (InterruptedException e) {
logger.warn(e);
}
}
}