Spring @Transactional(propagation = REQUIRES_NEW) 回滚
Spring @Transactional(propagation = REQUIRES_NEW) rollbacked
我需要有关 Spring、JTA、数据源设置和事务的建议:
@Transactional (Propagation = REQUIRES_NEW)
无法正常工作。
我在一个管理器方法上有一个 @Transactional
并且在注入的第二个管理器上调用了一个方法 @Transactional(propagation = REQUIRES_NEW)
。
如果回滚外部事务(通过抛出 RuntimeException),我希望内部事务中所做的更改将被保留。
但它并没有发生,内部事务根本没有提交。
日志显示:JtaTransactionManager - Initiating transaction commit
但是在 JDBC 驱动程序上没有调用 commit ()。
JDBC 驱动程序仅在外部事务回滚时调用 - 并且仅调用回滚。
设置:JBoss EAP 7,Spring 5.1,standalone.xml 中的 ojdbc 数据源,jta=true
通过 JNDI (WildFlyDataSource) 注入的数据源,autoCommit = false。
日志:
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()
问题已通过使用 TransactionAwareDataSourceProxy
并将 reobtainTransactionalConnections
设置为 true
解决
参见 this issue,它为我指出了正确的方法
我需要有关 Spring、JTA、数据源设置和事务的建议:
@Transactional (Propagation = REQUIRES_NEW)
无法正常工作。
我在一个管理器方法上有一个 @Transactional
并且在注入的第二个管理器上调用了一个方法 @Transactional(propagation = REQUIRES_NEW)
。
如果回滚外部事务(通过抛出 RuntimeException),我希望内部事务中所做的更改将被保留。 但它并没有发生,内部事务根本没有提交。
日志显示:JtaTransactionManager - Initiating transaction commit
但是在 JDBC 驱动程序上没有调用 commit ()。
JDBC 驱动程序仅在外部事务回滚时调用 - 并且仅调用回滚。
设置:JBoss EAP 7,Spring 5.1,standalone.xml 中的 ojdbc 数据源,jta=true 通过 JNDI (WildFlyDataSource) 注入的数据源,autoCommit = false。
日志:
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()
问题已通过使用 TransactionAwareDataSourceProxy
并将 reobtainTransactionalConnections
设置为 true
参见 this issue,它为我指出了正确的方法