gavin wrote:
This code is okay, but what is missing is that you should be closing the session at the end of each application transaction. As it stands, you have a memory leak, and a cache that gets more and more stale as time passes.
I thought that I understood what you meant by "application transaction" and that in the realm of web applications that meant "a user request" (begin to end of servicing of a request).
Is this incorrect?
And when you say "you should be closing the session at the end of each AT", do you mean that I should be calling session.close() instead of session.disconnect() at the end of servicing the user request (which should equal an AT, if I understand you correctly)?
gavin wrote:
Ummm. One more problem:
it doesn't look like the session is being allocated to a particular user! looks as if the session gets allocated to a pooled thread! That is terrible, if correct.
Regarding user assignment - I am not assigning the Session to a user anywhere explicitly.
I thought using ThreadLocal would automatically provide that:
Code:
Serlvet has e.g. 10 threads
User A makes a request and gets Thread T1
T1 has no Session associated with it, so I create a new Session and associate it with T1
User A completes request, so I call session.disconnect(), but leave Session associated w/ T1
User B makes a request and gets T1, which is now available
T1 already has a Session associated with, but it is disconnected, so I call session.reconnect()
User B completes request, so I call session.disconnect(), but leave Session associated w/ T1
Is this wrong?
That is, can multiple users share the same Session instance if their access to it is sequential (in this example user B could not get Session in T1 thread before user A was done with T1 and before his session was disconnect()ed)
If this is wrong, then are you saying that all I need is something like:
Code:
private static Map userToSessionMap = new HashMap();
where keys are userIds, for example, and values are Hibernate Sessions.
And then just provide appropriate accessors to this to get and remove Sessions?
In that case, is ThreadLocal in the servlet environment a bad idea?
gavin wrote:
(Also I don't see where you flush the session.)
Regarding flushing the Session - some of my DAO methods call session.flush() explicitly, but most do not. Most are in transactions (beginTransaction + commit or rollback).
Do I need to add explicit, manual session.flush() calls every time I do update(), save(), saveOrUpdate()?
Thanks for such speedy help!
Otis