Announcement Announcement Module
Collapse
No announcement yet.
Pb with Rollback declarative transaction with Hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Pb with Rollback declarative transaction with Hibernate

    Hi there,
    I've tried to run the example from a recent article on onjava.com (Wire Hibernate Transactions in Spring) and I can't get it work with Spring using JTA.

    Here are my Spring configuration files :

    applicationContext-hibernate.xml
    ----------------------------------

    <beans>
    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource"><ref bean="dataSource"/></property>
    <property name="mappingResources">
    <list>
    <value>org/glytest/model/LineItem.hbm.xml</value>
    <value>org/glytest/model/OrderList.hbm.xml</value>
    <value>org/glytest/model/AuditObject.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">@HIBERNATE-DIALECT@</prop>
    <!--prop key="hibernate.show_sql">true</prop-->
    <!-- Create/update the database tables automatically when the JVM starts up
    <prop key="hibernate.hbm2ddl.auto">update</prop> -->
    </props>
    </property>
    </bean>

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

    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
    <!--
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    -->
    <!-- Transaction manager that delegates to JTA (for a transactional JNDI DataSource)
    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTran sactionManager"/>
    -->

    <bean id="auditObjectDAO" class="org.glytest.dao.hibernate.AuditObjectDAOHib ernate">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>

    <bean id="orderListDAO" class="org.glytest.dao.hibernate.OrderListDAOHiber nate">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    </beans>

    applicationContext-services.xml
    --------------------------------

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>


    <!-- Transaction template for Managers -->
    <bean id="txProxyTemplate" abstract="true"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager"><ref bean="transactionManager"/></property>
    <property name="transactionAttributes">
    <props>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="remove*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    </props>
    </property>
    </bean>


    <!-- Transaction declarations for business services. -->


    <bean id="orderListManager" parent="txProxyTemplate">
    <property name="target">
    <bean class="org.glytest.service.impl.OrderListManagerIm pl" autowire="byName"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="getAllOrderList">PROPAGATION_REQUIRED</prop>
    <prop key="getOrderList">PROPAGATION_REQUIRED</prop>
    <prop key="createOrderList">PROPAGATION_REQUIRED</prop>
    <prop key="addLineItem">PROPAGATION_REQUIRED,-TooManyLineItemException</prop>
    <prop key="getAllLineItems">PROPAGATION_REQUIRED,readOnl y</prop>
    <prop key="queryNumberOfLineItems">PROPAGATION_REQUIRED, readOnly</prop>
    </props>
    </property>
    </bean>

    <bean id="auditManager" parent="txProxyTemplate">
    <property name="target">
    <bean class="org.glytest.service.impl.AuditManagerImpl">
    <property name="auditObjectDAO"><ref bean="auditObjectDAO"/></property>
    </bean>
    </property>
    <!-- Override default transaction attributes b/c of LoginCookie methods -->
    <property name="transactionAttributes">
    <props>
    <!--
    <prop key="log">PROPAGATION_SUPPORTS</prop>
    <prop key="log">PROPAGATION_REQUIRES_NEW</prop>
    <prop key="log">PROPAGATION_REQUIRED</prop>
    -->
    <prop key="log">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    </beans>

    applicationContext-resources
    ----------------------------

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>

    <!-- JNDI DataSource for J2EE environments -->
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName"><value>java:/jdbc/txdemoDS</value></property>
    </bean>
    </beans>

    Environment:
    ------------
    Spring 1.2.1
    Hibernate 3.0.3
    JBoss 3.2.7
    Windows XP
    My data source in JBoss is declaring via a mysql database:
    <?xml version="1.0" encoding="UTF-8"?>

    <datasources>
    <local-tx-datasource>
    <jndi-name>jdbc/txdemoDS</jndi-name>
    <connection-url>jdbc:mysql://localhost:3306/txdemo</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>root</user-name>
    <password>root</password>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLE xceptionSorter</exception-sorter-class-name>
    <!-- sql to call when connection is created
    <new-connection-sql>some arbitrary sql</new-connection-sql>
    -->
    <!-- sql to call on an existing pooled connection when it is obtained from pool -->
    <!-- <check-valid-connection-sql>show databases;</check-valid-connection-sql>-->

    </local-tx-datasource>
    </datasources>

    Results
    --------------------------------
    When running the program with an Hibernate transaction manager (org.springframework.orm.hibernate3.HibernateTrans actionManager), I get the expected behavior:

    LineItem:

    lineitem_id productId orderlist_id
    1 Professional Hibernate 1
    2 Spring Live 1
    3 Professional .NET 2
    4 Architecture Bootcamp 2

    By just changing the transaction manager by JTA (org.springframework.transaction.jta.JtaTransactio nManager)
    I don't get the roolback on the method addLineItem (this method throw an TooManyLineItemException) when trying to
    add more than 2 lineTtem to an orderlist. The result reads:

    lineitem_id productId orderlist_id
    1 Professional Hibernate 1
    2 Spring Live 1
    3 Professional .NET 2
    4 Architecture Bootcamp 2
    5 .NET Remoting 2



    What I am doing wrong? Is there a problem using a JTA transaction manager with Spring declarative transactions?

    Here is JBoss log:

    11:48:54,635 INFO [ConnectionProviderFactory] Initializing connection provider: org.springframe
    work.orm.hibernate3.LocalDataSourceConnectionProvi der
    11:48:55,106 INFO [SettingsFactory] RDBMS: MySQL, version: 4.1.10a-nt
    11:48:55,116 INFO [SettingsFactory] JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector
    -java-3.1.7 ( $Date: 2005/01/25 19:11:41 $, $Revision: 1.27.4.54 $ )
    11:48:55,176 INFO [Dialect] Using dialect: org.hibernate.dialect.MySQLDialect
    11:48:55,206 INFO [TransactionFactoryFactory] Using default transaction strategy (direct JDBC t
    ransactions)
    11:48:55,216 INFO [TransactionManagerLookupFactory] No TransactionManagerLookup configured (in
    JTA environment, use of read-write or transactional second-level cache is not recommended)
    11:48:55,216 INFO [SettingsFactory] Automatic flush during beforeCompletion(): disabled
    11:48:55,216 INFO [SettingsFactory] Automatic session close at end of transaction: disabled
    11:48:55,216 INFO [SettingsFactory] JDBC batch size: 15
    11:48:55,216 INFO [SettingsFactory] JDBC batch updates for versioned data: disabled
    11:48:55,216 INFO [SettingsFactory] Scrollable result sets: enabled
    11:48:55,226 INFO [SettingsFactory] JDBC3 getGeneratedKeys(): enabled
    11:48:55,226 INFO [SettingsFactory] Connection release mode: null
    11:48:55,377 INFO [SessionFactoryImpl] building session factory
    11:48:56,879 INFO [SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
    11:48:56,879 INFO [SessionFactoryImpl] Checking 2 named queries
    11:48:57,480 INFO [DefaultListableBeanFactory] Creating shared instance of singleton bean 'transactionManager'
    11:48:57,550 INFO [HibernateTransactionManager] Using DataSource [org.jboss.resource.adapter.jd
    bc.WrapperDataSource@14d556e] of Hibernate SessionFactory for HibernateTransactionManager
    11:48:57,620 INFO [DefaultListableBeanFactory] Creating shared instance of singleton bean 'auditObjectDAO'
    11:48:57,630 INFO [DefaultListableBeanFactory] Creating shared instance of singleton bean 'orderListDAO'
    11:48:57,670 INFO [DefaultListableBeanFactory] Creating shared instance of singleton bean 'manager'
    11:48:57,680 INFO [DefaultAopProxyFactory] CGLIB2 available: proxyTargetClass feature enabled
    11:48:57,980 INFO [DefaultListableBeanFactory] Creating shared instance of singleton bean 'orderListManager'
    11:48:58,000 INFO [DefaultListableBeanFactory] Creating shared instance of singleton bean 'auditManager'
    11:48:58,020 INFO [AuditManagerImpl]
    AuditManagerImpl auditObjectDAO: org.glytest.dao.hibernate.AuditObjectDAOHibernate@ 1e4bece
    11:49:15,235 INFO [STDOUT]
    OrderListManagerImpl.createOrderList : Start...
    11:49:15,235 INFO [STDOUT]
    OrderListManagerImpl.createOrderList : auditManager: org.glytest.service.impl.AuditManagerImpl@1
    28ed5f
    11:49:15,285 DEBUG [UploadAction] Created OrderList with id '2'...

    11:49:15,305 INFO [STDOUT] Added LineItem 3 to Order 2.
    11:49:15,305 INFO [STDOUT] OrderListManagerImpl.addLineItem : End.
    11:49:15,375 INFO [STDOUT] Added LineItem 4 to Order 2.
    11:49:15,445 INFO [STDOUT] Added LineItem 5 to Order 2; But rolling back *** !
    11:49:15,495 DEBUG [UploadAction] ERROR : Make a new Order for this line item

    if I enable log4j on org.springframework.transaction and org.hibernate I get this log:

    12:23:29,599 INFO [STDOUT] Added LineItem 5 to Order 2; But rolling back *** !
    12:23:29,599 DEBUG [RuleBasedTransactionAttribute] Applying rules to determine whether transacti
    on should rollback on org.glytest.service.TooManyLineItemException: Make a new Order for this li
    ne item
    12:23:29,599 DEBUG [RuleBasedTransactionAttribute] Winning rollback rule is: RollbackRule with p
    attern [TooManyLineItemException]
    12:23:29,599 DEBUG [TransactionInterceptor] Invoking rollback for transaction on org.glytest.ser
    vice.OrderListManager.addLineItem due to throwable [org.glytest.service.TooManyLineItemException
    : Make a new Order for this line item]
    12:23:29,599 DEBUG [JtaTransactionManager] Triggering beforeCompletion synchronization
    12:23:29,599 DEBUG [SessionImpl] setting flush mode to: NEVER
    12:23:29,599 DEBUG [JtaTransactionManager] Initiating transaction rollback
    12:23:29,599 DEBUG [JtaTransactionManager] Rolling back JTA transaction
    12:23:29,609 DEBUG [JtaTransactionManager] Triggering afterCompletion synchronization
    12:23:29,609 DEBUG [SessionImpl] after transaction completion
    12:23:29,609 DEBUG [TransactionSynchronizationManager] Clearing transaction synchronization
    12:23:29,609 DEBUG [UploadAction] ERROR : Make a new Order for this line item
    12:23:29,669 DEBUG [TransactionSynchronizationManager] Removed value [org.springframework.orm.hi
    bernate3.SessionHolder@28318d] for key [org.hibernate.impl.SessionFactoryImpl@db5b3f] from threa
    d [http-0.0.0.0-8080-Processor25]
    12:23:29,679 DEBUG [SessionImpl] closing session
    12:23:29,679 DEBUG [AbstractBatcher] closing JDBC connection (open PreparedStatements: 0, global
    ly: 0) (open ResultSets: 0, globally: 0)
    12:23:29,679 DEBUG [JDBCContext] after transaction completion
    12:23:29,679 DEBUG [SessionImpl] after transaction completion

    From the log it appears that the JtaTransactionManager is rolling back the transaction but at the end the transaction is not rollback as the last lineitem is save to the database.
    What's wrong?
Working...
X