title: Trying to destroy beans in the correct order with Spring
body: <p>I've got a web app with Spring set up to create my hibernate session factory (singleton) and session and transaction (both are request scoped), but it is destroying the session and transaction in the wrong order. How can i configure it so that the transaction is destroyed before the session? Here's my spring applicationContext.xml file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
        "http://www.springframework.org/dtd/spring-beans-2.0.dtd"&gt;
  &lt;beans&gt;
    &lt;bean id="hibernateSessionFactory" scope="singleton"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
      &lt;property name="configLocation" value="classpath:hibernate.cfg.xml" /&gt;
    &lt;/bean&gt;
  
    &lt;!-- The per-http request hibernate session --&gt;
    &lt;bean id="hibernateSession" factory-bean="hibernateSessionFactory"
      factory-method="openSession" destroy-method="close" scope="request" /&gt;
  
    &lt;!--  The per-http request transaction (i need this to be destroyed BEFORE the session) --&gt;
    &lt;bean id="hibernateTransaction" factory-bean="hibernateSession"
      factory-method="beginTransaction" destroy-method="commit" scope="request" /&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>And here's the log that shows it closing the session before it closes the transaction:</p>
  
  <pre><code>16111 [http-8080-3] DEBUG org.springframework.beans.factory.support.DisposableBeanAdapter  - Invoking destroy method 'close' on bean with name 'hibernateSession'
  16111 [http-8080-3] DEBUG org.hibernate.jdbc.ConnectionManager  - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
  16111 [http-8080-3] DEBUG com.mchange.v2.resourcepool.BasicResourcePool  - trace com.mchange.v2.resourcepool.BasicResourcePool@17e4dee [managed: 4, unused: 3, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@19a8416)
  16111 [http-8080-3] DEBUG org.springframework.beans.factory.support.DisposableBeanAdapter  - Invoking destroy method 'commit' on bean with name 'hibernateTransaction'
  16111 [http-8080-3] DEBUG org.hibernate.transaction.JDBCTransaction  - commit
  16111 [http-8080-3] WARN  org.springframework.beans.factory.support.DisposableBeanAdapter  - Invocation of destroy method 'commit' failed on bean with name 'hibernateTransaction'
  org.hibernate.SessionException: Session is closed
  </code></pre>