-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 posts ] 
Author Message
 Post subject: Error : no session or session was closed
PostPosted: Thu May 03, 2007 6:31 pm 
Newbie

Joined: Thu May 03, 2007 6:24 pm
Posts: 14
Hello,
I'm using JPA/Hibernate version: 3.2 in my project. Recently I'm seeing following error when Collection of object is received(which is lazily initialized).Though Entity manager is NOT closed at that point of code, I'm surprised/confused on how this error is thrown. More over this error is not consistent. any help would be highly appreciated. Thanks.

no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:249)


Last edited by techluver on Fri May 04, 2007 3:24 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 10:30 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Quote:
Though Entity manager is closed at that point of code, I'm surprised/confused on how this error is thrown


? It can hardly be a surprise that if you close the persistence context, you get an exception saying "it is closed" when you try to use it again (illegally)? You can't continue loading stuff from the database on-demand once the persistence context is closed. Read http://hibernate.org/43.html or really anything about lazy loading/persistence context handling.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: wrong statement
PostPosted: Fri May 04, 2007 11:51 am 
Newbie

Joined: Thu May 03, 2007 6:24 pm
Posts: 14
Thanks christian for your reply.

I'm sorry. I missed "NOT" in my first thread.
It should have been: Though Entity manager is NOT closed at that point of code, I'm surprised/confused on how this error is thrown. Could you please help ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 04, 2007 2:42 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
It's very unlikely that there is a bug in this area, but if you can reproduce the issue in a simple minimal test case, post it to JIRA.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 08, 2007 12:45 pm 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
emmanuel wrote:
It's very unlikely that there is a bug in this area, but if you can reproduce the issue in a simple minimal test case, post it to JIRA.

Hi,
I am seeing something similar, I clear the persistence context and then invoke a size operation on a 1M relationship:

Code:
em.clear();
company.getEmployees().size();


I get:

com.fineos.ta3g.test.jpatests.JPASqlAnalysis.test_1Morg.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.fineos.ta3g.test.garage.Company.employees, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97)
at org.hibernate.collection.PersistentBag.size(PersistentBag.java:225)
at com.fineos.ta3g.test.jpatests.JPASqlAnalysis.test_1M(JPASqlAnalysis.java:111)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 08, 2007 4:43 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
... which is expected, since your object is detached (ie not associated to a session)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 08, 2007 4:47 pm 
Newbie

Joined: Thu May 03, 2007 6:24 pm
Posts: 14
Thanks! Yes. I was calling em.clear() which detaches all the object.
I'm using hibernate annotations. I know that we have session.evict(object) to clear particular object. Do we have same kind of function in entitymanager? if not , Then I should use
org.hibernate.Session session = ((org.hibernate.ejb.HibernateEntityManager)em).getSession();
session.evict(object);

Also I want to know how I can do cascade evict in annotations.

Thanks again!


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 09, 2007 5:40 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
emmanuel wrote:
... which is expected, since your object is detached (ie not associated to a session)

Interesting and thanks for that.
If it's evicted from the session, it's surely then just a normal API (i.e. non JPA) being invoked on a Java object.
I would have thought it should have just returned something like it does when the object is transient and not throw a hibernate exception.

Not sure what other people think, but the behaviour seems counter intuitive to me.

Is this behaviour standard JPA or is it something Hibernate adds on?

Many many thanks
Breako


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 11, 2007 5:32 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Just an update on this issue, I didn't see this behaviour when using
an older version of the Entity Manager.
I did not see this behaviour when I was using:

Annotations: Version: 3.2.0 CR1
Core: version 3.2 cr2
EntityManager: Version: 3.2.0.CR1

but do see it when using the following versions:

Annotations: Version: 3.3.0.GA
Core: version 3.2.3 GA
EntityManager: 3.3.1.GA

As it is not clear what version if I am on the same versions as techluver
I might take this in a separate post.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 11, 2007 10:40 am 
Beginner
Beginner

Joined: Mon Jan 09, 2006 3:05 pm
Posts: 24
Section 3.2.4 of the spec explains what state is still available in a detached entity. About associations that a detached entity has, it says:
Quote:
If the persistent field or property is an association, the available state of an associated instance may only
be safely accessed if the associated instance is available.

So if the associated instance isn't available, it is *unsafe* to access it, and an error like you are getting is reasonable.
--keenan


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 14, 2007 11:58 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
your transient object is different, it has no association with lazy objects usually

The behavior has always been the same but the object loading might have changed, there were some bugs in previous HEM versions that forced unneeded initializations (having the object eagerly loaded where lazy was expected).

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 05, 2009 11:12 pm 
Newbie

Joined: Thu Mar 05, 2009 11:03 pm
Posts: 2
breako wrote:
Hi,
I am seeing something similar, I clear the persistence context and then invoke a size operation on a 1M relationship:

Code:
em.clear();
company.getEmployees().size();




Sorry to drag up this old topic... but it is probably relevant for people searching google on this issue.

In the case being discussed by breako, does anyone else think that the error message is a bit misleading?

Code:
no session or session was closed


After all, the session still exists and is still open, the problem is that the object is not in the hibernate cache any more.


Top
 Profile  
 
 Post subject: Re: Error : no session or session was closed
PostPosted: Mon Sep 06, 2010 9:41 am 
Newbie

Joined: Mon Sep 06, 2010 9:31 am
Posts: 1
sorry for rise the thread again. I am having a similiar problem.

I have a User object that has many Houses. The fetch type is LAZY
This is how I reproduce the issue:

1. Open Session
2. Fetch User object
3. Close the Session.
4. Make some operations with the User object (and now, I want the user's houses, so I call user.getHouses():
5. Open a new session
6. Call user.getHouses()

then I got failed to lazily initialize a collection of role: User.houses, no session or session was closed

Already tried those, with no sucess:
1. call session.refresh(user) before call user.getHouses()
2. open a transaction before user.getHouses()
3. call session.lock(user, NONE) before user.getHouses()


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.