MyException - 我的异常网
当前位置:我的异常网» J2EE » LazyInitializationException: could not initializ

LazyInitializationException: could not initialize proxy - the owning Session was closed

www.MyException.Cn  网友分享于:2013-11-12  浏览:2821次
dao类中生成的方法没有关闭session啊!怎么会提示Session was closed
两边的多对多配置
<set name="rights" table="rightsOfRoles" order-by="rightsId asc" > 
<key column="roleId" />
<many-to-many lazy="false"
class="com.clientRelation.pojo.Rights" 
column="rightsId" />
</set> 
<set name="roles" table="rightsOfRoles" inverse="true" >
<key column="rightsId" />
<many-to-many lazy="false"
class="com.clientRelation.pojo.Roles" 
column="roleId" />
</set>
异常:
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:60)
org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
com.clientRelation.pojo.Roles$$EnhancerByCGLIB$$f9a2bdca.getRoleName(<generated>)
com.clientRelation.action.LoginAction.doLogin(LoginAction.java:60)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:585)
org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:274)
org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:194)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
com.clientRelation.filter.SetCharacterEncoding.doFilter(SetCharacterEncoding.java:29)

==》dao类中生成的方法没有关闭session啊!怎么会提示Session was closed

------解决方案--------------------
LoginAction.java中获得session用的是什么方法,如果用的getSession(),改为openSession()试试。
------解决方案--------------------
lz用的是不是spring的getTempletHibernate().XXX(),spring好像自动关掉的,
可以用spring内置的openSessionInViewFilter解决,或者自己写个过滤器,再有你把lazy设置成类级别的试试,
总之代码没看到,只能猜啦
------解决方案--------------------
HibernateTemplate会自动关闭session的。
------解决方案--------------------
spring中的调用封装的方法,好像会自动关掉
------解决方案--------------------
XML code

<filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有