-->
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.  [ 2 posts ] 
Author Message
 Post subject: How LazyInitializationException with OpenSessionInView
PostPosted: Mon Dec 05, 2005 7:52 pm 
Newbie

Joined: Mon Dec 05, 2005 8:52 am
Posts: 4
1. I can't find "since Hibernate 3.0.1 OpenSessionInView is built-in without having to use Spring..." . What do I need to do special to use this built-in feature?

2. In our Struts->EJB->Hibernate application, we have mutltiple Hibernate session open/close calls made before the requested page is returned to the user. If I understand this correctly using 'OpenSessionInView' pattern the hibernate session would be opened & closed only once. Can anyone comment on performance, memory consumption, server load, connection limitations, connection pool sizes, etc. This would be used to justify this big code modification which would probably involve stripping out all those getCurrentSession() and closeSession() calls in the Data access layer (Dal) classes.

In a Struts->EJB->Hibernate application with the following sample Object hierarchy:

Client
|-ClientGUID
|-FirstName
|-LastName
+-Roles (Map) (lazy=true)
|--RoleCode
|--Role
|
+-AddressRoles (Map) (lazy=true)
|--AddressRoleCode
|--Address

In a typical HTTP request:
* In the business layer with ClientGUID obtained from the Struts ActionForm we fetch the Client object in the DAO (Hibernate session open & close)
* In the business layer we realize we need more of the Client details. And, since accessing them on the retrieved Client object instance gives LazyInitializationException, we use the ClientGUID to lookup the Roles & AddressRoles using the data access layer DAOs(Hibernate session open & close in both cases)

But, with the OpenSessionInView pattern it sounds like for the entire Request-Response cycle there will be only one Hibernate session open & close. On the face of it it seems like this would be a better performer since opening & closing database connections/sessions is an expensive operation I would imagine.

Any coments, suggestions?

Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 05, 2005 10:38 pm 
Beginner
Beginner

Joined: Tue Jun 07, 2005 11:36 pm
Posts: 22
For reading info as you are doing, the OpenSessionInView pattern works great. You don't have to worry about accidentally leaving a session open, and you reduce or eliminate the search-specific code in your business layer because you let lazy loading kick in when needed to get the extra info you want.

The pattern also works well for "guaranteed no conflict writes" - inserts or updates where you are certain that you won't conflict with another thread doing the same thing or another thread isn't waiting on your to commit your changes.

There are tons of threads on it, including this one that I happened to chip in on: http://forum.hibernate.org/viewtopic.php?t=949667

_________________
Useful? Hit me with a point! Completely off base? Ya, well, what do you expect from a newbie...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.