Results 1 to 5 of 5

Thread: Hibernate - Transaction Doesnot roll back

  1. #1
    Join Date
    Mar 2005
    Posts
    13

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    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
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    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.

  4. #4
    Join Date
    Mar 2005
    Posts
    13

    Default

    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

  5. #5
    Join Date
    Mar 2005
    Posts
    13

    Default

    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

Similar Threads

  1. Replies: 0
    Last Post: Jun 6th, 2005, 06:22 AM
  2. Replies: 3
    Last Post: May 16th, 2005, 07:04 AM
  3. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  4. Replies: 3
    Last Post: Oct 1st, 2004, 03:56 AM
  5. Transaction pb Hibernate/MySQL
    By syluser in forum Data
    Replies: 2
    Last Post: Aug 28th, 2004, 02:40 PM

Posting Permissions

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