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.

    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>

    <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="aDao" class="ADaoImpl">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>

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

    <bean id="aService"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="proxyInterfaces">
    <list>
    <value>AService</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>

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

    Comment


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

      Thank you.

      Comment

      Working...
      X