user1896298
user1896298

Reputation: 43

failed to lazily initialize a collection ..., could not initialize proxy - no Session

I have an application that i am extending to provide a REST API. Everything works fine in the main site, but I am getting the following in the exception log when I try to hit the REST API:

"Error","ajp-bio-8014-exec-3","12/02/14","12:54:06","table","failed to lazily initialize a collection of role: field, could not initialize proxy - no Session The specific sequence of files included or processed is: service.cfc'' "
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: field, could not initialize proxy - no Session
    at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:566)
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)
    at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:137)
    at org.hibernate.collection.internal.PersistentBag.size(PersistentBag.java:242)
    at coldfusion.runtime.xml.ListIndexAccessor.getSize(ListIndexAccessor.java:44)
    at coldfusion.runtime.xml.ArrayHandler.serialize(ArrayHandler.java:69)
    at coldfusion.runtime.xml.CFComponentHandler.serialize(CFComponentHandler.java:106)
    at coldfusion.runtime.XMLizerUtils.serializeXML(XMLizerUtils.java:83)
    at coldfusion.rest.provider.CFObjectProvider.writeTo(CFObjectProvider.java:378)
    at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1479)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
    at coldfusion.rest.servlet.CFRestServletContainer.service(CFRestServletContainer.java:141)
    at coldfusion.rest.servlet.CFRestServletContainer.service(CFRestServletContainer.java:86)
    at coldfusion.rest.servlet.CFRestServlet.serviceUsingAlreadyInitializedContainers(CFRestServlet.java:556)
    at coldfusion.rest.servlet.CFRestServlet.invoke(CFRestServlet.java:434)
    at coldfusion.rest.servlet.RestFilter.invoke(RestFilter.java:58)
    at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:94)
    at coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28)
    at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38)
    at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22)
    at coldfusion.rest.servlet.CFRestServlet.invoke(CFRestServlet.java:409)
    at coldfusion.rest.servlet.CFRestServlet.service(CFRestServlet.java:400)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at coldfusion.monitor.event.MonitoringServletFilter.doFilter(MonitoringServletFilter.java:42)
    at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:422)
    at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:198)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Disabling lazy loading will fix this, but results in unacceptable performance (load times from 200ms to 22s). I'm not sure how else to handle this.

I am new to REST in ColdFusion, and it seems to me that the CFC's are being handled in an unusual way. They do not appear to be initialized (init method does not seem to run) and now it seems that ORM is not handled the same either. Am I missing something?

Here is the excerpt of my code producing this error:

component rest="true" restpath="item" 
{
    import model.beans.*;

    remote item function getitem( numeric id restargsource="Path" ) restpath="{id}" httpmethod="GET"
    {
        var item = entityLoad("item",{ id = id },true);

        return item;
    }

}

And the bean:

component persistent="true" table="item" output="false" extends="timestampedBean" batchsize="10" cacheuse="read-only"
{
    /* properties */

    property name="id" column="id" type="numeric" ormtype="int" fieldtype="id" generator="identity"; 
    property name="title" column="title" type="string" ormtype="string"; 
    property name="description" column="description" type="string" ormtype="string"; 
    property name="status" column="status" type="numeric" ormtype="byte" default="0" ; 
    property name="user" fieldtype="many-to-one" cfc="user" fkcolumn="userid" inversejoincolum="userid" lazy="true" cacheuse="read-only";
    property name="field" type="array" fieldtype="many-to-many" cfc="field" fkcolumn="id" linktable="items_fields" inversejoincolumn="fieldid" lazy="extra" batchsize="10" cacheuse="read-only";
}

I also noticed in the stdout log that Hibernate is logging the query, but then it logs the "No session" error:

Hibernate: 
    select
        item0_.id as id0_0_,
        item0_.dtcreated as dtcreated0_0_,
        item0_.dtmodified as dtmodified0_0_,
        item0_.title as title0_0_,
        item0_.description as descript6_0_0_,
        item0_.status as status0_0_,
        item0_.userid as userid0_0_ 
    from
        item item0_ 
    where
        item0_.id=?
Dec 2, 2014 15:23:00 PM Error [ajp-bio-8014-exec-3] - failed to lazily initialize a collection of role: field, could not initialize proxy - no Session The specific sequence of files included or processed is: service.cfc''

I should probably also add that this "item" table is part of a many-to-many relationship, so "collection of role: field" is referencing the foreign table.

Upvotes: 2

Views: 4999

Answers (2)

jose calles
jose calles

Reputation: 1

set the list to null

example:

iterate the objects and set list every entity to null

entity.setList(null);

Upvotes: 0

overthrown
overthrown

Reputation: 131

org.hibernate.LazyInitializationException is a popular hibernate problem. The root cause is that you have no hibernate Session opened, because of no transaction available. There are several approaches to overcome the issue. Please, read following links:

http://www.javacodegeeks.com/2012/07/four-solutions-to-lazyinitializationexc_05.html http://javarevisited.blogspot.ru/2014/04/orghibernatelazyinitializationException-Could-not-initialize-proxy-no-session-hibernate-java.html

Or you can use

<property name="hibernate.enable_lazy_load_no_trans" value="true"/>

after 4.1.6 hibernate version. See more at Solve Hibernate Lazy-Init issue with hibernate.enable_lazy_load_no_trans

Upvotes: 2

Related Questions