Announcement Announcement Module
Collapse
No announcement yet.
LazyInitializationException with OpenEntityManagerInViewFilter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • LazyInitializationException with OpenEntityManagerInViewFilter

    When freemarker attempts to render contents of lazily loaded data members, it throws an exception. I've confirmed I can print the values of those same data members from the doGet() method of the ApplicationController. Will need to update this post with attachments. Could someone shed some light on what I'm doing wrong?




    Using:
    Freemarker
    Spring MVC 3.0.0
    EJB3

  • #2
    more files

    Comment


    • #3
      This file drastically trimmed to fit posting constraints.

      Comment


      • #4
        also trimmed considerably

        Comment


        • #5
          Last ones.


          Comment


          • #6
            1) Your tx:annotation-driven should be in your application context not the servlet
            2) Your locale resolver should be in your servlet xml not your application context xml
            3) You are including @Controller but you aren't EXCLUDING anything which means you have 2 instances of your services (disable the default filters in your servlet xml)

            Comment


            • #7
              Thank you for the feedback Marten. I followed your feedback, but am still observing errors. I referred to the spring docs for suggestion #3, but would like to confirm with this app-config.xml snippet that I implemented it correctly:

              Code:
              <context:component-scan base-package="com.company.app"
              	use-default-filters="false">
              	<context:include-filter type="annotation"
              		expression="org.springframework.stereotype.Controller" />
              </context:component-scan>
              Naturally, you could not have known that localeResolver was injected into languageSetterHandlerInterceptor. I had to move localeResolver back to app-config.xml, otherwise I observed this exception in the logs:

              Code:
              2010-09-26 15:50:57,818 ERROR [org.springframework.web.context.ContextLoader] (main) Context initialization failed
              org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'languageSetterHandlerInterceptor': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.web.servlet.LocaleResolver com.company.app.interceptor.LanguageSetterHandlerInterceptor.localeResolver; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.web.servlet.LocaleResolver] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
              .
              .
              The application then started without errors. But, once I entered a URL in the browser to request a page, I observed the following exception in my jboss logs:

              Code:
              2010-09-26 16:05:35,338 ERROR [org.hibernate.LazyInitializationException] (http-127.0.0.1-8080-1) failed to lazily initialize a collection of role: com.company.app.model.Page.components, no session or session was closed
              org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.company.app.model.Page.components, no session or session was closed
              	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
              	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
              	at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
              	at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
              	at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)
              	at com.company.app.ApplicationController.populatePage(ApplicationController.java:233)
              	at com.company.app.ApplicationController.doGet(ApplicationController.java:104)
              	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              	.
              	.
              Here is the method within which the exception occurs. The specific line is the start of the for loop containing the call to page.getComponents()
              Code:
              protected void populatePage(OrderablePage page,
              		OrderingSession orderingSession) {
              	for (PageComponent pageComponent : page.getComponents()) {
              		IComponentService componentService = componentServiceLocator
              				.getComponentServiceByPageComponent(pageComponent
              						.getClass());
              		if (componentService != null) {
              			componentService.populatePageComponent(pageComponent,
              					orderingSession);
              		}
              	}
              }
              Any more feedback would be appreciated.

              Comment


              • #8
                Your handlerInterceptors should also be in the servlet.xml they belong in/to the web configuration not to the general configuration of your application. But just a minor detail.

                Also how/what is your servicelocator doing?

                Another thing which you in general should't be doing is a @transactional web layer! Your service layer should be transactional not your web layer (i.e. your controller).

                Comment


                • #9
                  I Have de same problem. My architecture:

                  1 EJB3 JPA Hibernate module
                  1 Web Spring Module
                  1 EAR to publish two
                  Server JBOSS 4.2.3.GA

                  web.xml
                  Code:
                          <context-param>
                  		<param-name>contextConfigLocation</param-name>
                  		<param-value>classpath:org/springmvc/resources/spring-context.xml</param-value>
                  	</context-param>
                  
                  	<filter>
                  		<filter-name>openEntityManagerInViewFilter</filter-name>
                  		<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
                  	</filter>
                  	<filter-mapping>
                  		<filter-name>openEntityManagerInViewFilter</filter-name>
                  		<url-pattern>*.do</url-pattern>
                  	</filter-mapping>
                  
                  	<listener>
                  		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
                  	</listener>
                  	
                  	<servlet>
                  		<servlet-name>springmvcdispatcherservlet</servlet-name>
                  		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                  		<init-param>
                  			<param-name>contextConfigLocation</param-name>
                  			<param-value>classpath:org/springmvc/resources/spring-mvc.xml</param-value>
                  		</init-param>
                  		<load-on-startup>1</load-on-startup>
                  	</servlet>
                  	
                  	<servlet-mapping>
                  		<servlet-name>springmvcdispatcherservlet</servlet-name>
                  		<url-pattern>*.do</url-pattern>
                  	</servlet-mapping>
                  spring-context.xml
                  Code:
                  <tx:annotation-driven />
                  
                  <tx:jta-transaction-manager />
                  
                  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                  	<property name="persistenceUnitName" value="EJBMODULEWebPU" />
                  </bean>
                  	
                  <bean id="transactionManager"
                  		class="org.springframework.orm.jpa.JpaTransactionManager">
                  	<property name="entityManagerFactory" ref="entityManagerFactory" />
                  </bean>
                  spring-mvc.xml
                  Code:
                  <context:component-scan base-package="org.springmvc.spring" />
                  
                  <context:annotation-config />
                  
                  <mvc:annotation-driven />
                  
                  <mvc:default-servlet-handler />
                  persistence.xml ( EJB )
                  Code:
                  <persistence-unit name="EJBMODULE" transaction-type="JTA">	
                  	<provider>org.hibernate.ejb.HibernatePersistence</provider>
                  	<jta-data-source>java:MySqlDS</jta-data-source>
                  	<class>org.ejbmodule.entitys.Entidade</class>
                  	<class>org.ejbmodule.entitys.Estado</class>
                  	<class>org.ejbmodule.entitys.Cidade</class>
                  	<properties>
                  		<property name="jboss.entity.manager.factory.jndi.name" value="java:/EJBMODULE"/>
                  		<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
                  		<property name="hibernate.session_factory_name" value="java:/EJBMODULESF"/>			
                  		<property name="hibernate.transaction.factory_class" value="org.hibernate.ejb.transaction.JoinableCMTTransactionFactory"/>
                  		<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
                  		<property name="hibernate.transaction.auto_close_session" value="false"/>
                  		<property name="hibernate.connection.release_mode" value="auto"/>
                  		<property name="hibernate.connection.autocommit" value="true"/>
                  		<property name="hibernate.transaction.flush_before_completion" value="true"/>
                  		<property name="hibernate.show_sql" value="true" />
                  		<property name="hibernate.format_sql" value="true" />
                  	</properties>	
                  </persistence-unit>
                  persistence.xml (Spring)
                  Code:
                  <persistence-unit name="EJBMODULEWebPU"	transaction-type="RESOURCE_LOCAL">
                  	<provider>org.hibernate.ejb.HibernatePersistence</provider>
                  	<properties>
                  		<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
                  		<property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1:3306/developer" />
                  		<property name="hibernate.connection.username" value="root" />
                  		<property name="hibernate.connection.password" value="****" />
                  	</properties>
                  </persistence-unit>

                  ERROR [LazyInitializationException] failed to lazily initialize a collection of role: org.ejbmodule.entitys.Estado.cidades, no session or session was closed
                  org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.ejbmodule.entitys.Estado.cidades, no session or session was closed

                  any idea?

                  tanks
                  Last edited by danielfarkas; Feb 12th, 2011, 06:03 AM.

                  Comment


                  • #10
                    In case Marten responds again, I've got a question regarding his comment:
                    Originally posted by Marten Deinum View Post
                    Another thing which you in general should't be doing is a @transactional web layer! Your service layer should be transactional not your web layer (i.e. your controller).
                    Why shouldn't the web layer be transactional as well? (in addition to the service layer of course) If I have a controller accessing several service methods I normally do want a single transaction spanning those multiple service calls. So I would state the opposite: the web layer should be transactional as well.
                    (I've never liked the idea that Spring closes the tx after calling the controller method though, I would like to be able to have a single transaction spanning the whole call that means, the same tx would be used when the view gets resolved.)

                    Comment


                    • #11
                      Resolved !


                      Migrated to the WebLogic 11, and changed the EclipseLink persistence provider to prove that support for 2.1 Lazy and transactions outside the EJB container.
                      Minimum configuration, good.
                      following configuration:

                      web.xml
                      Code:
                      <filter>
                      		<filter-name>openEntityManagerInViewFilter</filter-name>
                      		<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
                      	</filter>
                      	<filter-mapping>
                      		<filter-name>openEntityManagerInViewFilter</filter-name>
                      		<url-pattern>*.do</url-pattern>
                      	</filter-mapping>
                      	
                      	<context-param>
                      		<param-name>contextConfigLocation</param-name>
                      		<param-value>classpath:org/springmvc/resources/spring-context.xml</param-value>
                      	</context-param>
                      
                      	<listener>
                      		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
                      	</listener>
                      
                      	<servlet>
                      		<servlet-name>springmvcdispatcherservlet</servlet-name>
                      		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                      		<init-param>
                      			<param-name>contextConfigLocation</param-name>
                      			<param-value>classpath:org/springmvc/resources/spring-mvc.xml</param-value>
                      		</init-param>
                      		<load-on-startup>1</load-on-startup>
                      	</servlet>
                      	
                      	<servlet-mapping>
                      		<servlet-name>springmvcdispatcherservlet</servlet-name>
                      		<url-pattern>*.do</url-pattern>
                      	</servlet-mapping>
                      persistence.xml
                      Code:
                      <persistence-unit name="EJBPROJECT">
                      		<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
                      		<jta-data-source>jdbc/mySQL</jta-data-source>
                      		<class>org.ejbproject.entitys.Entidade</class>
                      		<class>org.ejbproject.entitys.Estado</class>
                      		<class>org.ejbproject.entitys.Cidade</class>
                      		<exclude-unlisted-classes>true</exclude-unlisted-classes>
                      		<properties>			
                      			<property name="show_sql" value="true" />
                      		</properties>
                      	</persistence-unit>
                      spring-context.xml
                      Code:
                      <context:load-time-weaver
                      		weaver-class="org.springframework.instrument.classloading.weblogic.WebLogicLoadTimeWeaver" />
                      
                      	<bean id="transactionManager"
                      		class="org.springframework.transaction.jta.JtaTransactionManager" />
                      
                      	<bean id="entityManagerFactory"
                      		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                      		<property name="persistenceUnitName" value="EJBPROJECT" />
                      		<property name="jpaVendorAdapter">
                      			<bean id="jpaAdapter"
                      				class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
                      				<property name="databasePlatform"
                      					value="org.eclipse.persistence.platform.database.MySQLPlatform" />
                      				<property name="showSql" value="true" />
                      			</bean>
                      		</property>
                      	</bean>
                      spring-mvc.xml
                      Code:
                       <tx:annotation-driven />
                      
                      	<tx:jta-transaction-manager />
                      
                      	<context:component-scan base-package="org.springmvc.spring" />
                      
                      	<context:annotation-config />
                      
                      	<mvc:annotation-driven />
                      
                      	<mvc:default-servlet-handler />
                      The Lazy collections open in view, and any change in the Spring @Service layer bean annotated with @Transaction instantly reflected in the database.

                      The web module there is nothing persistence, everything is in EJB.

                      JBoss 5 or 6 is J2SE server, not a JEE server.

                      Tanks.
                      Last edited by danielfarkas; Feb 15th, 2011, 02:22 AM.

                      Comment

                      Working...
                      X