views:

440

answers:

0

Hello, I have some problems in implementing the OpenSessionInView pattern. From various resources on the web i configured my application in this way:
1) in my dispatcher-servlet.xml I have an interceptor that should get all my requests with a spring class OpenSessionInViewInterceptor:

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="interceptors">
         <list>
              <ref bean="openSessionInViewInterceptor"/>
         </list>
       </property>
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean name="openSessionInViewInterceptor"  class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
        <property name="sessionFactory">
            <ref bean="sessionFactoryAnnotation"/>
        </property>
    </bean>

2) then I have configured transactions in my Service layer with AOP in this way:

<aop:config>
        <aop:pointcut expression="execution(* it.jdk.crm.service..*Service.*(..))" id="gestioneComunicazioniOperation"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="gestioneComunicazioniOperation"/>
    </aop:config>

    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
<bean class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="txManager">
        <property name="sessionFactory">
            <ref local="sessionFactoryAnnotation" />
        </property>
    </bean>
<bean class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="txManager">
        <property name="sessionFactory">
            <ref local="sessionFactoryAnnotation" />
        </property>
    </bean>

Is it possible that the two configurations are in conflict? I have a requirement that my Service layer (used in portlets) should be transaction-bounded, is it in conflict with the OpenSessionInView? If it is, then how can I prevent LazyInitializationException errors in my controllers?

EDIT: the exception is occurring in the view, when i call

<c:forEach items="${communication.questions}" var="question" >

with exception

ERROR [jsp:165] org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: it.*.Communication.questions, no session or session was closed

and the code in the controller that calls the view is like:

@RequestMapping(params="action=dettagli")
    public ModelAndView dettagliComunicazione(@ModelAttribute("communication") Communication c) {
        Communication communication = null;
        try {
            communication = communicationService.findById(c.getId());
        } catch (Exception ex) {
        }
        Hashtable l = new Hashtable();
        l.put("communication", communication);
        return new ModelAndView("communication/details", l);

    }

that gets resolved in a file by the bean in dispatcher-servlet.xml:

<bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />