Spring Boot 编程式事务 TransactionTemplate
Spring Boot 事务 About 3,560 words备注
不会触发Spring Event
,按需手动发送。
代码
import org.springframework.transaction.support.TransactionTemplate;
@Resource
private TransactionTemplate transactionTemplate;
transactionTemplate.executeWithoutResult(transactionStatus -> {
try {
// Logic
} catch (Throwable e) {
log.error("error: {}", e.getMessage(), e);
transactionStatus.setRollbackOnly();
throw e;
}
})
注册事务同步事件
当transactionTemplate
执行完事务后会触发事件,等同于发送Spring Event
事件。
transactionTemplate.executeWithoutResult(transactionStatus -> {
log.info("add start");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
//做你想做的业务
System.out.println("afterCommit#" + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
}
@Override
public void afterCompletion(int status) {
// int STATUS_COMMITTED = 0;
// int STATUS_ROLLED_BACK = 1;
// int STATUS_UNKNOWN = 2;
System.out.println("afterCompletion#" + status + ": " + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
}
});
});
回滚机制
org.springframework.transaction.support.TransactionTemplate#execute
doInTransaction
中抓了RuntimeException
和Error
的异常,并且调用rollbackOnException()
方法进行回滚。
public class TransactionTemplate extends DefaultTransactionDefinition
implements TransactionOperations, InitializingBean {
@Override
@Nullable
public <T> T execute(TransactionCallback<T> action) throws TransactionException {
Assert.state(this.transactionManager != null, "No PlatformTransactionManager set");
if (this.transactionManager instanceof CallbackPreferringPlatformTransactionManager cpptm) {
return cpptm.execute(this, action);
}
else {
TransactionStatus status = this.transactionManager.getTransaction(this);
T result;
try {
result = action.doInTransaction(status);
}
catch (RuntimeException | Error ex) {
// Transactional code threw application exception -> rollback
rollbackOnException(status, ex);
throw ex;
}
catch (Throwable ex) {
// Transactional code threw unexpected exception -> rollback
rollbackOnException(status, ex);
throw new UndeclaredThrowableException(ex, "TransactionCallback threw undeclared checked exception");
}
this.transactionManager.commit(status);
return result;
}
}
private void rollbackOnException(TransactionStatus status, Throwable ex) throws TransactionException {
Assert.state(this.transactionManager != null, "No PlatformTransactionManager set");
logger.debug("Initiating transaction rollback on application exception", ex);
try {
this.transactionManager.rollback(status);
}
catch (TransactionSystemException ex2) {
logger.error("Application exception overridden by rollback exception", ex);
ex2.initApplicationException(ex);
throw ex2;
}
catch (RuntimeException | Error ex2) {
logger.error("Application exception overridden by rollback exception", ex);
throw ex2;
}
}
}
Views: 729 · Posted: 2024-05-30
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...