Hello, using version 2.1.8 I encounter the following problem, which I think it might be a bug:
Code:
public void testSessionEvict() throws Exception {
Session s1 = sessionFactory.openSession();
Session s2 = sessionFactory.openSession();
TargetPortfolio tp = (TargetPortfolio)s1.load(TargetPortfolio.class, new Long(1));
s1.clear();
s2.lock(tp, LockMode.NONE);
I tried with s1.clear() and s1.evict(tp), same result.
If s1.close() is called there is no problem.
The s2.lock() results in:
net.sf.hibernate.LazyInitializationException: Illegally attempted to associate a proxy with two open Sessions
at net.sf.hibernate.proxy.LazyInitializer.setSession(LazyInitializer.java:152)
at net.sf.hibernate.impl.SessionImpl.reassociateProxy(SessionImpl.java:1027)
at net.sf.hibernate.impl.SessionImpl.unproxyAndReassociate(SessionImpl.java:1011)
at net.sf.hibernate.impl.SessionImpl.lock(SessionImpl.java:1709)
Looking in LazyInitializer.setSession() I see:
if ( session!=null && session.isOpen() ) {
//TODO: perhaps this should be some other RuntimeException...
throw new LazyInitializationException("Illegally attempted to associate a proxy with two open Sessions");
Indeed s1 is still open and the session field of the evicted object is not cleared, even when it is detached from s1.
According to the "Hibernate in action" book on page 116 section 4.1.1 an object is detached by session close/clear/evict, no difference is made between these. Now it seems that this is not fully the case.
Should not the object(s) evicted/cleared from the session have their session field put to null?
B.t.w. before people ask why we use multiple sessions: in reality we use multiple threads, a thread is getting objects and passes them to worker threads. Since session is not threadsafe we have to use a different session for each thread. We might constantly close the session in the first thread and then reopen a new one, but I'd rather not.
Regards,
Peter
|