我正在基于Spring的Web应用程序上(spring-boot 1.5.13.RELEASE)连接到MySQL 8数据库。我们选择使用tomcat jdbc池(spring-boot的默认池)。生产部门要求我们使用一种解决方案,以避免使用rollback-on-return
属性(我们也设置了auto-commit=false
)来完成不完整的交易。
我们不能只使用面向注释的事务。我们必须以编程方式管理其中的一些。因此,我们认为使用rollback-on-return
可以避免“待处理”事务。不幸的是,在测试阶段,我们发现如果一个请求创建了一个“待处理”事务,则请求的结束不会随着连接的返回而进行。因此,没有回滚!
我已经测试了正常情况下连接是否关闭,
这是我的示例代码:
public CommandLineRunner commandLineRunner(CustomerManager customerManager, EngineManager engineManager, DataSource dataSource) {
return (String... args) -> {
log.info("Start commandLineRunner" + engineManager.getTransactionPartEngineInnodbStatus());
Thread t = new Thread(() -> {
try {
customerManager.processII_notcomplete();
} catch (Exception e) {
System.err.println(e.getMessage());
}
});
t.start();
t.join();
log.info("End commandLineRunner" + engineManager.getTransactionPartEngineInnodbStatus());
};
}
public void processII_notcomplete() {
TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED));
log.info("processII [START]");
Customer customer = customerRepository.findOne(1L);
customer.lastName = "De " + customer.lastName;
customerRepository.save(customer);
log.info("processII: customerRepository.save(customer)" + engineManager.getTransactionPartEngineInnodbStatus() + "\n");
// Intentionally don't commit the transaction to create the use case
log.info("processII [END]");
}
我找到了一个JdbcInterceptor来记录借用和连接的返回:
public class JDBCLogInterceptor extends JdbcInterceptor {
private static final Logger log = LoggerFactory.getLogger(JDBCLogInterceptor.class);
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (log.isDebugEnabled()) {
String name = method.getName();
if (CLOSE_VAL.equals(name)) {
log.debug(String.format("Returning Connection to Pool [%s]", proxy));
}
}
return super.invoke(proxy, method, args);
}
@Override
public void reset(ConnectionPool connPool, PooledConnection conn) {
if (connPool != null && conn != null) {
if (log.isDebugEnabled()) {
log.debug(String.format("Getting Connection [%s], Pool active=[%s], idle=[%s]", conn.toString(),
connPool.getActive(), connPool.getIdle()));
}
}
}
@Override
public void disconnected(ConnectionPool connPool, PooledConnection conn, boolean finalizing) {
if (connPool != null && conn != null) {
if (log.isDebugEnabled()) {
log.debug(String.format("Closing Connection [%s], Pool active=[%s], idle=[%s]", conn.toString(),
connPool.getActive(), connPool.getIdle()));
}
}
}
}
这是相关的日志部分:
ERROR 2019/01/29 16:28:13.975 - no-request --- mova.jpatest.JPATestConfig - Start commandLineRunner
------------
TRANSACTIONS
------------
Trx id counter 269691
Purge done for trx's n:o < 269691 undo n:o < 0 state: running but idle
History list length 7
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 283703119152960, not started
0 lock struct(s), heap size 1136, 0 row lock(s) - [no-one]
DEBUG 2019/01/29 16:28:13.985 - no-request --- mova.jpatest.jdbc.JDBCLogInterceptor - Getting Connection [PooledConnection[com.p6spy.engine.wrapper.ConnectionWrapper@63bf9fce]], Pool active=[1], idle=[9] - [no-one]
INFO 2019/01/29 16:28:13.989 - no-request --- mova.jpatest.manager.CustomerManager - processII [START] - [no-one]
INFO 2019/01/29 16:28:13.995 - no-request --- p6spy - statement --- select customer0_.id as id1_0_0_, customer0_.first_name as first_na2_0_0_, customer0_.last_name as last_nam3_0_0_ from customer customer0_ where customer0_.id=? - select customer0_.id as id1_0_0_, customer0_.first_name as first_na2_0_0_, customer0_.last_name as last_nam3_0_0_ from customer customer0_ where customer0_.id=1 - 01-29-19 16:28:13:995-2 [connection 8] - [no-one]
INFO 2019/01/29 16:28:14.029 - no-request --- p6spy - statement --- update customer set first_name=?, last_name=? where id=? - update customer set first_name='Jack', last_name='De De De De De De De Bauer' where id=1 - 01-29-19 16:28:14:29-0 [connection 8] - [no-one]
INFO 2019/01/29 16:28:14.039 - no-request --- p6spy - statement --- show engine innodb status; - show engine innodb status; - 01-29-19 16:28:14:39-6 [connection 8] - [no-one]
INFO 2019/01/29 16:28:14.039 - no-request --- mova.jpatest.manager.CustomerManager - processII: customerRepository.save(customer)
------------
TRANSACTIONS
------------
Trx id counter 269692
Purge done for trx's n:o < 269691 undo n:o < 0 state: running but idle
History list length 7
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 269691, ACTIVE 0 sec
2 lock struct(s), heap size 1136, 1 row lock(s), undo log entries 1
MySQL thread id 624, OS thread handle 20236, query id 47274 localhost 127.0.0.1 mohicane starting
show engine innodb status
Trx read view will not see trx with id >= 269691, sees < 269691
- [no-one]
INFO 2019/01/29 16:28:14.039 - no-request --- mova.jpatest.manager.CustomerManager - processII [END] - [no-one]
DEBUG 2019/01/29 16:28:14.039 - no-request --- mova.jpatest.jdbc.JDBCLogInterceptor - Getting Connection [PooledConnection[com.p6spy.engine.wrapper.ConnectionWrapper@3d646e1a]], Pool active=[2], idle=[8] - [no-one]
INFO 2019/01/29 16:28:14.045 - no-request --- p6spy - statement --- show engine innodb status; - show engine innodb status; - 01-29-19 16:28:14:45-2 [connection 9] - [no-one]
DEBUG 2019/01/29 16:28:14.045 - no-request --- mova.jpatest.jdbc.JDBCLogInterceptor - Returning Connection to Pool [ProxyConnection[PooledConnection[com.p6spy.engine.wrapper.ConnectionWrapper@3d646e1a]]] - [no-one]
ERROR 2019/01/29 16:28:14.045 - no-request --- mova.jpatest.JPATestConfig - End commandLineRunner
------------
TRANSACTIONS
------------
Trx id counter 269692
Purge done for trx's n:o < 269691 undo n:o < 0 state: running but idle
History list length 7
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 283703119153840, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 269691, ACTIVE 0 sec
2 lock struct(s), heap size 1136, 1 row lock(s), undo log entries 1
MySQL thread id 624, OS thread handle 20236, query id 47274 localhost 127.0.0.1 mohicane
Trx read view will not see trx with id >= 269691, sees < 269691 - [no-one]
我的问题是:
NB: 请不要介意ERROR日志级别,我有点色盲。
编辑:
我尝试通过一个拦截器来使用TransactionSynchronizationManager.isActualTransactionActive()
检测当前的活动事务。这样,我找到了未完成的任务,但是当我尝试用TransactionAspectSupport.currentTransactionStatus()
来获得它时,它返回了我null
?听不懂...
简化: 有没有办法检测和回滚被遗忘的程序化事务(如下面的代码所示)?