/*
  * HibernateOpenSessionInViewInterceptor.java
  *
  * Created on March 18, 2006, 3:51 PM
  *
  * To change this template, choose Tools | Template Manager
  * and open the template in the editor.
  */
 
package edu.washington.javawebdevelopment.webwork.interceptor;
 
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.AroundInterceptor;
import edu.washington.javawebdevelopment.dao.DaoFactoryHibernate;
import javax.servlet.ServletException;
import org.hibernate.SessionFactory;
import org.hibernate.StaleObjectStateException;
 
/**
  *
  * @author gary
  */
public class HibernateOpenSessionInViewInterceptor extends AroundInterceptor {
     private SessionFactory hibernateSessionFactory;
 
     public void init() {
         System.out.println("Initializing HibernateOpenSessionInViewInterceptor 
interceptor, obtaining Hibernate SessionFactory from DaoFactoryHibernate");
         hibernateSessionFactory = DaoFactoryHibernate.getSessionFactory();
     }
 
     public void destroy() {
     }
 
     public void before(ActionInvocation invocation) throws Exception {
         System.out.println("Starting a database transaction in the 
HibernateOpenSessionInViewInterceptor");
         hibernateSessionFactory.getCurrentSession().beginTransaction();
     }
 
     public void after(ActionInvocation invocation, String result) throws 
Exception {
         // Commit and cleanup
         try {
             System.out.println("Committing the database transaction in the 
HibernateOpenSessionInViewInterceptor");
             
hibernateSessionFactory.getCurrentSession().getTransaction().commit();
         } catch (StaleObjectStateException staleEx) {
             System.err.println("This interceptor does not implement optimistic 
concurrency control!");
             System.err.println("Your application will not work until you add 
compensation actions!");
             // Rollback, close everything, possibly compensate for any 
permanent changes
             // during the conversation, and finally restart business 
conversation. Maybe
             // give the user of the application a chance to merge some if his 
work with
             // fresh data... what you do here depends on your applications 
design.
             throw staleEx;
         } catch (Throwable ex) {
             // Rollback only
             ex.printStackTrace();
             try {
                 if 
(hibernateSessionFactory.getCurrentSession().getTransaction().isActive()) {
                     System.out.println("Trying to rollback database 
transaction after exception");
                     
hibernateSessionFactory.getCurrentSession().getTransaction().rollback();
                 }
             } catch (Throwable rbEx) {
                 System.err.println("Could not rollback transaction after 
exception! - " + rbEx);
             }
 
             // Let others handle it... maybe another interceptor for 
exceptions?
             throw new ServletException(ex);
         }
     }
}

 

?

Martin-

  



> Date: Tue, 11 Feb 2014 14:42:47 -0700
> Subject: Re: REST question
> From: ken.mcwilli...@gmail.com
> To: user@struts.apache.org
> 
> Sounds like you have used the Open Session in View method... probably
> provided by a custom interceptor, when adding the rest plugin you have
> changed your action to the rest package and forgot to put your interceptor
> back in place, so now you don't have a hibernate session and the view
> generates this error.
> 
> 
> On Mon, Feb 10, 2014 at 10:40 PM, Miguel <miguel...@gmail.com> wrote:
> 
> > Hi,
> >
> > A strange situation happens when I load *struts2-rest-plugin-2.3.16.jar*
> > With out the REST plugin my action and jsp's works as always has worked.
> > But with the mere inclusion of the REST Plugin, some jsp's stops finding
> > variables, because the variable name changes.
> > Here is the snippet:
> >
> > struts.xml
> >
> > <constant name="struts.objectFactory"
> > value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
> > <constant name="struts.objectFactory.spring.autoWire" value="name" />
> > <package name="proyectox" extends="json-default" >
> > <!-- Si no le pones este stack, pues no funciona...-->
> > <interceptors>
> > <interceptor name="strutsSpring"
> >
> > class="com.proyectox.ui.interceptor.HibernateOpenSessionInViewInterceptor"/>
> > <interceptor name="viewuseragentselector"
> > class="com.proyectox.ui.interceptor.UserAgentResultInterceptor"/>
> > <interceptor-stack name="strutsSpringPPPStack">
> > <interceptor-ref name="strutsSpring"/>
> > <interceptor-ref name="paramsPrepareParamsStack"/>
> > <interceptor-ref name="viewuseragentselector"/>
> > </interceptor-stack>
> > </interceptors>
> > ...
> > </package>
> >
> > <package name="cfdi" extends="proyectox" namespace="/cfdi">
> > <action name="header_*" class="com.fcm.cfdi.ui.HeaderAction">
> > <result type="redirectAction">
> > <param name="actionName">edit</param>
> > </result>
> > <!-- <result >printHeader.jsp</result>-->
> > <result name="input">headerEdit.jsp</result>
> > <result name="error">/facturaerror.jsp</result>
> > <interceptor-ref name="scope">
> > <param name="session">contribuyente</param>
> > <param name="autoCreateSession">true</param>
> > <param name="key">contribuyenteActual</param>
> > </interceptor-ref>
> > <interceptor-ref name="scope">
> > <param name="session">factura</param>
> > <param name="autoCreateSession">false</param>
> > <param name="key">cfdi</param>
> > </interceptor-ref>
> > <interceptor-ref name="strutsSpringPPPStack"/>
> > </action>
> >
> >
> >
> > HeaderAction.java:
> >
> > public class HeaderAction extends ActionSupport implements Preparable {
> > protected Contribuyente contribuyente;
> > protected Long contribuyenteId;
> >
> > public void prepare() throws Exception {
> > ...
> > }
> >
> >
> > public String input(){
> >
> > dirReceptorId = factura.getDomicilioReceptor().getId();
> > dirEmisionId = factura.getDomicilioEmision().getId();
> > return INPUT;
> > }
> > ...
> > public Contribuyente getContribuyente() {
> > return contribuyente;
> > }
> >
> > public void setContribuyente(Contribuyente contribuyente) {
> > this.contribuyente = contribuyente;
> > }
> > ...
> > }
> >
> > edit.jsp:
> > ...
> > <s:select label="Direccion Emision"
> > name="dirEmisionId"
> > list="*contribuyente*.domicilios"
> > listKey="id"
> > listValue="nombre"
> > multiple="false"
> > size="1"
> > required="true"
> > key="factura.direccionEmision.id"
> > />
> > ...
> >
> > After loading *struts2-rest-plugin-2.3.16.jar* it starts giving the
> > following exception:
> > Caused by: org.hibernate.LazyInitializationException: failed to lazily
> > initialize a collection of role: *contribuyentes.domicilios*, no session or
> > session was closed
> > ...
> >
> > I know that's a Hibernate exception, but the jsp works fine without the
> > REST Plugin.
> > It's like the REST Plugin appends that extra "s" after the variable name in
> > the .jsp or the value stack changes or something like that.
> >
> > The action is invoked without error in either case, and has into it's
> > fields the required values.
> >
> >
> > Does anyone has any pointer of what can be happening?
> >
> > Thanks
> >
> > Si quieres ser más positivo, pierde un electrón
> > Miguel Ruiz Velasco Sobrino
> >
                                          

Reply via email to