Skip to content

Commit 68b6cf2

Browse files
committed
spring-transaction: 补充事务挂起
1 parent f8d6abc commit 68b6cf2

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

note/spring-transaction.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,23 @@ private static Object doUnbindResource(Object actualKey) {
506506

507507
同时注意getTransaction方法返回的是一个TransactionStatus对象,**被挂起的事务的各种状态都被保存在此对象中**
508508

509+
那么挂起这个操作到底是如何实现(起作用)的呢?
510+
511+
DataSourceTransactionManager.doSuspend:
512+
513+
```java
514+
@Override
515+
protected Object doSuspend(Object transaction) {
516+
DataSourceTransactionObject txObject = (DataSourceTransactionObject) transaction;
517+
txObject.setConnectionHolder(null);
518+
ConnectionHolder conHolder = (ConnectionHolder)
519+
TransactionSynchronizationManager.unbindResource(this.dataSource);
520+
return conHolder;
521+
}
522+
```
523+
524+
其实玄机就在于将ConnectionHolder设为null这一行,因为**一个ConnectionHolder对象就代表了一个数据库连接,将ConnectionHolder设为null就意味着我们下次要使用连接时,将重新从连接池获取,而新的连接的自动提交是为true的**
525+
509526
##### PROPAGATION_REQUIRES_NEW
510527

511528
```java

0 commit comments

Comments
 (0)