-->
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.  [ 7 posts ] 
Author Message
 Post subject: closed sessions
PostPosted: Wed Apr 12, 2006 3:01 pm 
Newbie

Joined: Tue Apr 04, 2006 1:00 pm
Posts: 8
Hibernate version: 3.1

Full stack trace of any exception that occurs:
ERROR LazyInitializationException:19 - could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:56)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:158)
at jrc.utdanningspartner.docsys.persistent.Client$$EnhancerByCGLIB$$404b8a94.getClientName(<generated>)
at jrc.utdanningspartner.docsys.testfiles.TryMeNow.main(TryMeNow.java:87)
Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:56)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:158)
at jrc.utdanningspartner.docsys.persistent.Client$$EnhancerByCGLIB$$404b8a94.getClientName(<generated>)
at jrc.utdanningspartner.docsys.testfiles.TryMeNow.main(TryMeNow.java:87)

Name and version of the database you are using:MySql 5

Hello!

I'm facing a problem I beleve is quite common, however I have not found a solution/explanation on it - so I'm sorry if this has been answered many times.

As you can see in my code below, in the first session I make some Objects that I save to the database - that work without any problem - alle the associations are intact and works great (I've checked my database, and all relations are correct set). I then close the session and prints the objects with their associated objects -so far no problem.

I then make a second session where I simply get a object with the session.get(), and close the connection.

What I do not understand, is that it is okay to close the first session and then use the objects (e.g. printline ) , but it is not okay to close the seccond session...

Anyone interest in explaining this to me?


My code:



Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();

User user = (User) session.get(User.class, 2 ); // getting a user I know exist
Client client = (Client) session.get(Client.class, 3 );// getting a client I know exist

System.out.println("firstname of User: " + user.getFirstname() );
System.out.println("name on Client: " + client.getClientName() );


Project project = new Project();
project.setProjectTitle("Project 1");
project.setDescription("A description (p1)");
project.setEstablishedDate( Calendar.getInstance().getTime() );
project.representProjectByClient( client );
project.establishProjectByUser( user );
project.setIsActive(true);



Project project2 = new Project();
project2.setProjectTitle("Project 2");
project2.setDescription("A description (p2)");
project2.setEstablishedDate( Calendar.getInstance().getTime() );
project2.representProjectByClient( client );
project2.establishProjectByUser( user );
project2.setIsActive(true);



Project project3 = new Project();
project3.setProjectTitle("Project 3");
project3.setDescription("A description (p2)");
project3.setEstablishedDate( Calendar.getInstance().getTime() );
project3.representProjectByClient( client );
project3.establishProjectByUser( user );
project3.setIsActive(true);

session.save(project);
session.save(project2);
session.save(project3);

session.beginTransaction().commit();


System.out.println("User has established following projects: ");
for ( Iterator it = user.getEstablishedProjects().iterator(); it.hasNext(); ){
System.out.println("\t" + ((Project) it.next()).getProjectTitle() );
}

System.out.println();

System.out.println("Client represent the following projects: ");
for ( Iterator it = client.getRepresentedInProjects().iterator(); it.hasNext(); ){
System.out.println("\t" + ((Project) it.next()).getProjectTitle() );
}



Session session2 = HibernateUtil.getSessionFactory().getCurrentSession();
session2.beginTransaction();

Project p = (Project) session2.get(Project.class, 2 );

session2.beginTransaction().commit();

System.out.println("Project represented by: " + p.getRepresentedByClient().getClientName() );
System.out.println("Project established by: " + p.getEstablishedByUser().getFirstname() );

Thanks in advance
Robert


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 3:36 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Laziness of the relationships causes the error.

You have several options:
set lazy='false' on the relationships;
do not close session while accessing object fields;
initialize object properties before ending session;

And by the way, it is better to write session.getTransaction().commit();
than session.beginTransaction().commit();

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 3:38 pm 
Newbie

Joined: Mon Apr 10, 2006 2:58 pm
Posts: 8
In the first session where you print some info, you are displaying data within the Project object, specifically, the title.

In the second session, you are getting the client and user objects with the project and then trying to reference properties within those objects, but because of lazy loading, those records haven't been loaded from the database as yet and when Hibernate went to load it, it found the session closed.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 3:55 pm 
Newbie

Joined: Tue Apr 04, 2006 1:00 pm
Posts: 8
kgignatyev wrote:
initialize object properties before ending session;

And by the way, it is better to write session.getTransaction().commit();
than session.beginTransaction().commit();


Thanks - makes more sens to me now.
Anyway, what do you mean by initialize object properties before ending session; ?


...and yes, I was just a bit hasty, regarding the beginTransaction.commit() :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 4:00 pm 
Newbie

Joined: Mon Apr 10, 2006 2:58 pm
Posts: 8
For my purposes, I would simply call:

Code:
p.getEstablishedByUser() ;
p.getRepresentedByClient() ;


before you close the session. That way, the user and client are retrieved and can be used outside of the session.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 4:10 pm 
Newbie

Joined: Tue Apr 04, 2006 1:00 pm
Posts: 8
Wel, I tried that:

Code:
User u = p.getEstablishedByUser();
Client c = p.getRepresentedByClient();
session2.getTransaction().commit();


however, I got the same exception when I used the new user- and client-object outside the closed session:

Code:
System.out.println("Project established by: " + u.getFirstname() );
System.out.println("Project represented by: " + c.getClientName() );


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 4:17 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Labanmann wrote:

Thanks - makes more sens to me now.
Anyway, what do you mean by initialize object properties before ending session; ?


Hibernate.initialize( obj )
/**
* Force initialization of a proxy or persistent collection.
* <p/>
* Note: This only ensures intialization of a proxy object or collection;
* it is not guaranteed that the elements INSIDE the collection will be initialized/materialized.
*
* @param proxy a persistable object, proxy, persistent collection or <tt>null</tt>
* @throws HibernateException if we can't initialize the proxy at this time, eg. the <tt>Session</tt> was closed
*/
public static void initialize(Object proxy) throws HibernateException {

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.