views:

30

answers:

1

I'm utilizing the Open Session in View pattern for my jsf/icesfaces application. As usual a servlet filter is opening and closing all the hibernate sessions at the beginning and the end of a "web server thread".

My problem now is that I'm using asynchronous tasks for eg. loading big lists of objects from the database. Therefore I'm creating a new thread "by hand" which executes the task.

Now my question: what's the best way to handle hibernate sessions for such async tasks? Should I manually create a session in the thread itself or is there something like a servlet filter also for threads (something which opens the session when the thread starts and closes it when it ends)?

I would be grateful for any best practives or the like. Thank you.

Here's the code which creates the thread:

protected static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5,
            15, 50, TimeUnit.SECONDS, new LinkedBlockingQueue(20));

// called by action method of a button
private void asyncLoading() {

        SessionRenderer.addCurrentSession(this.renderGroup);            

        threadPool.execute(new Thread() {   
                // do the thing to do (...)
                }
}
A: 

Generally speaking, the "Open Session in view" filters are quite simple: open a session, attach it to a ThreadLocal, close it at the end. What changes between the implementations is how to reach the Session stored in the ThreadLocal. So, depending on the "Open Session in View" parttern implementation you are using, either there is an accessible factory that is not tied to the Filter you are using, or (in almost all cases), you can peek at the source of the filter, and mimic its processing around "// do the thing to do (...)".

Damien B