spring - OpenSessionInViewFilter and Wicket and still running into LazyInitializationExceptions -


i'm using spring's opensessioninviewfilter in combination wicket , kept running lazyinitializationexceptions, couldn't resolved. see in logging hibernate session closed before exception thrown, doing wrong.

the osivfilter must above wicketfilter, or else requests won't reach it. wicketfilter handles page requests itself, doesn't delegate other components, such servlet (thus, filter chain stops there).

another way create implementation of irequestcyclelistener same osivfilter. i've created such class modifying spring's filter code comply new interface, below:

import org.apache.wicket.metadatakey; import org.apache.wicket.protocol.http.webapplication; import org.apache.wicket.request.cycle.abstractrequestcyclelistener; import org.apache.wicket.request.cycle.requestcycle; import org.hibernate.flushmode; import org.hibernate.hibernateexception; import org.hibernate.session; import org.hibernate.sessionfactory; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.dao.dataaccessresourcefailureexception; import org.springframework.orm.hibernate4.sessionfactoryutils; import org.springframework.orm.hibernate4.sessionholder; import org.springframework.transaction.support.transactionsynchronizationmanager; import org.springframework.web.context.webapplicationcontext; import org.springframework.web.context.support.webapplicationcontextutils;  public class opensessioninviewrequestcyclelistener extends abstractrequestcyclelistener {      protected final logger logger = loggerfactory.getlogger(getclass());      public static final string default_session_factory_bean_name = "sessionfactory";      private static final metadatakey<boolean> participate = new metadatakey<boolean>() {     };      private static final metadatakey<sessionfactory> session_factory = new metadatakey<sessionfactory>() {     };      private string sessionfactorybeanname = default_session_factory_bean_name;      private final webapplication application;      public opensessioninviewrequestcyclelistener(webapplication application) {         this.application = application;     }      /**      * set bean name of sessionfactory fetch spring's      * root application context. default "sessionfactory".      * @see #default_session_factory_bean_name      */     public void setsessionfactorybeanname(string sessionfactorybeanname) {         this.sessionfactorybeanname = sessionfactorybeanname;     }      /**      * return bean name of sessionfactory fetch spring's      * root application context.      */     protected string getsessionfactorybeanname() {         return this.sessionfactorybeanname;     }      @override     public void onbeginrequest(requestcycle cycle) {         sessionfactory sessionfactory = lookupsessionfactory(cycle);         cycle.setmetadata(session_factory, sessionfactory);         cycle.setmetadata(participate, false);          if (transactionsynchronizationmanager.hasresource(sessionfactory)) {             // not modify session: set participate flag.             cycle.setmetadata(participate, true);         }         else {             logger.debug("opening hibernate session in opensessioninviewrequestcyclelistener");             session session = opensession(sessionfactory);             sessionholder sessionholder = new sessionholder(session);             transactionsynchronizationmanager.bindresource(sessionfactory, sessionholder);         }     }      @override     public void onendrequest(requestcycle cycle) {         if (!cycle.getmetadata(participate)) {             sessionholder sessionholder = (sessionholder) transactionsynchronizationmanager.unbindresource(cycle.getmetadata(session_factory));             logger.debug("closing hibernate session in opensessioninviewfilter");             sessionfactoryutils.closesession(sessionholder.getsession());         }     }      /**      * sessionfactory filter should use,      * taking current http request argument.      * <p>the default implementation delegates {@link #lookupsessionfactory()}      * variant without arguments.      * @param cycle current request      * @return sessionfactory use      */     protected sessionfactory lookupsessionfactory(requestcycle cycle) {         return lookupsessionfactory();     }      /**      * sessionfactory filter should use.      * <p>the default implementation looks bean specified name      * in spring's root application context.      * @return sessionfactory use      * @see #getsessionfactorybeanname      */     protected sessionfactory lookupsessionfactory() {         if (logger.isdebugenabled()) {             logger.debug("using sessionfactory '" + getsessionfactorybeanname() + "' opensessioninviewfilter");         }         webapplicationcontext wac = webapplicationcontextutils.getrequiredwebapplicationcontext(this.application.getservletcontext());         return wac.getbean(getsessionfactorybeanname(), sessionfactory.class);     }      /**      * open session sessionfactory filter uses.      * <p>the default implementation delegates      * {@code sessionfactory.opensession} method ,      * sets {@code session}'s flush mode "manual".      * @param sessionfactory sessionfactory filter uses      * @return session use      * @throws dataaccessresourcefailureexception if session not created      * @see org.hibernate.flushmode#manual      */     protected session opensession(sessionfactory sessionfactory) throws dataaccessresourcefailureexception {         try {             session session = sessionfactory.opensession();             session.setflushmode(flushmode.manual);             return session;         } catch (hibernateexception ex) {             throw new dataaccessresourcefailureexception("could not open hibernate session", ex);         }     }  } 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -