Dependent rollback among different DAO implementation

768 views Asked by At

I am currently working on a spring mvc + hibernate application. It has Service and DAO layer.

What I want to know is is there a way to achieve dependent rollback among different DAO implementation.

for example is I add a entry/ row in table A mapped with Model A by using DAO implementation of A. then I am trying to do same for Table B. However while trying to same for Table C an error occurs then I wish to rollback both the changes done in Table A and Table B.

Is there a way to achieve this, Let me know any help is appreciated and thank you in advance :D

NOTE to Moderators : I know this question may sound to opinionated but be assured if I don't find any satisfactory answer in few days I will close it myself.

2

There are 2 answers

0
Ramanujan R On BEST ANSWER

Obviously, you will call all these dao methods from your service class. Make that service method/class transactional. Annotate the service method/class with @Transactional.

  1. This will create a new Hibernate session at the start of method execution.
  2. Will use that session for all your db transaction in dao layer.
  3. Commits/Rollback all those db transactions at the end.

If there is any Runtime exception occures, during your Table C modification, rollback will be done in Tables A & B, automatically.

Refer these

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html http://blog.jhades.org/how-does-spring-transactional-really-work/

0
2787184 On

Yes,you can rollback all transactions, Please find below snippet, from serviceMethod you are calling all dto methods, in case of exception all the transaction will be rollback.

Note: You have to specify for which exception you want rollback. for runtime exception transaction will not be rollback. spring DTO throwing DataAccessException which is a runtime exception and use proper implementation of transactionManager.

@Service
    public class ServiceClass{

        @Autowire
        private Repository repo;

        @Transactional(rollbackFor={XYZ.class}, propagation=Propagation.REQUIRES_NEW, isolation=Isolation.SERIALIZABLE)
        public void serviceMethod(){
            repo.insertIntoA();
            repo.insertIntoB();
            repo.insertIntoC();
        }
    }

XML configuration:

<bean id="transactionManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!--  Thread safe, singletone , Hibernate 3 only -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource"  ref="dataSource"/>
        <property name="mappingResources">
            <array>
                <value>com/domain/Customer-hbm.xml</value>
            </array>
        </property>
        <property name="hibernateProperties">
            <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
                    <prop key="hibernate.show_Sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>        
            </props>
        </property>
    </bean>