Announcement Announcement Module
Collapse
No announcement yet.
Transaction Management Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transaction Management

    Hi,

    I desperately need help.
    Code:
    public class B{
    @Transactional(propagation = Propagation.REQUIRED)
      insert(){
        //inserts into DB.
      }
    }
    
    public class A{
    @Transactional(propagation = Propagation.REQUIRED)
          public void add(){
                B.insert(); // DAO class1
                B.insert(); //DAO Class2 …………………Got exception.
    
          } 
       .
       .
    }
    ----->Need to rollback ‘insert() of DAO class1’.

    Please give me some ideas. Thanks.
    Last edited by hnarayanappa; Dec 2nd, 2010, 03:09 AM.

  • #2
    Use [ code][/code ] tags when posting code.

    If you configured tx management correctly rollback is happening automatically. If you use MySQL make sure you use InnoDB tables instead of MyISAM tables (the latter don't support transactions).

    Comment


    • #3
      This is already achieved with your code. With propagation required set on insert() of class B, the second insert in class A's add() failing means that add() cannot commit. Since A's add() has propagation required as well, it will TRY to commit but will rollback instead and a UnexpectedRollbackException will be thrown, which you can handle properly.

      Please refer to the official documentation, page 317.

      Comment


      • #4
        Transaction Management

        Hi,

        Thank you so much for the immediate reply.

        We are using InnoDB of MySql. Still I could not achieve the rollback.

        The issue is, The first insert() inserts a value to a column specified as primary key.
        When we insert the same value to the second insert(), it tries to insert this duplicate value to the same column, for this method we get Integrity Exception, At this situation the first insert() has already been committed.
        We would like to achieve a rollback to the first insert() when a Runtime Exception occurs.
        This is not happening here, I'm still not sure if I'm missing something.
        Please do help me.

        Thanks so much, will be waiting for your response.
        Last edited by hnarayanappa; Dec 2nd, 2010, 12:07 AM.

        Comment


        • #5
          As stated if you configured you tx management correctly the rollback should happen. It doesn't happen so your configuration is wrong/flawed. However you don't post your configuration...

          Comment


          • #6
            Thank again, here's my configuration:-
            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:aop="http://www.springframework.org/schema/aop"
                xmlns:jee="http://www.springframework.org/schema/jee"
            	xmlns:tx="http://www.springframework.org/schema/tx"
                xsi:schemaLocation="
                   		http://www.springframework.org/schema/beans 
                   		http://www.springframework.org/schem...-beans-2.0.xsd
                   		http://www.springframework.org/schema/aop
                		http://www.springframework.org/schem...ng-aop-2.0.xsd
                   		http://www.springframework.org/schema/jee 
                   		http://www.springframework.org/schem...ng-jee-2.0.xsd
                   		http://www.springframework.org/schema/tx
                		http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
            
            
            	<!-- Tell Spring the we declare transactions using annotations in java -->
            	<tx:annotation-driven transaction-manager="transactionManager"/>
            
            	<!-- JTA Transaction Manager for several XA data sources -->
            	<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
            
            	<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
            	   <property name="userTransaction" ref="jotm"/>
            	   <property name="allowCustomIsolationLevels" value="true"/>
            	</bean>
            	 
            	<bean id="vpnDataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
            	   <property name="dataSource">
            	      <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
            	         <property name="transactionManager" ref="jotm" />
            	         <property name="driverName" value="${db.vpn.driverName}" />
            	         <property name="url" value="${db.vpn.url}" />
            	      </bean>
            	   </property>
            	   <property name="user" value="${db.vpn.username}" />
            	   <property name="password" value="${db.vpn.password}" />
            	   <property name="maxSize" value="${db.vpn.maxSize}" />
            	   <property name="minSize" value="${db.vpn.minSize}" />
                </bean>
            	
            </beans>
            Last edited by hnarayanappa; Dec 2nd, 2010, 03:09 AM.

            Comment


            • #7
              As i stated in my first reply 'Use [ code][/code ] tags when posting code.' !!!!

              Your classes don't implement interfaces thus you need classproxies to make tx work, you need to specify this on the tx element you speficied.

              Comment


              • #8
                Hi,

                I'm sorry for not specifying the code tags, but I'm really not sure about the classproxies, could you please elaborate this or give me a link for this tutorial.

                Thanks.

                Comment


                • #9
                  I suggest the reference guide, the tx and/or the aop section gives you the information you need.

                  Comment


                  • #10
                    Transaction Management

                    Hi,

                    Thanks a lot Marten and Enrico for the help provided; regarding the proxyclasses, it will not help my architecture since, there are lot of places I would need to handle if I implement the proxies, but otherwise...

                    I was able to resolve this issue by passing an attribute to the Transaction annotation. Since I know the name of this runtime exception occured. I have hardcoded this exception in my code below.
                    Code:
                    @Transactional(propagation = Propagation.REQUIRED, rollbackForClassName={"MySQLIntegrityConstraintViolationException"})
                    However, this will not solve the issue for any kind of runtime Exception.
                    I'm still wondering, if there is still any other way to handle the rollback, when any global Runtime Exception has occured.

                    Please do provide me your suggestions.

                    Thanks a lot
                    Last edited by hnarayanappa; Dec 3rd, 2010, 06:56 AM.

                    Comment


                    • #11
                      I suggest you read the tx chapter... In general tx management follows the ejb semantics, rollback for every runtime exception. So you shouldn't need to configure it like that..

                      Comment

                      Working...
                      X