Announcement Announcement Module
Collapse
No announcement yet.
Hibernate - Transaction Doesnot roll back Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate - Transaction Doesnot roll back

    Hi all,

    I saw all the threads related to transaction not rolling back, but could not relate anyone to my situation. Or so I think

    This is my applicationContext.xml section ===

    <bean id="tagDataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName">
    <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
    <value>jdbc:mysql://ashah/dwhweb</value>
    </property>
    <property name="username">
    <value>dwhweb</value>
    </property>
    <property name="password">
    <value>dwhweb</value>
    </property>
    </bean>
    <bean id="tagSessionFactory"
    class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
    <property name="mappingResources">
    <list>
    <value>
    com/tagaudit/tagportal/om/SecurityPrincipal.hbm.xml
    </value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    net.sf.hibernate.dialect.MySQLDialect
    </prop>
    </props>
    </property>
    <property name="dataSource">
    <ref bean="tagDataSource"/>
    </property>
    </bean>
    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
    <bean id="tagTransactionManager"
    class="org.springframework.orm.hibernate.Hibernate TransactionManager">
    <property name="sessionFactory">
    <ref local="tagSessionFactory"/>
    </property>
    </bean>
    <!-- ============== End Data Access using Spring provided O/R Mappers ============== -->
    <bean id="securityPrincipalDao"
    class="com.tagaudit.tagportal.om.SecurityPrincipal Dao">
    <property name="sessionFactory">
    <ref bean="tagSessionFactory"/>
    </property>
    </bean>
    <bean id="testServiceTarget"
    class="com.tagaudit.tagportal.services.TestService Impl">
    <property name="securityPrincipalDao">
    <ref bean="securityPrincipalDao"/>
    </property>
    </bean>
    <bean id="testService"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager">
    <ref bean="tagTransactionManager"/>
    </property>
    <property name="target">
    <ref bean="testServiceTarget"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="testMethodRead">PROPAGATION_REQUIRED,readOnly </prop>
    <prop key="testMethodWrite">
    PROPAGATION_REQUIRED,-DataAccessException</prop>
    </props>
    </property>
    </bean>



    And following is my file ======

    public void testMethodWrite() throws TagException {
    SecurityPrincipal principal = new SecurityPrincipal();
    principal.setClassname("TestClass");
    principal.setCreationDate(Calendar.getInstance().g etTime());
    principal.setFullPath("testDelete");
    principal.setPrincipalId(new Integer(500));

    for(int i = 0; i < 4; ++i) {
    try {
    securityPrincipalDao.savePrincipal(principal);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    System.out.println("Did the insert too !!!!");
    }


    When I call this method, it performs the first write, and then throws exception, but never rolls back. Please help :cry:

  • #2
    The transaction does not rollback becauseyour method does not throw any exception.
    try
    Code:
    for&#40;int i = 0; i < 4; ++i&#41; &#123; 
    try &#123; 
      securityPrincipalDao.savePrincipal&#40;principal&#41;; 
    &#125; catch &#40;Exception e&#41; &#123; 
      e.printStackTrace&#40;&#41;;
      throw e;
    &#125;
    HTH

    Comment


    • #3
      Also note:
      Code:
      -DataAccessException
      is not required because DataAccessException is a runtime exception and Spring will rollback by default (Spring behaves like EJB in this respect).

      You also will want to ensure your re-thrown Exception is a runtime exception. You can wrap it in a runtime exception (a bit ugly), or catch and re-throw a more specific exception that is a runtime exception.

      Failing that (i.e. you must catch Exception) you could add (a bit coarse)
      Code:
      -Exception
      to force a rollback.

      Comment


      • #4
        Thanks a lot for all your replies.

        I tried all of that, but nothing helped. Actually I tried doing this --


        <property name="transactionAttributes">
        <props>
        <prop key="testMethodWrite">
        PROPAGATION_REQUIRED,readOnly</prop>
        </props>
        </property>



        Just to see if the write is not allowed, but it still writes to the DB. So I am not sure if the interceptor is working properly and starting the transactions as needed at all :cry:

        Any helps, ideas ...

        Thanks,
        Amit

        Comment


        • #5
          Hi all,

          Thanks a lot for your help.

          I was able to figure out the issue. I guess I misunderstood, or it was pretty dumb of me :cry:

          The issue was as follows --

          My serviceClass has 3 methods -

          Method1, Method2, Method3. Now Method1 calls Method2, and Method3.


          In the applicationContext.xml file, I declared Method2 and Method3 to be under a transaction. But then my web layer calls Method 1, and NOT Method2 and Method3 directly. That was the reason why a new transaction was not being created at all.

          The solution was, I had to declare Method1 in the xml file too, so that it starts under a transaction too.

          Sorry for the trouble all ...
          Amit

          Comment

          Working...
          X