Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: LazyInitializationException with OpenEntityManagerInViewFilter

  1. #1

    Default 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?
    applicationContext.xml.txt

    ApplicationController.java.txt

    Using:
    Freemarker
    Spring MVC 3.0.0
    EJB3

  2. #2

    Default

    more files
    Attached Files Attached Files

  3. #3

    Default

    This file drastically trimmed to fit posting constraints.
    Attached Files Attached Files

  4. #4

    Default

    also trimmed considerably
    Attached Files Attached Files

  5. #5

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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)
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7

    Default

    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.

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9

    Default

    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 at 06:03 AM.

  10. #10
    Join Date
    Oct 2008
    Posts
    26

    Default

    In case Marten responds again, I've got a question regarding his comment:
    Quote 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.)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •