Results 1 to 7 of 7

Thread: Configure transaction managment in Spring or Hibernate?

  1. #1
    Join Date
    Dec 2005
    Location
    London
    Posts
    22

    Default 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. #2
    Join Date
    Aug 2004
    Posts
    1,104

    Default

    I would use the HibernateTransactionManager.
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  3. #3
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,840

    Default

    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.

  4. #4
    Join Date
    Dec 2005
    Location
    London
    Posts
    22

    Default

    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.

  5. #5
    Join Date
    Dec 2005
    Location
    London
    Posts
    22

    Default

    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?

  6. #6
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    use JTA - the transactions for each DB will be enlisted with the JTA TM which will make them all commit or rollback.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  7. #7
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,840

    Default

    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"/>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •