First, thank you all very much for your help, it is really useful. Unfortunately, it not over :)
Ok, so I: 1) thread-safed my application and 2) I now disconnect the Session after every method of my DAO interface.
But I get an unexpected problem:
christian wrote:
Usually, you have some methods in a DAO/persistence layer that are called in a single unit of work. An example would be: Find customer, check his account for validity, place the order. Each persistence/DAO method requires a Session, so what are your options here to deal with the Session?
Exactly, that's also my case. I have a DAO class called LazyHibernateSerializer which uses Hibernate to do stuff like, for instance,
Code:
Site getSite(long id)
.
After LazyHibernateSerializer.getSite(long) finished, the Session (inside LazyHibernateSerializer) is now disconnect()-ed. And here's the interesting part:
In my case, Site has a 1-n lazy association with Service. If I do
Code:
site.addService(Service serv)
-- which actually uses the PersistentCollection's add() method -- , I will get an exception "Session was disconnected"). And that's true because, guess what, I do disconnect the session after each access (method call) to the persistence layer.
And that seems to mean, exactly what I feared, I cannot use my lazy associations... And then, in order not to have this behaviour, I must not only keep my Session open but also connect()-ed ! (which is what I used to do and what I see now as not very intelligent thing to do...)
Can you see a better way of doing it? Can the Session somehow autoconnect() ? Do you not use the lazy collections in your object in the time between calling the DAO layer's methods ?