Hi I've got some issues with the OpenSessionInViewFilter I integrated into a Wicket application to get rid of the LazyLoadingException problem.
So my problem is. I got a login page with a SignInPanel. When a user tries to log into the web site his credentials are passed to the authentication() method of a Session class which extends AuthenticatedWebSession. In this method a AuthenticationService tries to load the user form the database, when it succeeds the user is stored in the session. The method in the AuthenticationService class which loads the user is annotated with @Transactional. After the user is loaded procession of the request goes on. During this getRoles() of the session class is called to check the user's rights. Therefore getRoles() gets called on the user which was stored in the session before. getRoles() should normally lazy loads the roles of the user but unfortunately a LazyLoadingException is thrown. This behavior is really strange as I thought the OpenSessionInViewFilter takes care that all requests to the database within on web request use the same session. To examine this issue further I activate logging on the OpenSessionInViewFilter. This shows me that the session is opened and closed multiple times by the OpenSessionInViewFilter during the request. It is normal that the session got opened and closed multiple times by the OpenSessionInViewFilter during a request? Does any one has an idea why this happens and how to fix this? *My web.xml * /<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>StoreFinder</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>wicket.web</filter-name> <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class> <init-param> <param-name>applicationClassName</param-name> <param-value>com.appandmore.storefinder.backend.core.web.StoreFinderApplication</param-value> </init-param> <init-param> <param-name>configuration</param-name> <param-value>development</param-value> </init-param> </filter> <filter> <filter-name>wicket.session</filter-name> <filter-class>org.apache.wicket.protocol.http.servlet.WicketSessionFilter</filter-class> <init-param> <param-name>filterName</param-name> <param-value>wicket.web</param-value> </init-param> </filter> <filter> <filter-name>open.hibernate.session.in.view</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>sessionFactoryBeanName</param-name> <param-value>sessionFactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>open.hibernate.session.in.view</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>wicket.web</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> / *My applicationContext.xml* / <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:application.properties</value> </list> </property> <property name="ignoreResourceNotFound" value="true"/> </bean> <bean id="authenticationService" class="com.appandmore.storefinder.backend.core.service.security.AuthenticationServiceImpl"> <constructor-arg ref="userDao"/> </bean> <bean id="offerDao" class="com.appandmore.storefinder.backend.core.infrastructure.hibernate.OfferDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="userDao" class="com.appandmore.storefinder.backend.core.infrastructure.hibernate.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="roleDao" class="com.appandmore.storefinder.backend.core.infrastructure.hibernate.RoleDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="imageDao" class="com.appandmore.storefinder.backend.core.infrastructure.hibernate.ImageDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="storeDao" class="com.appandmore.storefinder.backend.core.infrastructure.hibernate.StoreDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.h2.Driver"/> <property name="url" value="jdbc:h2:file:target/db/"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="use_outer_join">true</prop> <prop key="hibernate.cache.use_second_level_cache">false</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop> <prop key="hibernate.hbm2ddl.auto">create-drop</prop> <prop key="hibernate.connection.pool_size">10</prop> <prop key="hibernate.jdbc.batch_size">1000</prop> <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop> </props> </property> <property name="annotatedClasses"> <list> <value>com.appandmore.storefinder.backend.core.domain.offer.Offer</value> <value>com.appandmore.storefinder.backend.core.domain.store.Image</value> <value>com.appandmore.storefinder.backend.core.domain.store.Store</value> <value>com.appandmore.storefinder.backend.core.domain.store.OpeningPeriod</value> <value>com.appandmore.storefinder.backend.core.domain.store.CommunicationData</value> <value>com.appandmore.storefinder.backend.core.domain.user.Role</value> <value>com.appandmore.storefinder.backend.core.domain.user.User</value> <value>com.appandmore.storefinder.backend.core.domain.base.BusinessObject</value> </list> </property> <property name="schemaUpdate" value="${hibernate.schemaUpdate}"/> </bean> <tx:annotation-driven/> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> </beans> / *Log of the web request which leads to the exception *(user entered login data and pressed the login button) DEBUG - OpenSessionInViewFilter - Using SessionFactory 'sessionFactory' for OpenSessionInViewFilter DEBUG - OpenSessionInViewFilter - Opening single Hibernate Session in OpenSessionInViewFilter INFO - AuthenticatedSession - User trys to login with username: [email protected] Hibernate: select this_.id as id4_0_, this_.createdOn as createdOn4_0_, this_.editedOn as editedOn4_0_, this_.active as active4_0_, this_.email as email4_0_, this_.firstname as firstname4_0_, this_.lastname as lastname4_0_, this_.password as password4_0_ from User this_ where this_.email=? INFO - AuthenticatedSession - User '[email protected]' is logged in successfully. INFO - ClassCryptFactory - using encryption/decryption object org.apache.wicket.util.crypt.SunJceCrypt@7b6b89ad DEBUG - OpenSessionInViewFilter - Closing single Hibernate Session in OpenSessionInViewFilter DEBUG - OpenSessionInViewFilter - Using SessionFactory 'sessionFactory' for OpenSessionInViewFilter DEBUG - OpenSessionInViewFilter - Opening single Hibernate Session in OpenSessionInViewFilter ERROR - DefaultExceptionMapper - Unexpected error occurred org.apache.wicket.WicketRuntimeException: Can't instantiate page using constructor public com.appandmore.storefinder.backend.core.web.page.StoreTablePage() at org.apache.wicket.session.DefaultPageFactory.newPage(DefaultPageFactory.java:173) at org.apache.wicket.session.DefaultPageFactory.newPage(DefaultPageFactory.java:59) at org.apache.wicket.session.DefaultPageFactory.newPage(DefaultPageFactory.java:95) at org.apache.wicket.session.DefaultPageFactory.newPage(DefaultPageFactory.java:43) at org.apache.wicket.DefaultMapperContext.newPageInstance(DefaultMapperContext.java:107) at org.apache.wicket.request.handler.PageProvider.getPageInstance(PageProvider.java:266) at org.apache.wicket.request.handler.PageProvider.getPageInstance(PageProvider.java:160) at org.apache.wicket.request.handler.render.PageRenderer.getPage(PageRenderer.java:78) at org.apache.wicket.request.handler.render.WebPageRenderer.renderPage(WebPageRenderer.java:105) at org.apache.wicket.request.handler.render.WebPageRenderer.respond(WebPageRenderer.java:219) at org.apache.wicket.request.handler.RenderPageRequestHandler.respond(RenderPageRequestHandler.java:139) at org.apache.wicket.request.cycle.RequestCycle$HandlerExecutor.respond(RequestCycle.java:717) at org.apache.wicket.request.RequestHandlerStack.execute(RequestHandlerStack.java:63) at org.apache.wicket.request.cycle.RequestCycle.processRequest(RequestCycle.java:212) at org.apache.wicket.request.cycle.RequestCycle.processRequestAndDetach(RequestCycle.java:253) at org.apache.wicket.protocol.http.WicketFilter.processRequest(WicketFilter.java:160) at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:216) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1089) at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1089) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:365) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:712) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405) at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:211) at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139) at org.mortbay.jetty.Server.handle(Server.java:295) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:503) at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:827) at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:511) at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:210) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:379) at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:361) at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:442) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at org.apache.wicket.session.DefaultPageFactory.newPage(DefaultPageFactory.java:155) ... 36 more Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.appandmore.storefinder.backend.core.domain.user.User.roles, no session or session was closed at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383) at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375) at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368) at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111) at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186) at com.appandmore.storefinder.backend.core.web.AuthenticatedSession.getRoles(AuthenticatedSession.java:68) at org.apache.wicket.authroles.authentication.AuthenticatedWebApplication.hasAnyRole(AuthenticatedWebApplication.java:78) at org.apache.wicket.authroles.authorization.strategies.role.AbstractRoleAuthorizationStrategy.hasAny(AbstractRoleAuthorizationStrategy.java:62) at org.apache.wicket.authroles.authorization.strategies.role.annotations.AnnotationsRoleAuthorizationStrategy.isInstantiationAuthorized(AnnotationsRoleAuthorizationStrategy.java:60) at org.apache.wicket.authorization.strategies.CompoundAuthorizationStrategy.isInstantiationAuthorized(CompoundAuthorizationStrategy.java:62) at org.apache.wicket.Application$1.onInstantiation(Application.java:278) at org.apache.wicket.application.ComponentInstantiationListenerCollection$1.notify(ComponentInstantiationListenerCollection.java:36) at org.apache.wicket.application.ComponentInstantiationListenerCollection$1.notify(ComponentInstantiationListenerCollection.java:34) at org.apache.wicket.util.listener.ListenerCollection.notify(ListenerCollection.java:79) at org.apache.wicket.application.ComponentInstantiationListenerCollection.onInstantiation(ComponentInstantiationListenerCollection.java:32) at org.apache.wicket.Component.<init>(Component.java:676) at org.apache.wicket.MarkupContainer.<init>(MarkupContainer.java:117) at org.apache.wicket.Page.<init>(Page.java:211) at org.apache.wicket.Page.<init>(Page.java:175) at org.apache.wicket.markup.html.WebPage.<init>(WebPage.java:72) at com.appandmore.storefinder.backend.common.web.page.BasePage.<init>(BasePage.java:30) at com.appandmore.storefinder.backend.core.web.page.StoreTablePage.<init>(StoreTablePage.java:23) ... 41 more DEBUG - OpenSessionInViewFilter - Closing single Hibernate Session in OpenSessionInViewFilter -- View this message in context: http://apache-wicket.1842946.n4.nabble.com/Lazy-loading-exception-despite-of-OpenSessionInViewFilter-and-object-loading-within-a-request-tp3786744p3786744.html Sent from the Users forum mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
