You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by hezjing <he...@gmail.com> on 2007/08/22 10:39:14 UTC

Configuring OpenSessionInViewInterceptor as S2's interceptor

Hi

I'm encountering this exception (when trying to iterate a list of
orders for a specific customer in JSP),

failed to lazily initialize a collection of role:
com.dummy.Customer.orders, 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)
at org.apache.struts2.util.MakeIterator.convert(MakeIterator.java:81)
......

I'm using Struts 2, Hibernate and Spring (for creating actions, DAOs and etc).

I read that Spring's OpenSessionInViewInterceptor will resolve this
problem but can you describe how to configure
OpenSessionInViewInterceptor in Struts2?

And, is there a better alternative than using OpenSessionInViewInterceptor?



-- 

Hez

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


Re: Configuring OpenSessionInViewInterceptor as S2's interceptor

Posted by cilquirm <aa...@gmail.com>.
In my experience, just having OSIV doesn't automatically imply that you
expect all your data to be retrieved ad-hoc. (maybe the users do :) 

  OSIV doesn't prevent the user from using eager fetching when necessary or
useful ( and my experience delays it until your application is working and
you're onto improving performance ).  There's no reason I still can't
Hibernate.initialize() or  fetch join when I want to bring down the number
of round-trips to the database.   OSIV does provide a big boost ( to me, at
least ) in being able to do rapid turn-around development.


Toni Lyytikäinen wrote:
> 
> Lazy initialization is to boost performance. The problem is that it
> doesn't
> apply to all cases. In cases where you have to iterate over a list of
> objects and those have lazy loading properties that you need, it will
> generate a lot of extra queries to to database. In those cases it's best
> to
> use eager loading to fetch the properties. However, if you eager load too
> many properties, the queries will become slow because of the joins. So
> it's
> all about finding a good balance that fits you database schema. In order
> to
> find the balance monitor the amount of queries your actions make, and the
> time it took to execute the queries. In some simple database schemas OSIV
> can be avoided but with complex schemas this isn't always the case.
> 
> On 8/22/07, hezjing <he...@gmail.com> wrote:
>>
>> This is something new to me, I always thought that lazy initialization
>> is to boost performance. So we should really use eager fetching by
>> default and minimize the use of OSIV.
>>
>>
>> On 8/22/07, Toni Lyytikäinen <to...@gmail.com> wrote:
>> > "And, is there a better alternative than using
>> > OpenSessionInViewInterceptor?"
>> >
>> > Yes. It's important to realise that while OpenSessionInView feels very
>> > handy, it is not a particularly nice design pattern, as it can stress
>> your
>> > database with lots of small queries. It's best to learn how to do eager
>> > fetching with Hibernate and use it for most cases and rely on OSIV only
>> in
>> > some special cases where eager fetching is not possible. This way it's
>> often
>> > possible to build entire web applications without resorting to use
>> OSIV.
>> >
>>
>>
>> --
>>
>> Hez
>>
> 
> 

-- 
View this message in context: http://www.nabble.com/Configuring-OpenSessionInViewInterceptor-as-S2%27s-interceptor-tf4310239.html#a12278172
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: Configuring OpenSessionInViewInterceptor as S2's interceptor

Posted by Toni Lyytikäinen <to...@gmail.com>.
Lazy initialization is to boost performance. The problem is that it doesn't
apply to all cases. In cases where you have to iterate over a list of
objects and those have lazy loading properties that you need, it will
generate a lot of extra queries to to database. In those cases it's best to
use eager loading to fetch the properties. However, if you eager load too
many properties, the queries will become slow because of the joins. So it's
all about finding a good balance that fits you database schema. In order to
find the balance monitor the amount of queries your actions make, and the
time it took to execute the queries. In some simple database schemas OSIV
can be avoided but with complex schemas this isn't always the case.

On 8/22/07, hezjing <he...@gmail.com> wrote:
>
> This is something new to me, I always thought that lazy initialization
> is to boost performance. So we should really use eager fetching by
> default and minimize the use of OSIV.
>
>
> On 8/22/07, Toni Lyytikäinen <to...@gmail.com> wrote:
> > "And, is there a better alternative than using
> > OpenSessionInViewInterceptor?"
> >
> > Yes. It's important to realise that while OpenSessionInView feels very
> > handy, it is not a particularly nice design pattern, as it can stress
> your
> > database with lots of small queries. It's best to learn how to do eager
> > fetching with Hibernate and use it for most cases and rely on OSIV only
> in
> > some special cases where eager fetching is not possible. This way it's
> often
> > possible to build entire web applications without resorting to use OSIV.
> >
>
>
> --
>
> Hez
>

Re: Configuring OpenSessionInViewInterceptor as S2's interceptor

Posted by hezjing <he...@gmail.com>.
This is something new to me, I always thought that lazy initialization
is to boost performance. So we should really use eager fetching by
default and minimize the use of OSIV.


On 8/22/07, Toni Lyytikäinen <to...@gmail.com> wrote:
> "And, is there a better alternative than using
> OpenSessionInViewInterceptor?"
>
> Yes. It's important to realise that while OpenSessionInView feels very
> handy, it is not a particularly nice design pattern, as it can stress your
> database with lots of small queries. It's best to learn how to do eager
> fetching with Hibernate and use it for most cases and rely on OSIV only in
> some special cases where eager fetching is not possible. This way it's often
> possible to build entire web applications without resorting to use OSIV.
>


-- 

Hez

Re: Configuring OpenSessionInViewInterceptor as S2's interceptor

Posted by Toni Lyytikäinen <to...@gmail.com>.
"And, is there a better alternative than using
OpenSessionInViewInterceptor?"

Yes. It's important to realise that while OpenSessionInView feels very
handy, it is not a particularly nice design pattern, as it can stress your
database with lots of small queries. It's best to learn how to do eager
fetching with Hibernate and use it for most cases and rely on OSIV only in
some special cases where eager fetching is not possible. This way it's often
possible to build entire web applications without resorting to use OSIV.

Re: Configuring OpenSessionInViewInterceptor as S2's interceptor

Posted by Roberto Nunnari <ro...@supsi.ch>.
probably you'll need to invert the filter-mapping declaration order.


hezjing wrote:
> With  OpenSessionInViewFilter, my web.xml will look like this
> 
> <filter>
>   <filter-name>struts2</filter-name>
>   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
> </filter>
> <filter>
>   <filter-name>sessionFilter</filter-name>
>   <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
> </filter>
> <filter-mapping>
>   <filter-name>struts2</filter-name>
>   <url-pattern>/*</url-pattern>
> </filter-mapping>
> <filter-mapping>
>   <filter-name>sessionFilter</filter-name>
>   <url-pattern>/*</url-pattern>
> </filter-mapping>
> 
> 
> Does it cause any conflict when both struts2 and sessionFilter are
> mapped to the same URL pattern?
> 
> 
> 
> On 8/22/07, Toni Lyytikäinen <to...@gmail.com> wrote:
>> You can't use the OpenSessionInViewInterceptor from Spring in Struts 2.
>> Struts 2 interceptors are not compatible with Spring interceptors as they
>> have their own interface. You'll have to use OpenSessionInViewFilter. Take a
>> look here:
>>
>> http://www.springframework.org/docs/api/org/springframework/orm/hibernate/support/OpenSessionInViewFilter.html
>>
>>
>>
>> On 8/22/07, hezjing <he...@gmail.com> wrote:
>>> Hi
>>>
>>> I'm encountering this exception (when trying to iterate a list of
>>> orders for a specific customer in JSP),
>>>
>>> failed to lazily initialize a collection of role:
>>> com.dummy.Customer.orders, 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)
>>> at org.apache.struts2.util.MakeIterator.convert(MakeIterator.java:81)
>>> ......
>>>
>>> I'm using Struts 2, Hibernate and Spring (for creating actions, DAOs and
>>> etc).
>>>
>>> I read that Spring's OpenSessionInViewInterceptor will resolve this
>>> problem but can you describe how to configure
>>> OpenSessionInViewInterceptor in Struts2?
>>>
>>> And, is there a better alternative than using
>>> OpenSessionInViewInterceptor?
>>>
>>>
>>>
>>> --
>>>
>>> Hez
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: user-help@struts.apache.org
>>>
>>>
> 
> 


-- 
Roberto Nunnari
Servizi Informatici SUPSI-DTI
SUPSI-DTI - Via Cantonale - 6928 Manno - Switzerland
email: mailto:roberto.nunnari@supsi.ch
tel: +41-58-6666561


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


Re: Configuring OpenSessionInViewInterceptor as S2's interceptor

Posted by hezjing <he...@gmail.com>.
With  OpenSessionInViewFilter, my web.xml will look like this

<filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter>
  <filter-name>sessionFilter</filter-name>
  <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
  <filter-name>sessionFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>


Does it cause any conflict when both struts2 and sessionFilter are
mapped to the same URL pattern?



On 8/22/07, Toni Lyytikäinen <to...@gmail.com> wrote:
> You can't use the OpenSessionInViewInterceptor from Spring in Struts 2.
> Struts 2 interceptors are not compatible with Spring interceptors as they
> have their own interface. You'll have to use OpenSessionInViewFilter. Take a
> look here:
>
> http://www.springframework.org/docs/api/org/springframework/orm/hibernate/support/OpenSessionInViewFilter.html
>
>
>
> On 8/22/07, hezjing <he...@gmail.com> wrote:
> >
> > Hi
> >
> > I'm encountering this exception (when trying to iterate a list of
> > orders for a specific customer in JSP),
> >
> > failed to lazily initialize a collection of role:
> > com.dummy.Customer.orders, 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)
> > at org.apache.struts2.util.MakeIterator.convert(MakeIterator.java:81)
> > ......
> >
> > I'm using Struts 2, Hibernate and Spring (for creating actions, DAOs and
> > etc).
> >
> > I read that Spring's OpenSessionInViewInterceptor will resolve this
> > problem but can you describe how to configure
> > OpenSessionInViewInterceptor in Struts2?
> >
> > And, is there a better alternative than using
> > OpenSessionInViewInterceptor?
> >
> >
> >
> > --
> >
> > Hez
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>


-- 

Hez

Re: Configuring OpenSessionInViewInterceptor as S2's interceptor

Posted by Toni Lyytikäinen <to...@gmail.com>.
You can't use the OpenSessionInViewInterceptor from Spring in Struts 2.
Struts 2 interceptors are not compatible with Spring interceptors as they
have their own interface. You'll have to use OpenSessionInViewFilter. Take a
look here:

http://www.springframework.org/docs/api/org/springframework/orm/hibernate/support/OpenSessionInViewFilter.html



On 8/22/07, hezjing <he...@gmail.com> wrote:
>
> Hi
>
> I'm encountering this exception (when trying to iterate a list of
> orders for a specific customer in JSP),
>
> failed to lazily initialize a collection of role:
> com.dummy.Customer.orders, 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)
> at org.apache.struts2.util.MakeIterator.convert(MakeIterator.java:81)
> ......
>
> I'm using Struts 2, Hibernate and Spring (for creating actions, DAOs and
> etc).
>
> I read that Spring's OpenSessionInViewInterceptor will resolve this
> problem but can you describe how to configure
> OpenSessionInViewInterceptor in Struts2?
>
> And, is there a better alternative than using
> OpenSessionInViewInterceptor?
>
>
>
> --
>
> Hez
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>