尽管有OpenSessionInViewFilter,但LazyInitializationException [英] LazyInitializationException in spite of OpenSessionInViewFilter

查看:118
本文介绍了尽管有OpenSessionInViewFilter,但LazyInitializationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎在Spring / MVC 3.0 / Hibernate 3.5应用程序中随机获取以下LazyInitializationException,尽管在堆栈跟踪本身中看到了过滤器。任何想法我应该看看?

  2011年6月7日13:48:47,152 [错误](http-3443-2 )org.hibernate.LazyInitializationException:无法初始化代理 - 没有会话
org.hibernate.LazyInitializationException:无法初始化代理 - 没有会话
在org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:140)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at com.test.Image _ $$ _ javassist_18.getMyKey(Image _ $$ _ javassist_18.java)$ b $ at com.test.AppTagHelper.getAssetUrl(AppTagHelper.java:66)
at sun.reflect.GeneratedMethodAccessor81 .invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
在org .apache.el.parser.AstFunction.getValue(AstFunction.java:110)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
at org.apache.jasper.runtime .PageContextImpl.proprietaryEvaluate(PageContextImpl.java:935)
at org.apache.jsp.WEB_002dINF.view.home_jsp._jspx_meth_c_005fout_005f2(home_jsp.java:1027)
at org.apache.jsp.WEB_002dINF.view .home_jsp._jspx_meth_c_005fwhen_005f1(home_jsp.java:1002)
位于org.apache.jsp.WEB_002dINF.view.home_jsp._jspx_meth_c_005fchoose_005f1(home_jsp.java:969)
位于org.apache.jsp.WEB_002dINF.view .home_jsp._jspx_meth_display_005fcolumn_005f0(home_jsp.java:867)
at org.apache.jsp.WEB_002dINF.view.home_jsp._jspService(home_jsp.java:214)
<<<<各种弹簧过滤器>>>
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)

web.xml

 < filter> ; 
< filter-name> hibernateFilter< / 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> hibernateFilter< / filter-name>
< url-pattern> *。html< / url-pattern>
< / filter-mapping>
< filter-mapping>
< filter-name> hibernateFilter< / filter-name>
< url-pattern> *。json< / url-pattern>
< / filter-mapping>

更新,添加定义 SessionFactoryBean

 < bean id =sessionFactoryclass =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBeandepends-on = 数据源 > 
< property name =dataSourceref =dataSource/>
< property name =packagesToScanvalue =com.test.model/>
< property name =schemaUpdatevalue =false/>
< property name =eventListeners>
< map>
<! - - 创建 - >
< entry key =pre-insert>
< ref local =hibernateCreateListener/>
< / entry>
< entry key =post-insert>
< ref local =hibernateRevisionListener/>
< / entry>
<! - 更新 - >
< entry key =pre-update>
< ref local =hibernateUpdateListener/>
< / entry>
< entry key =post-update>
< ref local =hibernateRevisionListener/>
< / entry>
< entry key =post-delete>
< ref local =hibernateRevisionListener/>
< / entry>
< entry key =pre-collection-update>
< ref local =hibernateRevisionListener/>
< / entry>
< entry key =post-collection-recreate>
< ref local =hibernateRevisionListener/>
< / entry>
< entry key =pre-collection-remove>
< ref local =hibernateRevisionListener/>
< / entry>
< / map>
< / property>
< property name =entityInterceptor>
< ref bean =hibernateAuditInterceptor/>
< / property>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> org.hibernate.dialect.Oracle10gDialect< / prop>
< prop key =hibernate.default_schema> $ {app.databaseSchema}< / prop>
< prop key =hibernate.show_sql> false< / prop>
< prop key =hibernate.format_sql> false< / prop>
< prop key =hibernate.use_sql_comments> false< / prop>
< prop key =hibernate.connection.oracle.jdbc.ReadTimeout> 60000< / prop>
< prop key =org.hibernate.envers.revisionTypeFieldName> REV_TYPE< / prop>
< prop key =org.hibernate.envers.revisionFieldName> REV_ID< / prop>
< /道具>
< / property>
< / bean>


解决方案

