3

スタックトレース自体にフィルターが表示されているにもかかわらず、Spring / MVC 3.0 /Hibernate3.5アプリケーションで次のLazyInitializationExceptionがランダムに発生しているようです。私が何を調べるべきかについて何か考えはありますか?

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)

から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="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>
4

2 に答える 2

6

フィルタをオンにした遅延読み込み例外について私が知っている2つの最も一般的な原因は、例外がHibernateセッションを無効にした後に何かにアクセスしようとしたか、 Webセッション上に実際に存在していない何かのフィールドにアクセスしようとしたことです。 tが添付されています。

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

明らかに、明確にするために多くのコードと注釈が省略されています:)

@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";
  }
}
于 2011-06-07T17:37:16.150 に答える
2

春の MVC では、フィルターの代わりに OpenSessionInViewInterceptor を使用する必要があります。現在のプロジェクトでは、次のように構成されています。

<mvc:annotation-driven/>
<mvc:interceptors>
<bean class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
</mvc:interceptors>

sessionFactory が参照する場所org.springframework.orm.hibernate3.LocalSessionFactoryBean

完璧に動作します。

于 2011-06-07T16:48:16.877 に答える