PDA

View Full Version : OpenSessionInViewFilter alternative for servlet 2.2/struts?



garpinc2
Sep 20th, 2004, 11:14 PM
Is there an OpenSessionInViewFilter alternative for servlet 2.2 containers where filters are not supported? I am using struts as MVC architecture so please answer with this in mind. If so please email me at KeithGarry.Boyce at bcbsma.com. Thanks

kajism
Sep 22nd, 2004, 08:51 AM
It is better to avoid usage of OpenSessionInViewFilter at all. A better alternative is to prepare all the object graph (which will be used by the view) in your service methods by "touching" the lazy loaded collections or proxies. This is much more cleaner, bug-prone approach and keeps the separation of layers.

You can search the net for more discussion of this topic.

garpinc2
Sep 23rd, 2004, 10:23 AM
What's the best practice for touching lazy loaded collection?

kajism
Sep 23rd, 2004, 10:41 AM
Assuming that your service methods are running in transactions and because the same Hibernate session is open in transaction. Then your service method can look like this:




public MyObject myServiceMethod() {
MyObject mo = myDao.findMyObject();
if (mo != null) {
mo.getMyLazyCollection().size(); // inits lazy collection
// another lazy inits can go here
}
return myObject;
}

garpinc2
Sep 24th, 2004, 03:26 PM
Our application has a paged list of entities.

collection1
Obj contains -> subCollection1
Obj contains -> subSubCollection.

On the first page why should I go to the DB and populate all the subcollections.

I only need data for the subcollections that I am about to view.

Also the code to initialize lazily is messy. Why do I need to iterate through the collections and sub collections to initialize them only then to go to the view and iterate through them again. This doesn't seem to make sense.


So I ask again. Is there another alternative.

Is there a way to hold onto the session, pass it to the view and in the view get rid of the pointer to the session.

irbouho
Sep 24th, 2004, 03:48 PM
did you consider using OpenSessionInViewInterceptor?

garpinc2
Sep 24th, 2004, 04:42 PM
I would love an example of how to utilize that with struts?

irbouho
Sep 24th, 2004, 04:50 PM
For an example on how to use Spring with Struts, take a look at jpetstore from Spring distribution.

Following excerpt, from jpetstore-servlet.xml, show how to configure an interceptor:

<bean id="secureHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
<property name="interceptors">
<list>
<ref bean="signonInterceptor"/>
</list>
</property>
<property name="urlMap">
<map>
<entry key="/shop/editAccount.do"><ref local="secure_editAccount"/></entry>
<entry key="/shop/listOrders.do"><ref local="secure_listOrders"/></entry>
<entry key="/shop/newOrder.do"><ref local="secure_newOrder"/></entry>
<entry key="/shop/viewOrder.do"><ref local="secure_viewOrder"/></entry>
</map>
</property>
</bean>


HTH

Juergen Hoeller
Sep 24th, 2004, 05:04 PM
See my comment on the following JIRA issue:

http://opensource.atlassian.com/projects/spring/browse/SPR-350

Juergen

kajism
Sep 25th, 2004, 12:22 AM
> On the first page why should I go to the DB
> and populate all the subcollections.

I didn't write that you should populate all the subcollections. I said you should populate excatly what your view needs.
So why don't you pass the limit values as arguments of your service methods? I use for example my PagingAndSorting object where I store: orderColumn, orderDescription, firstPage and pageCount.
Than those values can be used by any DAO implementation, it doesn't depend on Hibernate lazy magic.

> I only need data for the subcollections that I am about to view.

Exactly.


> Also the code to initialize lazily is messy.
> Why do I need to iterate through the collections
>and sub collections to initialize them only then
>to go to the view and iterate through them again
> This doesn't seem to make sense.

I don't find anything messy on just touching a variable. My goal is a clear separation of layers and the possibility to change the implementation of my services to something else (than Hibernate).

I find much more messy the OpenSessionInView magic. After excecution of my service method all should be loaded and I can check the queries and fine tune the performance. With the OpenSessionInView approach you never know what will be touched later, lazy loaded and how many queries will be issued.

Imagine you implement the persistence using JDBC - you will also load just the object you will need for your view - nothing less, nothing more.

You can also notice how many threads concerning troubles with the openSessionInView filter appers in this forums...


You can say that the lazy init should be done in the Hibernate DAO implementation. That's true, but than you cannot reuse your DAO's in several business scenarios which everyone needs different object graphs initialized. For this reason it is better to do it in the service layer.

The approach I'm suggesting you is completely valid alternative of OpenSessionInView and in my (and not only my) opinion better alternative (and not so messy and not making sense as you may think).

But as a good Czech proverb says: "He who won't take an advice, cannot be helped."

garpinc2
Sep 25th, 2004, 01:44 AM
I developed something for those of us who don't want to be helped.

http://forum.springframework.org/showthread.php?t=10519