title: Upgrade from Spring 2.5.6 to 3.2.6 caused Spring Transaction Management to no longer work on JBoss 5.1.0 and Hibernate 3.5.6
body: <p>We were using Spring managed transactions fine with:</p>
  
  <p>JBoss 5.1.0.GA
  Spring.2.5.6
  Hibernate.3.5.6 (using natively).</p>
  
  <p>Upgraded to Spring 3.2.6 (needed to upgrade because there were some issues with JBoss 5, Spring 2.5.6 and using Autowiring for Dependency Injection, that is now solved). After the Spring upgrade, Spring managed Transaction's are no longer kicking in.</p>
  
  <p>Here is the services-context.xml file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
              http://www.springframework.org/schema/jee 
              http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
              http://www.springframework.org/schema/aop 
              http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
    &lt;context:annotation-config/&gt;
    &lt;context:component-scan base-package="com.mycompany, com.mycompany.imports"/&gt;  
  
    &lt;jee:jndi-lookup id="dataSource" jndi-name="java:/myDB"/&gt;
  
    &lt;bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;property name="configLocation" value="hibernate.cfg.xml"/&gt;
    &lt;/bean&gt;
  
    &lt;bean id="benefitsRepository" class="com.mycompany.repository.impl.BenefitsHibernateRepository"&gt;
      &lt;property name="sessionFactory" ref="hibernateSessionFactory"/&gt;
    &lt;/bean&gt;
  
    &lt;!-- ******************* Configure Transactions ************************** --&gt;
  
    &lt;bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"/&gt;
  
    &lt;aop:config&gt;
      &lt;aop:pointcut id="serviceOperation" expression="execution(* com.mycompany.service..*Service.*(..))"/&gt;
      &lt;aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/&gt;
    &lt;/aop:config&gt;
  
    &lt;tx:advice id="txAdvice" transaction-manager="txManager"&gt;
          &lt;tx:attributes&gt;
              &lt;tx:method name="get*" read-only="true"/&gt;
              &lt;tx:method name="search*" read-only="true"/&gt;
              &lt;tx:method name="*"/&gt;
          &lt;/tx:attributes&gt;
      &lt;/tx:advice&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>The Service class where we want to inject Spring Configured Transaction looks like this:</p>
  
  <pre><code>package com.mycompany.service.impl;
  
  @Service
  public class BenefitsServiceImpl implements BenefitsService
  {
      @Autowired
      protected BenefitsRepository        _benefitsRepository;    
  
      public void assignBenefitToEmployee(Employee employee, Long benefitId)
      {
          // JTA Transaction should begin here
  
          // We load persistent benefit object (proxy) no database call made yet
          // Note: Spring's HibernateTemplate is going to open Hibernate session/transaction here   
          Benefit benefit = _benefitsRepository.loadBenefit(benefitId);     
  
          // Hibernate session/transaction is already closed and further calls on proxy are causing LazyInitializationException
          // This is where Hibernate LazyInitializationException gets thrown
          benefit.getEmployees();  
  
          // JTA Transaction should end here (but obviously not the case as the hibernate session is already closed!)             
      }
  }
  </code></pre>
  
  <p>The above code worked fine with Spring 2.5.6, however after the upgrade to Spring 3.2.6 the transactions are no longer being injected. The default behavior of HibernateTemplate is to begin/end transaction when a call is made via getHibernateTempalte().get/find calls, unless a JTA or some other transaction already exists, in which case it will propagate it. </p>
  
  <p>Before the Spring upgrade, JTA transaction was present at service level, so session remained open through out the service call (in our case assignBenefitToEmployee() method call). But now since the transaction is no longer taking affect the Hibernate session is closing at the Repository level and thus causing LazyInitializationException.</p>
  
  <p>We can solve the problem easily by making following change</p>
  
  <pre><code>_benefitsRepository.loadBenefit(benefitId); // want this to work
  // change to :
  _benefitsRepository.getBenefit(benefitId); // works
  </code></pre>
  
  <p>Which obviously works, but the goal is to enable transaction at the service level. We are using this as a test case to make sure the transactions are properly configured.</p>
  
  <p>For completeness, this is what the Repository looks like:</p>
  
  <pre><code>package com.mycompany.repository.impl;
  
  public class BenefitsHibernateRepository extends HibernateDaoSupport implements BenefitsRepository
  {
  
      public Benefit loadBenefit(Integer benefitId)
      {
          return (Benefit) getHibernateTemplate().load(Benefit.class, benefitId);
      }
  }
  </code></pre>
  
  <p>Let me re-iterate, all of this worked perfectly with Spring 2.5.6. The only things that changed are:</p>
  
  <ol>
  <li>Upgrade Spring 2.5.6 -> 3.2.6 </li>
  <li>Autowire some of the beans (before we were not using any annotations/autowiring)</li>
  </ol>
  
  <p>I have also tried configuring the transactions through </p>
  
  <pre><code>&lt;context:annotation-config/&gt;
  &lt;context:component-scan base-package="com.mycompany, com.mycompany.imports"/&gt; 
  &lt;tx:annotation-driven transaction-manager="txManager"/&gt;
  </code></pre>
  
  <p>But that has not worked either. </p>
  
  <p>Also tried changing transaction type from JTA to Hibernate:</p>
  
  <pre><code>  &lt;!--  
    &lt;bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"/&gt;
    --&gt;
    &lt;bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="hibernateSessionFactory"/&gt;
    &lt;/bean&gt;
  </code></pre>
  
  <p>This has not worked either. Running out of options, any ideas??</p>
  
  <p>Here is some additional information about how our EAR is structured and how Spring is initialized:</p>
  
  <p>MyApp.ear:</p>
  
  <ul>
  <li>FrontEnd.war (Spring MVC)
  <ul>
  <li>/WEB-INF/dispatcher-servlet.xml</li>
  <li>/WEB-INF/applicationContext.xml (import's another applicationContext.xml present in EJBs.jar project)</li>
  </ul></li>
  <li>WebServices.war</li>
  <li>CoreServices.jar (Hibernate)
  <ul>
  <li>services-context.xml (The one shown above, trying to configure transactions in) </li>
  <li>beanRefContext.xml</li>
  </ul></li>
  <li>EJBs.jar (EJB 2.1)
  <ul>
  <li>/com/myapp/applicationContext.xml</li>
  </ul></li>
  </ul>
  
  <p>This is what the web.xml in FrontEnd.war looks like:</p>
  
  <pre><code> &lt;context-param&gt;
       &lt;param-name&gt;parentContextKey&lt;/param-name&gt;
       &lt;param-value&gt;myapp.ear.context&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;listener&gt;
        &lt;listener-class&gt;
          org.springframework.web.context.ContextLoaderListener
        &lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;servlet-class&gt;
              org.springframework.web.servlet.DispatcherServlet
          &lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;
                  /WEB-INF/applicationContext.xml
                  /WEB-INF/dispatcher-servlet.xml
              &lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  </code></pre>
  
  <p>Here is how the beanRefContext.xml in CoreServices.jar looks like (just realized it is still pointing to Spring 2.5.6 xsd schema's which may be an issue?)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
                          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;
  
      &lt;!-- The context files listed here should contain beans that are used by all WARs, 
           for example Services, EJB's and DAO's etc. --&gt;
      &lt;bean id="com.myapp.services-context" class="org.springframework.context.support.ClassPathXmlApplicationContext"&gt;
          &lt;constructor-arg&gt;
            &lt;list&gt;
              &lt;value&gt;services-context.xml&lt;/value&gt;
            &lt;/list&gt;
          &lt;/constructor-arg&gt;
  
  
     &lt;!-- Define an alias to treat services-context as the enterprise wide "EAR" context.
          This context is the parent ApplicationContext for the WebApplicationContexts defined in the WARs. 
          If we ever want to change this parent we simply assign this alias to another context. --&gt;
     &lt;alias name="com.myapp.services-context" alias="myapp.ear.context"/&gt;
  
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>The idea is to allow the services in CoreServices.jar be available to all the WAR's present in the EAR, that is why we are NOT initializing services-context.xml via the dispatcher-servlet in web.xml.</p>
  
  <p>Note that we have also included the following in dispatcher-servlet.xml:</p>
  
  <pre><code>  &lt;context:annotation-config/&gt;
    &lt;context:component-scan base-package="com.myapp"/&gt;
  </code></pre>
  
  <p>This is to enable annotations in our Controllers in FrontEnd.war.</p>
  
  <p>Hopefully this will give a better picture as to how Spring is initialized. Will appreciate any insights as to why Spring AOP or annotations based Transactions are not working in the CoreService.jar project. Again they used to work in Spring 2.5.6.</p>