Results 1 to 4 of 4

Thread: One Transaction for each DAO

  1. #1
    Join Date
    Jan 2007
    Posts
    4

    Unhappy One Transaction for each DAO

    Hello!, I have a problem with my Transaction. The problem is that Spring is generating one Transaction per Dao and I can't rollback the changes in an previous insert or update when an error ocurred.

    My definition is:

    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    
        <import resource="dataSourceFactoryBeans.xml"></import>
    
        <bean id="dataSource" class="javax.sql.DataSource" factory-bean="portalDataSourceFactory" factory-method="getDataSource">
        </bean>
    
        <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="dataSource">
                <ref bean="dataSource"/>
            </property>
            <property name="sessionFactory"><ref local="portalSessionFactory"/></property>
        </bean>
    
        <bean id="abstractSessionFactory" abstract="true" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="mappingResources">
               <list>
                    <value>com/tonline/albura/osiris/business/hibernate/Accion.hbm.xml</value>
                    <value>com/tonline/albura/osiris/business/hibernate/CoberturaRed.hbm.xml</value>
              </list>
            </property>
        </bean>
    
        <bean id="portalSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" parent="abstractSessionFactory">
            <property name="hibernateProperties">
              <props>
                  <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
                  <prop key="hibernate.jdbc.batch_size">0</prop>
                  <prop key="hibernate.jdbc.use_streams_for_binary">true</prop>
                  <prop key="hibernate.connection.SetBigStringTryClob">false</prop>
                  <prop key="hibernate.cache.use_query_cache">false</prop>
                  <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
                  <prop key="hibernate.show_sql">true</prop>
              </props>
            </property>
        </bean>
    
        <bean id="hibernateDao" abstract="true" class="com.ya.telco.dataAccessTools.HibernateDAO">
            <property name="dataSource">
                <ref bean="dataSource"></ref>
            </property>
            <property name="component">
                <ref bean="component"></ref>
            </property>
            <property name="sessionFactory"><ref local="portalSessionFactory"/></property>
        </bean>
    
        <bean name="proxyHibernate" abstract="true"
            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager">
                <ref bean="hibernateTransactionManager"/>
            </property>
            <property name="transactionAttributes">
                <props>
                    <prop key="*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>
    
        <bean name="coberturaRedHib" class="com.tonline.albura.osiris.business.hibernate.CoberturaRed" singleton="false">
            <property name="coberturaRedDAOHib">
                <ref bean="coberturaRedDAOHib"></ref>
            </property>
        </bean>
        <bean name="coberturaRedDAOHib" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
            parent="proxyHibernate" lazy-init="true">
            <property name="target">
                <bean class="com.tonline.albura.osiris.dao.hibernate.CoberturaRedDAOImpl" parent="hibernateDao"/>
            </property>
        </bean>
    
        <bean name="component" class="com.app.second.osiris.OsirisComponent"></bean>
    
    
    </beans>
    Can I have the same Transaction for all the DAO operations??. I need to Rollback a previous DAO operation when the actual DAO operation crash a throws an Exception.

    Thanks

    pd: I speak English a little (I´m Spanish )

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

    If you are proxying your Dao with TransactionProxyFactoryBean then all transactions will be at the Dao level. What your would need to do is proxy something higher up the stack. It is typical to apply transactions at the service level.

    Current
    Code:
    Do something with dao.
    [Proxy]
        [TransactionProxyFactoryBean]
            [New Transaction]
                [Do stuff in Dao]
            [End Transaction]
        [End]
    [End]
    
    Do something else with dao.
    [Proxy]
        [TransactionProxyFactoryBean]
            [New Transaction]
                [Do stuff in Dao]
            [End Transaction]
        [End]
    [End]
    What you want is.
    Code:
    Do something with service.
    [Proxy]
        [TransactionProxyFactoryBean]
            [New Transaction]
                [Do stuff in Dao]
                [Do stuff in Dao]
            [End Transaction]
        [End]
    [End]

  3. #3
    Join Date
    Jan 2007
    Posts
    4

    Default

    Thanks, I just create beans at service level with the TransactionProxy asociations and this run corretly.

  4. #4
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

    Glad it solved your problem! It usually makes sense to provide transactions at the service layer anyway.

    But the Service Layer is still very usefull: it is a gateway to the internals of your system and this makes it an excelent place to coordinate (crosscutting) concerns like transactions and security.
    http://peter.jteam.nl/?p=17

Posting Permissions

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