Announcement Announcement Module
Collapse
No announcement yet.
How to do Hibernate Transactions in Spring outside of DAO Classes Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Why not let Spring do this by you. You need to move this code to a Spring bean & add @Transactional annotation to the method.

    Comment


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

      Comment


      • #4
        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

        Comment

        Working...
        X