4 Replies Latest reply on May 31, 2013 9:22 AM by sfcoy

    Open session in view and EJB 3

    gmlopezdev

      I've read some post about this topic but non of them clarify this. Is it actually possible to use open session in view with EJB3 within jboss AS7?

       

      I've read and actually sound reasonable, that it may be achieved by acquiring and initiating a UserTransaction using a filter. I'm about to test this out however I'd like to get some feedback from the community about this topic.

       

      Thanks for your help!

        • 1. Re: Open session in view and EJB 3
          gmlopezdev

          I did try to implement the approach explained within this article getting the UserTransaction from jndi and worked perfectly, however I'm concerned about side effects I cannot imagine right now.

           

          Any thoughts/experience you might share?

           

          Help/feedback is highly appreciated!

          • 2. Re: Open session in view and EJB 3
            nickarls

            If it works, it ain't broken ;-)

             

            Of course if the load is high and page rendering (request) takes a long time, you will keep the transaction open for the whole time which (depending on your data access) might cause unneccessary locks (guessing here)

            • 3. Re: Open session in view and EJB 3
              gmlopezdev

              Well, my tests have been quite basic :-(. Let's see what happens when I scale them... Any other thoughts?

              • 4. Re: Open session in view and EJB 3
                sfcoy

                My preferred approach has been to use a two phase approach, without using the "open session in view" pattern.

                1. If the request is an update operation, then invoke an EJB3 with all the request parameters and perform the update in a single transaction demarcated on the EJB.
                2. If the update succeeds forward to a response renderer that prepopulates another stateless EJB3 with the values needed to render the result, using @Producer methods to provide the prepopulated named pojos that can be accessed from the JSP or facelet. The @Producer methods would be annotated with @TransactionAttribute(NOT_SUPPORTED)

                 

                If it's not an update operation, then only step 2 applies. In some circumstances it may be appropriate to use the same EJB for both steps.

                 

                For performance and scalability you want to minimise the number of transactions that begin and commit during the HTTP request/response cycle.