org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
Using: Hibernate 3.1Beta
JDK: 1.5.0_04
I have a situation where I believe Hibernate is incorrectly retaining infomation for a closed session on objects loaded from new session instances.
I have a replication requirement whereby I have a "master" session, and several "slave" session configurations. At regular intervals, a scheduled task opens the "master" session and gets a list of replication tasks. Each task corresponds to a CREATE, UPDATE or DELETE against a "slave" session. Hence, I need to keep the master session open whilst opening and closing slave session.
The problem manifests in the following situation:
Consider 2 Objects, A and B.
Object A contains an instance of Object B
The replication system needs to update BOTH of these objects, as two separate steps. (That is, update Object A only, then update Object B only; no cascading)
The replication task runs as follows
Using (already opened) master system, load Object A
Evict Object A from master session
Open slave session
Save Object A against slave session
Close slave session
Using (already opened) master system, load Object B
Evict Object B from master session
Open slave session
Save Object B against slave session <-- ERROR HERE
Close slave session
Debugging, it seems the following is occurring:
When Object B is loaded, the nested session inside the org.hibernate.proxy.CGLIBLazyInitializer is the SAME INSTANCE as the session used to load Object A, which was closed.
So, it seems that even though I am closing the session and loading Object B with a newly opened session instance, the LazyInitializer is retaining the information from the earlier session in which Object A was loaded. I can only assume that Object B has this "old" session as the nested session in the CGLIBLazyInitializer because it was once loaded using this session instance when Object A was originally loaded.
Is there a way I can tell Hibernate to completely abandon the older closed session, or alternatively tell it that Object B should disregard it's earlier incarnation and use the current (open) session?
Thanks.
|