TransactionTemplate transactionTemplate =newTransactionTemplate(transactionManager);
JdbcTemplate jdbcTemplate =newJdbcTemplate(transactionManager.getDataSource());
transactionTemplate.execute(newTransactionCallback<Object>(){@Overridepublic Object doInTransaction(TransactionStatus status){
DefaultTransactionStatus defaultTransactionStatus =(DefaultTransactionStatus) status;try{
jdbcTemplate.update("update user set name='xxx' where id = 1");
defaultTransactionStatus.createAndHoldSavepoint();
jdbcTemplate.update("update user set name='xxx' where id = 1");int i =5/0;}catch(Exception e){thrownewRuntimeException(e);}return null;}});
public Object invoke(MethodInvocation invocation)throws Throwable {// Work out the target class: may be {@code null}.// The TransactionAttributeSource should be passed the target class// as well as the method, which may be from an interface.
Class<?> targetClass =(invocation.getThis()!= null ? AopUtils.getTargetClass(invocation.getThis()): null);// Adapt to TransactionAspectSupport's invokeWithinTransaction...//调用父类TransactionAspectSupport的方法returninvokeWithinTransaction(invocation.getMethod(), targetClass, invocation::proceed);}
事务的实现要包含了三个对象
PlatformTransactionManager事务管理器,
TransactionAttribute 可以看到事务属性中配置的正式事务的一些配置
DefaultTransactionStatus 事务运行过程中的状态,设置回滚点、获取事务信息的
正在的代理逻辑在TransactionAspectSupport中,实现的过程如下:
protected Object invokeWithinTransaction(Method method,@Nullable Class<?> targetClass,final InvocationCallback invocation)throws Throwable {// If the transaction attribute is null, the method is non-transactional.
TransactionAttributeSource tas =getTransactionAttributeSource();final TransactionAttribute txAttr =(tas != null ? tas.getTransactionAttribute(method, targetClass): null);final TransactionManager tm =determineTransactionManager(txAttr);if(this.reactiveAdapterRegistry != null && tm instanceofReactiveTransactionManager){//(可以忽略)ReactiveTransactionManager事务管理器}//spring的事务管理器
PlatformTransactionManager ptm =asPlatformTransactionManager(tm);final String joinpointIdentification =methodIdentification(method, targetClass, txAttr);if(txAttr == null ||!(ptm instanceofCallbackPreferringPlatformTransactionManager)){// Standard transaction demarcation with getTransaction and commit/rollback calls.
TransactionInfo txInfo =createTransactionIfNecessary(ptm, txAttr, joinpointIdentification);
Object retVal;try{// This is an around advice: Invoke the next interceptor in the chain.// This will normally result in a target object being invoked.
retVal = invocation.proceedWithInvocation();}catch(Throwable ex){// target invocation exceptioncompleteTransactionAfterThrowing(txInfo, ex);throw ex;}finally{cleanupTransactionInfo(txInfo);}if(vavrPresent && VavrDelegate.isVavrTry(retVal)){// Set rollback-only in case of Vavr failure matching our rollback rules...
TransactionStatus status = txInfo.getTransactionStatus();if(status != null && txAttr != null){
retVal = VavrDelegate.evaluateTryFailure(retVal, txAttr, status);}}commitTransactionAfterReturning(txInfo);return retVal;}else{//调用其他类型的事务管理器}