title: Hibernate spring annotation sessions not being closed/flushed
body: <p>I've 'inherited' a project which uses Spring annotations to manage transactions/sessions with Hibernate. Or at least it's meant to be. Currently the Hibernate sessions never get flushed (they are set to FLUSH_MODE_NEVER) and the DAO's need to be flushed by hand for any data to be written to the database.</p>
  
  <p>Also all the DTO objects stay resident in hibernate's memory, eventually leading to an OutOfMemory error.</p>
  
  <p>I believe I need to tell Spring/Hibernate to close the session or commit the transaction. In my controller class I have the annotated method for handling the requests:</p>
  
  <pre><code>@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
      ...
      ...
  }
  </code></pre>
  
  <p>and in the applicationContetxt.xml file I believe I setup the hibernate transaction manager and tell spring to use the annotations:</p>
  
  <pre><code>&lt;!-- hibernate3 transaction manager --&gt;
  &lt;bean id="transactionManagerLocal" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="dataSource" ref="${local.data.source}" /&gt;
      &lt;property name="sessionFactory" ref="localSessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Demarcate using @Transactional annotation --&gt;
  &lt;tx:annotation-driven transaction-manager="transactionManagerLocal" order="200" /&gt;
  </code></pre>
  
  <p>Not only am I pretty sure that the config is wrong by the way data doesn't get written to the DB without manually calling flush on each of the DAO's, from the log file we can see that transaction manager has it's flushing and session closing disabled:</p>
  
  <pre><code>INFO  [http-8080-2] TransactionFactoryFactory - Transaction strategy: org.springframework.orm.hibernate3.SpringTransactionFactory
  INFO  [http-8080-2] TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
  INFO  [http-8080-2] SettingsFactory - Automatic flush during beforeCompletion(): disabled
  INFO  [http-8080-2] SettingsFactory - Automatic session close at end of transaction: disabled
  </code></pre>
  
  <p>What needs to be done to make Spring/hibernate automatically flush the DAOs and/or close the session to prevent huge amounts of memory being used by Hibernate?</p>
  
  <p>cheers
  Dan</p>
  
  <pre><code>&lt;!-- MySQL/InnoDB session factory --&gt;
  &lt;bean id="localSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="${local.data.source}"/&gt;
      &lt;property name="mappingResources"&gt;
          &lt;list&gt;
              &lt;value&gt;net/company/projectname/domain/ExchangeRate.hbm.xml&lt;/value&gt;
              &lt;!-- Other  --&gt;
  
          &lt;/list&gt;
      &lt;/property&gt;
  
     &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLInnoDBDialect&lt;/prop&gt;
              &lt;prop key="hibernate.cache.provider_class"&gt;org.hibernate.cache.NoCacheProvider&lt;/prop&gt;
              &lt;prop key="hibernate.connection.zeroDateTimeBehavior"&gt;convertToNull&lt;/prop&gt;
              &lt;!-- 
              DOES NOTHING
              &lt;prop key="hibernate.transaction.flush_before_completion"&gt;true&lt;/prop&gt;
              &lt;prop key="hibernate.transaction.auto_close_session"&gt;true&lt;/prop&gt;
              --&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;!-- hibernate3 transaction manager --&gt;
  &lt;bean id="transactionManagerLocal" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="dataSource" ref="${local.data.source}" /&gt;
      &lt;property name="sessionFactory" ref="localSessionFactory" /&gt;
  &lt;/bean&gt;
  
  
  &lt;!-- Demarcate using @Transactional annotation --&gt;
  &lt;tx:annotation-driven transaction-manager="transactionManagerLocal" order="200" /&gt;
  
  &lt;bean id="exchangeRateDAO" class="net.company.project.dao.hibernate.impl.ExchangeRateDAOImpl"&gt;
       &lt;property name="sessionFactory" ref="localSessionFactory"/&gt;
   &lt;/bean&gt;     
  </code></pre>