Results 1 to 4 of 4

Thread: How to do Hibernate Transactions in Spring outside of DAO Classes

  1. #1
    Join Date
    Jan 2011
    Posts
    7

    Default How to do Hibernate Transactions in Spring outside of DAO Classes

    I have a block of code which includes calls to various DAO classes.

    e.g.:

    //BEGIN
    for (int i = 0; i < [..]; i++)
    {
    // ....
    daoImpl1.doDBOperation1();
    //....
    daoImpl2.doDBOperation2();
    /.....
    }
    //END


    I need to wrap this entire block in a transaction. In case of errors, all operations across the referenced DAOImpls must be rolled back.

    My question is, how do I get access to a Hibernate session in a Spring app? I need to specify that I'm starting/stopping my transaction inside my Spring app, and not in any of the classes that extend HibernateDaoSupport (the DAOs).

    Any suggestions how to accomplish this?

  2. #2
    Join Date
    May 2011
    Location
    New Delhi, India
    Posts
    157

    Default

    Why not let Spring do this by you. You need to move this code to a Spring bean & add @Transactional annotation to the method.

  3. #3
    Join Date
    Apr 2011
    Posts
    107

    Default

    Spring can manage transaction, you can read this: http://static.springsource.org/sprin...ansaction.html

  4. #4

    Default

    As mentioned above... if your class that uses the DAOs is a Spring bean and you have a Spring managed TransactionManager then you can do this:

    Code:
    <aop:config>
        <aop:pointcut id="classThatUsesDaosPointcut" expression="execution(* com.your.package.ClassThatUsesDaos.*(..))"/>
        <aop:advisor advice-ref="classThatUsesDaosTxAdvice" pointcut-ref="classThatUsesDaosPointcut"/>
    </aop:config> 
    
    <tx:advice id="classThatUsesDaosTxAdvice">
        <tx:attributes>
            <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="methodThatNeedsTransaction" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    Or via annotations:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	   xmlns:tx="http://www.springframework.org/schema/tx"
    	   xmlns:context="http://www.springframework.org/schema/context"
    	   xsi:schemaLocation=
    	       "http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    	   	http://www.springframework.org/schema/tx 	    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    	   	 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" >
                           
    <tx:annotation-driven/>
    
    ......
    
    </beans>
    Code:
    @Transactional( readOnly = true )
    public ClassThatUsesDaos {
        private YourDao yourDao;
    
        @Transactional( propagation = Propagation.REQUIRED )
        public void methodThatUsesDaos() {
            .....
        }
    
        .....
    
    }
    You can configure a lot more stuff for the transaction (e.g. rollback for exceptions, do not rollback for some other exceptions, different propagation level, etc.)

    Read the Transaction Management section of the Reference Manual (link posted above) and you'll find the answers to all your questions.

    Good luck!


    nicolas.loriente

Posting Permissions

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