Skip to content

Commit 5e4d4a9

Browse files
committed
feat:事物源码done
1 parent b750c16 commit 5e4d4a9

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceTransactionManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ protected void doSetRollbackOnly(DefaultTransactionStatus status) {
359359
logger.debug("Setting JDBC transaction [" + txObject.getConnectionHolder().getConnection() +
360360
"] rollback-only");
361361
}
362-
txObject.setRollbackOnly();
362+
txObject.setRollbackOnly(); // 将trxInfo中的status中的tansacation中的connectionholder中的rollback only设为true
363363
}
364364

365365
@Override

spring-test/src/main/java/org/springframework/wx/tx/StudentService.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,15 @@ public void insertStudentBatch(){
2828

2929
// new Thread(()->{
3030
// System.out.println(Thread.currentThread().getName());
31-
// try {
31+
try {
3232
studentService.insertStudentOne();
33-
// } catch (Exception e){
34-
//
35-
// }
33+
} catch (Exception e){
34+
}
3635
// },"i am a new thread ").run();
3736
// studentService.insertStudentTWO();
3837
}
3938

40-
@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED )
39+
@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
4140
public void insertStudentOne(){
4241
System.err.println("-----------------------------------" + Thread.currentThread().getName());
4342
String sql = "INSERT INTO student(name,age,gender) values ('1同学',112,1)";

spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ protected TransactionInfo prepareTransactionInfo(@Nullable PlatformTransactionMa
637637
// We always bind the TransactionInfo to the thread, even if we didn't create
638638
// a new transaction here. This guarantees that the TransactionInfo stack
639639
// will be managed correctly even if no transaction was created by this aspect.
640-
txInfo.bindToThread();
640+
txInfo.bindToThread(); // 将当前的manager绑定到当前的线程中,但是老的manager页放入新的manager的older变量中
641641
return txInfo;
642642
}
643643

spring-tx/src/main/java/org/springframework/transaction/support/AbstractPlatformTransactionManager.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -699,15 +699,16 @@ public final void commit(TransactionStatus status) throws TransactionException {
699699
processRollback(defStatus, false);
700700
return;
701701
}
702-
702+
// 后者此处回去检测 txInfo中的status中的connectionHolder中的connection中的rollBackOnly标志
703+
// 如果为true则直接回滚
703704
if (!shouldCommitOnGlobalRollbackOnly() && defStatus.isGlobalRollbackOnly()) {
704705
if (defStatus.isDebug()) {
705706
logger.debug("Global transaction is marked as rollback-only but transactional code requested commit");
706707
}
707708
processRollback(defStatus, true);
708709
return;
709710
}
710-
711+
// 直接提交
711712
processCommit(defStatus);
712713
}
713714

@@ -727,21 +728,21 @@ private void processCommit(DefaultTransactionStatus status) throws TransactionEx
727728
triggerBeforeCommit(status);
728729
triggerBeforeCompletion(status);
729730
beforeCompletionInvoked = true;
730-
731+
// 是否有回滚点,有回滚点其实意味着是子事物则需要延迟提交待到父亲事物一起提交
731732
if (status.hasSavepoint()) {
732733
if (status.isDebug()) {
733734
logger.debug("Releasing transaction savepoint");
734735
}
735736
unexpectedRollback = status.isGlobalRollbackOnly();
736737
status.releaseHeldSavepoint();
737738
}
738-
else if (status.isNewTransaction()) {
739+
else if (status.isNewTransaction()) { // 如果是第一个connection则直接提交,第一个connection意味着是事物方法调用的根
739740
if (status.isDebug()) {
740741
logger.debug("Initiating transaction commit");
741742
}
742743
unexpectedRollback = status.isGlobalRollbackOnly();
743744
doCommit(status);
744-
}
745+
} // 对于使用的一个事物会到,此处,也就是说ab都是require,a调用b,b在提交的时候会走到这里
745746
else if (isFailEarlyOnGlobalRollbackOnly()) {
746747
unexpectedRollback = status.isGlobalRollbackOnly();
747748
}
@@ -754,7 +755,7 @@ else if (isFailEarlyOnGlobalRollbackOnly()) {
754755
}
755756
}
756757
catch (UnexpectedRollbackException ex) {
757-
// can only be caused by doCommit
758+
// can only be caused by doCommit //// 执行向TrxManger中注册同步器(afterRollback的逻辑)
758759
triggerAfterCompletion(status, TransactionSynchronization.STATUS_ROLLED_BACK);
759760
throw ex;
760761
}
@@ -763,7 +764,7 @@ else if (isFailEarlyOnGlobalRollbackOnly()) {
763764
if (isRollbackOnCommitFailure()) {
764765
doRollbackOnCommitException(status, ex);
765766
}
766-
else {
767+
else { // 执行向TrxManger中注册同步器,该同步器可以实现afterCommit和AfterRollback的执行逻辑
767768
triggerAfterCompletion(status, TransactionSynchronization.STATUS_UNKNOWN);
768769
}
769770
throw ex;

0 commit comments

Comments
 (0)