1
votes

I need advice on Spring, JTA, datasource setup and transactions: @Transactional (Propagation = REQUIRES_NEW) does not work correctly.

I have a @Transactional on a manager method and a method on the injected second manager @Transactional(propagation = REQUIRES_NEW) is called.

If the outer transaction is rollbacked (by throwing RuntimeException), I would expect the changes made in the inner transaction will be persisted. But it does not happen, the internal transaction is not commited at all.

The log says: JtaTransactionManager - Initiating transaction commit but commit () is not called on the JDBC driver.

The JDBC driver is called only at the outer's transaction rollback - and only rollback is called.

Setup: JBoss EAP 7, Spring 5.1, ojdbc datasource in standalone.xml, with jta=true Datasource injected via JNDI (WildFlyDataSource), autoCommit = false.

Log:

    11:10:23,725 INFO  [stdout] (default task-1) DEBUG DataSourceUtils - Registering transaction synchronization for JDBC Connection
    11:10:23,732 INFO  [stdout] (default task-1) TRACE TransactionSynchronizationManager - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@248c2848] for key [org.jboss.as.connector.subsystems.datasources.WildFlyDataSource@43dd288] to thread [default task-1]
    11:10:23,733 INFO  [stdout] (default task-1) DEBUG SQLInsertClause - update APPLICATION_REQUEST set APP_REQ_STATUS = ?, APP_REQ_SUB_STATUS = ?, REJECTIONREASON = ? where APPLICATION_REQUEST.APPLICATION_ID = ?
    11:10:23,734 DEBUG [jboss.jdbc.spy] (default task-1) java:/jdbc/myDS [Connection] prepareStatement(update APPLICATION_REQUEST
    set APP_REQ_STATUS = ?, APP_REQ_SUB_STATUS = ?, REJECTIONREASON = ?
    where APPLICATION_REQUEST.APPLICATION_ID = ?)
    11:10:23,735 DEBUG [jboss.jdbc.spy] (default task-1) java:/jdbc/myDS [PreparedStatement] setString(1, REJECTED)
    11:10:23,736 DEBUG [jboss.jdbc.spy] (default task-1) java:/jdbc/myDS [PreparedStatement] setString(2, REJECTED)
    11:10:23,740 DEBUG [jboss.jdbc.spy] (default task-1) java:/jdbc/myDS [PreparedStatement] setLong(3, 1)
    11:10:23,742 DEBUG [jboss.jdbc.spy] (default task-1) java:/jdbc/myDS [PreparedStatement] setLong(4, 461)
    11:10:23,748 DEBUG [jboss.jdbc.spy] (default task-1) java:/jdbc/myDS [PreparedStatement] setQueryTimeout(299)
    11:10:23,749 DEBUG [jboss.jdbc.spy] (default task-1) java:/jdbc/myDS [PreparedStatement] executeUpdate()
    11:10:23,768 DEBUG [jboss.jdbc.spy] (default task-1) java:/jdbc/myDS [PreparedStatement] close()
    11:10:23,769 DEBUG [jboss.jdbc.spy] (default task-1) java:/jdbc/myDS [Connection] isClosed()
    11:10:23,770 INFO  [stdout] (default task-1) INFO  StatisticLogger - DB, 36, update
    11:10:23,771 INFO  [stdout] (default task-1) DEBUG ApplicationRequestDaoImpl - DAO updateApplication - number of updated records is: 1
    11:10:23,771 INFO  [stdout] (default task-1) TRACE TransactionSynchronizationManager - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@248c2848] for key [org.jboss.as.connector.subsystems.datasources.WildFlyDataSource@43dd288] bound to thread [default task-1]
    11:10:23,774 INFO  [stdout] (default task-1) TRACE JtaTransactionManager - Triggering beforeCommit synchronization
    11:10:23,778 INFO  [stdout] (default task-1) TRACE JtaTransactionManager - Triggering beforeCompletion synchronization
    11:10:23,779 INFO  [stdout] (default task-1) TRACE TransactionSynchronizationManager - Removed value [org.springframework.jdbc.datasource.ConnectionHolder@248c2848] for key [org.jboss.as.connector.subsystems.datasources.WildFlyDataSource@43dd288] from thread [default task-1]
    11:10:23,780 INFO  [stdout] (default task-1) DEBUG DataSourceUtils - Returning JDBC Connection to DataSource
    11:10:23,781 DEBUG [jboss.jdbc.spy] (default task-1) java:/jdbc/myDS [Connection] close()
    11:10:23,782 INFO  [stdout] (default task-1) DEBUG JtaTransactionManager - Initiating transaction commit
    11:10:23,784 INFO  [stdout] (default task-1) TRACE JtaTransactionManager - Triggering afterCommit synchronization
    11:10:23,785 INFO  [stdout] (default task-1) TRACE TransactionSynchronizationManager - Clearing transaction synchronization
    11:10:23,786 INFO  [stdout] (default task-1) TRACE JtaTransactionManager - Triggering afterCompletion synchronization
>>>HERE I WOULD EXPECT INNER TRX BE COMMITED<<<
    11:10:23,787 INFO  [stdout] (default task-1) DEBUG JtaTransactionManager - Resuming suspended transaction after completion of inner transaction
    11:10:23,788 INFO  [stdout] (default task-1) TRACE TransactionSynchronizationManager - Initializing transaction synchronization
    11:10:23,792 INFO  [stdout] (default task-1) TRACE TransactionSynchronizationManager - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@5a3f13a] for key [org.jboss.as.connector.subsystems.datasources.WildFlyDataSource@43dd288] to thread [default task-1]
    11:10:23,803 INFO  [stdout] (default task-1) TRACE TransactionInterceptor - Completing transaction for [MyController.method] after exception: MyValidationException: Validation failed.
    11:10:23,805 INFO  [stdout] (default task-1) TRACE RuleBasedTransactionAttribute - Applying rules to determine whether transaction should rollback on MyValidationException: Validation failed.
    11:10:23,807 INFO  [stdout] (default task-1) TRACE RuleBasedTransactionAttribute - Winning rollback rule is: null
    11:10:23,808 INFO  [stdout] (default task-1) TRACE RuleBasedTransactionAttribute - No relevant rollback rule found: applying default rules
    11:10:23,813 INFO  [stdout] (default task-1) TRACE JtaTransactionManager - Triggering beforeCompletion synchronization
    11:10:23,815 INFO  [stdout] (default task-1) DEBUG JtaTransactionManager - Initiating transaction rollback
    11:10:23,838 INFO  [stdout] (default task-1) TRACE TransactionSynchronizationManager - Clearing transaction synchronization
    11:10:23,840 INFO  [stdout] (default task-1) TRACE JtaTransactionManager - Triggering afterCompletion synchronization
    11:10:23,841 INFO  [stdout] (default task-1) TRACE TransactionSynchronizationManager - Removed value [org.springframework.jdbc.datasource.ConnectionHolder@5a3f13a] for key [org.jboss.as.connector.subsystems.datasources.WildFlyDataSource@43dd288] from thread [default task-1]
    11:10:23,842 INFO  [stdout] (default task-1) DEBUG DataSourceUtils - Returning JDBC Connection to DataSource
    11:10:23,843 DEBUG [jboss.jdbc.spy] (default task-1) java:/jdbc/myDS [Connection] close()
1

1 Answers

1
votes

The problem has been solved by using TransactionAwareDataSourceProxy and setting reobtainTransactionalConnections to true

See this issue which pointed me to the right way