Announcement Announcement Module
Collapse
No announcement yet.
Spring Hibernate transaction rollback not working right Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Hibernate transaction rollback not working right

    Spring 1.2.2
    Hibernate 3.0.5
    SQL Server 7, jTDS 1.0.3
    JBoss 4.0.2

    I have an app using Spring's declarative transactions, delegating to HibernateTransactionManager, which is configured to use the JTATransactionManager strategy. I have an update that affects two tables, and if there is an exception on the second update, the first update does not get rolled back. I can see that the problem is that although everything is done within a single JTA transaction and the JTA transaction is rolled back, the individual updates are done with auto-commit ON.

    Everything is very standard. I am going by the book here and not doing anything unusual.

    Interestingly, I created a small test webapp that does not use Spring, but uses explicit Hibernate transaction management, and uses the exact same Hibernate configuration and the same JBoss datasource configuration, and things work fine. Hibernate is clearly setting auto-commit to false, and then setting it back to true after the transaction is done. Rollback correctly rolls back the first update.

    Here is my Spring configuation (this is by the book):

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName"><value>java:MSSQLDS</value></property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource"/>
    </property>
    <property name="mappingResources">
    <list>
    <value>A.hbm.xml</value>
    <value>B.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.SQLS erverDialect</prop>
    <prop key="hibernate.transaction.factory_class">org.hibe rnate.transaction.JTATransactionFactory</prop>
    <prop key="hibernate.connection.release_mode">on_close</prop>
    </props>
    </property>
    </bean>

    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>

    <bean id="transactionAttributeSource"
    class="org.springframework.transaction.interceptor .MatchAlwaysTransactionAttributeSource">
    </bean>

    <bean id="faqDao" class="FAQDaoImpl">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>

    <bean id="faqServiceTarget" class="FAQServiceImpl">
    <constructor-arg>
    <ref bean="faqDao"/>
    </constructor-arg>
    </bean>

    <bean id="faqService"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="proxyInterfaces">
    <list>
    <value>FAQService</value>
    </list>
    </property>
    <property name="target">
    <ref bean="faqServiceTarget"/>
    </property>
    <property name="transactionManager">
    <ref bean="transactionManager"/>
    </property>
    <property name="transactionAttributeSource">
    <ref bean="transactionAttributeSource"/>
    </property>
    </bean>

    The Dao code:
    public void save(A a, B a) {
    getHibernateTemplate().saveOrUpdate(a);
    getHibernateTemplate().saveOrUpdate(b);
    }

    For test purposes, A and B are very simple classes with just an id and a name and straightforward mapping.


    Everything works fine when I remove Spring from the equation, but when I take my Hibernate app and turn it into a Spring app, somehow Hibernate is not turning auto-commit off. What could Spring be doing to the session/connection that could alter Hibernate's behavior ?

    Any ideas, or, recommendations on how to debug this ?

  • #2
    Posted and solved here http://forum.springframework.org/showthread.php?t=16474
    Last edited by robyn; May 14th, 2006, 11:48 AM.

    Comment


    • #3
      Same Problem

      Hello, I am using the same technologies and I am having the same problem, can anyone help me please?

      Thank you.

      PD: The link indicating the solution is wrong, it redirects me to the same forum page.

      Comment


      • #4
        Ok, the correct link is:

        http://forum.springframework.org/showthread.php?t=16473

        Comment

        Working...
        X