Announcement Announcement Module
No announcement yet.
Spring Hibernate transaction rollback not working right Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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.

    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:

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

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource"/>
    <property name="mappingResources">
    <property name="hibernateProperties">
    <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>

    <bean id="aDao" class="ADaoImpl">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>

    <bean id="aServiceTarget" class="AServiceImpl">
    <ref bean="aDao"/>

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

  • #2
    If you want JTA transactions, you should use Spring's JtaTransactionManager instead of HibernateTransactionManager.


    • #3
      It looks like that solved my problems and everything is working great.

      Thank you.