在过滤器打开时,要么尝试访问某个异常后使 Hibernate 会话失效,要么尝试访问实际位于 Web 会话中的某个字段并没有附加。

  public interface EntityService {

@Transactional
EntityA getA (Long id);

@Transactional
EntityB getB(Long id);

$ b $ public class WebPageController {

public void handleGet(Long id1,Long id2){
EntityA a = entityService.getA(id1);
尝试{
EntityB b = entityService.getB(id2);
} catch(Exception e){
// print somthething
}
a.accessLazyField(); //在getB抛出异常后抛出延迟加载




显然)

  @SessionAttributes(model)
public class WebPageController {

@ModelAttribute(model)
@RequestMapping(method = RequestMethod.GET)
public EntityA handleGet(Long id){
return entityService.getA(id) ;


@RequestMapping(method = RequestMethod.POST)
public String handlePost(@ModelAttribute(model)EntityA a){
a.accessLazyField() ; //如果在原始页面渲染过程中未访问该字段,则会抛出延迟加载
returnviewName;
}
}


I seem to be randomly getting the following LazyInitializationException in a Spring/MVC 3.0/Hibernate 3.5 application in spite of seeing the filter in the stack trace itself. Any idea on what I should look into?

07 Jun 2011 13:48:47,152 [ERROR]  (http-3443-2) org.hibernate.LazyInitializationException: could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:86)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:140)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
    at com.test.Image_$$_javassist_18.getMyKey(Image_$$_javassist_18.java)
    at com.test.AppTagHelper.getAssetUrl(AppTagHelper.java:66)
    at sun.reflect.GeneratedMethodAccessor81.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.el.parser.AstFunction.getValue(AstFunction.java:110)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
    at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:935)
    at org.apache.jsp.WEB_002dINF.view.home_jsp._jspx_meth_c_005fout_005f2(home_jsp.java:1027)
    at org.apache.jsp.WEB_002dINF.view.home_jsp._jspx_meth_c_005fwhen_005f1(home_jsp.java:1002)
    at org.apache.jsp.WEB_002dINF.view.home_jsp._jspx_meth_c_005fchoose_005f1(home_jsp.java:969)
    at org.apache.jsp.WEB_002dINF.view.home_jsp._jspx_meth_display_005fcolumn_005f0(home_jsp.java:867)
    at org.apache.jsp.WEB_002dINF.view.home_jsp._jspService(home_jsp.java:214)
    <<VARIOUS SPRING FILTERS>>
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)

From the web.xml:

<filter>
    <filter-name>hibernateFilter</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>hibernateFilter</filter-name>
    <url-pattern>*.html</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>*.json</url-pattern>
</filter-mapping>

Update, adding definition of SessionFactoryBean:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" depends-on="dataSource">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.test.model" />
    <property name="schemaUpdate" value="false" />
    <property name="eventListeners">
      <map>
        <!-- Create -->
        <entry key="pre-insert">
          <ref local="hibernateCreateListener"/>
        </entry>
        <entry key="post-insert">
          <ref local="hibernateRevisionListener"/>
        </entry> 
        <!-- Update -->
        <entry key="pre-update">
          <ref local="hibernateUpdateListener"/>
        </entry>
        <entry key="post-update">
          <ref local="hibernateRevisionListener"/>
        </entry>
        <entry key="post-delete">
          <ref local="hibernateRevisionListener"/>
        </entry>
        <entry key="pre-collection-update">
          <ref local="hibernateRevisionListener"/>
        </entry>
        <entry key="post-collection-recreate">
          <ref local="hibernateRevisionListener"/>
        </entry>
        <entry key="pre-collection-remove">
          <ref local="hibernateRevisionListener"/>
        </entry>                   
      </map>
    </property>
    <property name="entityInterceptor">
      <ref bean="hibernateAuditInterceptor"/>
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
        <prop key="hibernate.default_schema">${app.databaseSchema}</prop>                 
        <prop key="hibernate.show_sql">false</prop>
        <prop key="hibernate.format_sql">false</prop>
        <prop key="hibernate.use_sql_comments">false</prop>
        <prop key="hibernate.connection.oracle.jdbc.ReadTimeout">60000</prop>
        <prop key="org.hibernate.envers.revisionTypeFieldName">REV_TYPE</prop>
        <prop key="org.hibernate.envers.revisionFieldName">REV_ID</prop>        
      </props>
    </property>
  </bean>

解决方案

Two most common causes I know of for lazy load exceptions with the filter on are either trying to access something after an exception has invalidated the Hibernate Session, or trying to access a field on something that was actually sitting around on the Web session and isn't attached.

public interface EntityService {

  @Transactional
  EntityA getA(Long id);

  @Transactional
  EntityB getB(Long id);
}

public class WebPageController {

  public void handleGet(Long id1, Long id2) {
    EntityA a = entityService.getA(id1);
    try {
      EntityB b = entityService.getB(id2);
    } catch (Exception e) {
      //print somthething
    }
    a.accessLazyField(); //will throw lazy load after getB throws exception
  }
}

Obviously lots of code and annotations ommitted for clarity :)

@SessionAttributes("model")
public class WebPageController {

  @ModelAttribute("model")
  @RequestMapping(method=RequestMethod.GET)
  public EntityA handleGet(Long id) {
    return entityService.getA(id);
  }

  @RequestMapping(method=RequestMethod.POST)
  public String handlePost(@ModelAttribute("model") EntityA a) {
    a.accessLazyField(); //will throw lazy load if the field was not accessed during original page rendering
    return "viewName";
  }
}

这篇关于尽管有OpenSessionInViewFilter,但LazyInitializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