2014-12-02 125 views
2

我有一個應用程序,我正在擴展提供一個REST API。一切工作中的主要場所罰款,但我得到的異常日誌下面當我嘗試打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) 

禁用延遲加載會解決這個問題,但是從結果不可接受的性能(加載時間200ms到22s)。我不知道如何處理這個。

我是ColdFusion中REST的新手,在我看來,CFC正在以不同尋常的方式處理。它們似乎沒有被初始化(init方法似乎沒有運行),現在看起來ORM也沒有被處理。我錯過了什麼嗎?

這裏是我的代碼生成該錯誤的摘錄:

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; 
    } 

} 

和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"; 
} 

我也注意到在標準輸出日誌休眠正在記錄的查詢,但隨後記錄「無會話」錯誤:

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'' 

我應該還可以補充說這個「item」表是屬於多對多關係的一部分,因此「collection of role:field」引用了外部表。

+0

錯誤消息與ORM。發佈你的'service.cfc'和你期望能夠工作但不適合你的代碼? – Henry 2014-12-02 19:32:06

回答

2

org.hibernate.LazyInitializationException是一個流行的hibernate問題。根本原因是您沒有打開休眠會話,因爲沒有可用的事務。 有幾種方法可以解決這個問題。 請閱讀以下鏈接:

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

或者你可以使用

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

之後4.1.6版本休眠。多見於 Solve Hibernate Lazy-Init issue with hibernate.enable_lazy_load_no_trans

+0

在4.1.6中有一個與連接泄漏相關的錯誤。最好使用4.1.7版本 – overthrown 2014-12-04 11:36:42

0

設置列表爲空

例如:

迭代對象和設置列表中每個實體爲null

entity.setList(null);

相關問題