我有一个 GWt 应用程序,我正在制作一个简单的爬虫来改进我的 SEO。为此,我使用 HTMLUnit 和 Google 使用过滤器发布的简单代码。
在这个过滤器中,我需要访问数据库来加载一些数据,这就是我得到这个异常的地方。
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.tapeanding.domain.beans.TFichas.tfichasDescripcioneses, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186)
at com.tapeanding.server.op.impl.OpFicha.cargarDescripcion(OpFicha.java:223)
at com.tapeanding.server.op.impl.OpFicha.cargarFicha(OpFicha.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy38.cargarFicha(Unknown Source)
at com.tapeanding.server.servlets.CrawlServlet.generarFicha(CrawlServlet.java:198)
at com.tapeanding.server.servlets.CrawlServlet.doFilter(CrawlServlet.java:136)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1088)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360)
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:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:205)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
- web.xml
<!-- Sesion de apertura de Spring -->
<filter>
<filter-name>session-in-view</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- Filtro para convertir el codigo en HTML -->
<filter>
<filter-name>crawler</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>session-in-view</filter-name>
<servlet-name>dispatcher</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>crawler</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
- 应用程序上下文.xml
<!-- ========================= Crawler ========================================= -->
<bean id="crawler" class="com.tapeanding.server.servlets.CrawlServlet">
<property name="opFicha" ref="opFicha" />
</bean>
<bean id="opFicha" class="com.tapeanding.server.op.impl.OpFicha">
<property name="tFichasDAO" ref="tFichasDAO" />
<property name="tFichasComentariosDAO" ref="tFichasComentariosDAO" />
</bean>
<bean id="tFichasDAO" class="com.tapeanding.server.daos.impl.TFichasDAO">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="tFichasDescripcionesDAO" class="com.tapeanding.server.daos.impl.TFichasDescripcionesDAO">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
我不想使用“lazy=false”标签,因为在所有情况下我都不需要获取大量信息。
一些想法?如果我正在使用 OpenSessionInViewFilter 过滤器,当我尝试在过滤器中使用 bean 时,为什么会发生这种情况?
谢谢。