Announcement Announcement Module
Collapse
No announcement yet.
Configure transaction managment in Spring or Hibernate? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Configure transaction managment in Spring or Hibernate?

    I have an abstract base DAO that uses HibernateTemplates for data access. I'm a little confused as to where I should be configuring transaction management. Should I be configuring it with my Hibernate properties like this:
    Code:
       <bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
          <property name="properties">
             <props>
                <prop key="hibernate.dialect">net.sf.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.query.substitutions">true 1, false 0</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.use_outer_join">true</prop>
                <prop key="hibernate.cache.provider_class">net.sf.hibernate.cache.EhCacheProvider</prop>
                <prop key="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JDBCTransactionFactory</prop>
            </props>
          </property>
       </bean>
    or should I be attaching a HibernateTransactionManager to the session factory like this:
    Code:
       <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
          <property name="sessionFactory" ref="sessionFactory" />
       </bean>
    If the second method is correct will this automagically use transaction management where necessary or do I need to apply additional configurations?

  • #2
    I would use the HibernateTransactionManager.

    Comment


    • #3
      You should use the second method. This allows you to apply transactional properties consistently and declaratively. In order to manage transactions, you can use the TransactionProxyFactoryBean (see the Spring documentation).

      Here is a simple example from that part of the documentation:
      Code:
      <bean id="petStoreTarget" class="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl">
        <property name="accountDao"><ref bean="accountDao"/></property>
        <!-- Other dependencies omitted -->
      </bean>
      
      <bean id="petStore" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager" ref="transactionManager"/>
        <property name="target" ref="petStoreTarget"/>
        <property name="transactionAttributes">
          <props>
            <prop key="insert*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
          </props>
        </property>
      </bean>
      One benefit of this approach is that you can easily switch to a JTA transaction manager if you need to have XA transaction support in the future with no code change required.

      Comment


      • #4
        Can I still do this without having DAOs configured in applicationContext.xml? I have no DAOs configured because I have multiple session factories and one DAO could use any of them.

        Comment


        • #5
          Hmm, reading the documentation a little more shows that this isn't the way to handle transactions when accessing multiple databases. Any pointers on the right way to go about that?

          Comment


          • #6
            use JTA - the transactions for each DB will be enlisted with the JTA TM which will make them all commit or rollback.

            Comment


            • #7
              To use JTA for transactions across multiple databases, you need to change your transactionManager from:
              Code:
              <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
                 <property name="sessionFactory" ref="sessionFactory" />
              </bean>
              to:
              Code:
              <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>

              Comment

              Working...
              X