Announcement Announcement Module
Collapse
No announcement yet.
OpenSessionInViewFilter alternative for servlet 2.2/struts? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • OpenSessionInViewFilter alternative for servlet 2.2/struts?

    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

  • #2
    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.

    Comment


    • #3
      What's best practice for touching lazy loaded collection?

      What's the best practice for touching lazy loaded collection?

      Comment


      • #4
        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:


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

        Comment


        • #5
          I don't agree to avoid usage of OpenSessionInViewFilter

          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.

          Comment


          • #6
            did you consider using OpenSessionInViewInterceptor?

            Comment


            • #7
              I would love an example of how to utilize that with struts?

              I would love an example of how to utilize that with struts?

              Comment


              • #8
                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:
                Code:
                	<bean id="secureHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
                		<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

                Comment


                • #9
                  See my comment on the following JIRA issue:

                  http://opensource.atlassian.com/proj...browse/SPR-350

                  Juergen

                  Comment


                  • #10
                    > 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."

                    Comment


                    • #11
                      See enclosed

                      I developed something for those of us who don't want to be helped.

                      http://forum.springframework.org/sho...?t=10519<br />
                      Last edited by robyn; May 14th, 2006, 09:58 AM.

                      Comment

                      Working...
                      X