我正在开发一个基于 spring (spring-boot 1.5.13.RELEASE) 连接到 MySQL 8 数据库的 Web 应用程序。我们选择使用 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]
我的问题是:
- 有没有办法确保挂起的事务总是像我们预期的那样完成返回时回滚?
- 在这种情况下,我们可以检测并返回“未返回”的连接吗?
- 在这种情况下存在这样的财产并且没有管理似乎很奇怪?为什么我们要在我们已经提交或回滚的事务之后使用回滚?有什么我不明白的(确定是 :p )?
注意:请不要介意错误日志级别,我有点色盲。
编辑:
我尝试通过一个拦截器来检测当前活动的事务,使用TransactionSynchronizationManager.isActualTransactionActive()
. 这样,我找到了未完成的,但是当我试图用TransactionAspectSupport.currentTransactionStatus()
它来获得它时,我回来了null
???看不懂...
简化
有没有办法检测和回滚被遗忘的程序化事务(如下面的代码所示)?