Announcement Announcement Module
Collapse
No announcement yet.
OpenSessionInViewFilter miss-configuration ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • OpenSessionInViewFilter miss-configuration ?

    Hi all,

    I don't manage to set Spring configuration to serialize (JSON) objects with lazy loaded member collections.
    I have set a OpenSessionInViewFilter as states this log extract :
    Code:
    11:54:09,049 DEBUG [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (http-localhost-127.0.0.1-8080-3) Resolving exception from handler [public com.c4_soft.resume.domain.Person com.c4_soft.resume.web.ResumeController.getResume(long)]: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: com.c4_soft.resume.domain.Person.emails, no session or session was closed (through reference chain: com.c4_soft.resume.domain.Person["emails"]); nested exception is org.codehaus.jackson.map.JsonMappingException: failed to lazily initialize a collection of role: com.c4_soft.resume.domain.Person.emails, no session or session was closed (through reference chain: com.c4_soft.resume.domain.Person["emails"])
    11:54:09,079 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http-localhost-127.0.0.1-8080-3) Null ModelAndView returned to DispatcherServlet with name 'resume': assuming HandlerAdapter completed request handling
    11:54:09,087 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http-localhost-127.0.0.1-8080-3) Successfully completed request
    11:54:09,092 DEBUG [org.springframework.orm.hibernate4.support.OpenSessionInViewFilter] (http-localhost-127.0.0.1-8080-3) Closing Hibernate Session in OpenSessionInViewFilter
    How comes that serializing fails because of missing session when a Session is closed in OpenSessionInViewFilter afterwards ?

    Edit: a little info about the stack I use:
    * JBoss AS 7.1.1
    * JTA
    * Spring 3.1.2

    Here is my datasource-context.xml:
    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:jdbc="http://www.springframework.org/schema/jdbc"
        xmlns:jee="http://www.springframework.org/schema/jee"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
        
        <jee:jndi-lookup id="entityManagerFactory" jndi-name="java:/resumePU"/>
        
        <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
            <property name="transactionManagerName" value="java:jboss/TransactionManager"/>
            <property name="userTransactionName" value="java:jboss/UserTransaction" />
        </bean>
        
        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="jtaTransactionManager" ref="transactionManager" />
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.connection.charSet">UTF-8</prop>
                    <!-- prop key="hibernate.connection.release_mode">after_transaction</prop -->
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                    <prop key="hibernate.generate_statistics">false</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform</prop>
                    <prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</prop>
                </props>
            </property>
        </bean>
        
    </beans>
    Last edited by ch4mp; Mar 8th, 2013, 06:22 AM.

  • #2
    Looks like I was injecting an EntityManger in my DAOs and using a SessionFilter. Stupid, isn't it ?
    Anyway, I removed sessionFactory declaration and used an OpenEntityManagerInViewFilter instead of an OpenSessionInViewFilter.
    Everything now works smoothly.

    Comment

    Working...
    X