tapestry-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Colin Sampaleanu <colin...@exis.com>
Subject Re: hibernate/tapestry & lazy initalization
Date Tue, 06 Jan 2004 22:43:49 GMT
I actually wrote the document mentioned, which  is available at:
  http://www.springframework.org/docs/integration/tapestry.html

As of the current 1.0M4 release, Spring actually offers built-in support 
for the Open Session in View 'pattern' with Hibernate.

However, I am personally not a big fan of using that pattern, with or 
without Spring. The problem is due to the fact that as per the Hibernate 
docs, once a Hibernate Session throws an Exception, the Session is 
considered to be inconsistent, and should not be used any longer. If you 
use the Open Session in View pattern however, the Session is handled by 
a layer above your view code (the servlet filter or view interceptor in 
the Spring case), but your view code is calling down to other code 
(service layer) which may do something to kill the Session in this 
fashion without any of the layers above (the view layer or the layer 
actually handling the session) knowing anything about that, and the 
session may thus continue to be used by other view code.

Personally, I think the best way to handle all of this is to create use 
case driven service layer code, which properly operates in a transaction 
(and handles the Hibernate session automatically via Spring), which 
returns all the needed data. With regards to lazy loaded data, it should 
touch any such data as needed for the use case.    Much less potential 
for getting into trouble.


John Meredith wrote:

>Hi John,
>
>Somehow I missed the original post (no idea where my filters have
>filtered it off to) ...
>
>Anyway, what I find works very well is the "Open Session in View"
>pattern/filter: http://www.hibernate.org/43.html
>
>A session is opened for the entire request and automatically closed.
>
>Another thing worth looking into is possibly the Spring Framework. I've
>played around with it briefly ... from memory, there's also a tutorial
>available for using Tapestry + Spring + Hibernate on their Wiki.
>
>Hope this helps.
>
>  - John
>
>On Tue, 2004-01-06 at 20:19, Eric Schneider wrote:
>  
>
>>John,
>>
>>Try creating the session in prepareForRender(), see if that makes a
>>difference.
>>
>>Cheers,
>>Eric
>>
>>----- Original Message ----- 
>>From: "Jamie Orchard-Hays" <jamie@dang.com>
>>To: "Tapestry users" <tapestry-user@jakarta.apache.org>;
>><studarus@jhlconsulting.com>
>>Sent: Tuesday, January 06, 2004 2:11 PM
>>Subject: Re: hibernate/tapestry & lazy initalization
>>
>>
>>    
>>
>>>I don't think initialize() is where you want to do this. That's where you
>>>set properties to the default values you want when you are *finished* with
>>>the page (usually null).
>>>
>>>
>>>----- Original Message ----- 
>>>From: "John Studarus" <studarus@jhlconsulting.com>
>>>To: <tapestry-user@jakarta.apache.org>
>>>Sent: Tuesday, January 06, 2004 1:29 PM
>>>Subject: hibernate/tapestry & lazy initalization
>>>
>>>
>>>      
>>>
>>>>  I've been using Tapestry with Hibernate (and Tomcat 5)
>>>>for a few months now with great results.  I recently ran
>>>>into a problem that I am curious if anyone else has
>>>>encountered.
>>>>  I've been rely upon Hibernate to populate objects
>>>>for me from the database via OGNL calls in Tapestry.
>>>>However, at times, Tapestry calls Hibernate objects
>>>>(like the source in a foreach) without hitting any of
>>>>my code.  This means I don't have the opportunity to
>>>>create a Session for Hibernate to use and I end up
>>>>with an exception.
>>>>  I need to be able to create a session object
>>>>before Tapestry makes any calls into my Hibernate
>>>>objects.  Any ideas on how this could be done?  I've
>>>>created the Hibernate sessions in both the Tapestry
>>>>page constructor and inside the initialize call.  I
>>>>get the exception when Tapestry pulls up the Hibernate
>>>>object.  Or I wonder if Tapestry is doing its work
>>>>in a different thread than my code and can't see
>>>>my Thread specific session.
>>>>  This only effects Tapestry/Hibernate when lazy initalization
>>>>is turned on.  I have a feeling that most people use
>>>>lazy initalization when using Tapestry and Hibernate together
>>>>but the performance hit is too great for me to leave lazy
>>>>initalization turned on (difference between something taking
>>>>1.5 hours or 30 seconds...).
>>>>  There was a post out there about using a Filter - anyone
>>>>every tried that?  Any Hibernate/Tapestry experts out there?
>>>>  Thanks,
>>>>
>>>>        John
>>>>
>>>>net.sf.hibernate.LazyInitializationException: Failed to lazily
>>>>        
>>>>
>>initialize
>>    
>>
>>>a collection - no Session
>>>      
>>>
>>>>        at
>>>>        
>>>>
>>net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollec
>>    
>>
>>>tion.java:167)
>>>      
>>>
>>>>        at
>>>>        
>>>>
>>net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.j
>>    
>>
>>>ava:64)
>>>      
>>>
>>>>        at net.sf.hibernate.collection.Set.size(Set.java:114)
>>>>        at
>>>>        
>>>>
>>org.apache.tapestry.Tapestry$CollectionIteratorAdaptor.coerce(Tapestry.java:
>>    
>>
>>>568)
>>>      
>>>
>>>>        at
>>>>        
>>>>
>>>org.apache.tapestry.Tapestry.coerceToIterator(Tapestry.java:864)
>>>      
>>>
>>>>        at
>>>>        
>>>>
>>>org.apache.tapestry.components.Foreach.getSourceData(Foreach.java:113)
>>>      
>>>
>>>>        at
>>>>        
>>>>
>>>org.apache.tapestry.components.Foreach.renderComponent(Foreach.java:126)
>>>      
>>>
>>>>...  cut ...
>>>>
>>>>
>>>>---------------------------------------------------------------------
>>>>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>>>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>>>        
>>>>



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Mime
View raw message