What is a global transaction Spring Boot(Spring) transactions are implemented through AOP (AOP related terminology: Advice, Joinpoint, Pointcut, Aspect, Target, Proxy, Weaving are implemented by section programming, at which point we can transact to the service method of the specified package Control.
How to configure Spring Boot global transaction Spring Boot with a transaction is very simple, only need to add annotations on the configuration class or start class @ EnableTransactionManagement open transaction support, and then add annotations in the service layer Transactional(rollbackFor = exception.class). Below is the global transaction code implementation
Special remind: mysql corresponding table must be InnoDB type to support transactions, myisam doesn't support transaction package com. Test. Sketelon. Util. Config. import org.aspectj.lang.annotation.Aspect; import org.springframework.aop.Advisor; import org.springframework.aop.aspectj.AspectJExpressionPointcut; import org.springframework.aop.support.DefaultPointcutAdvisor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.interceptor.*; import java.util.Collections; import java.util.HashMap; import java.util.Map; /** * Global transaction configuration ** REQUIRED: If a transaction currently exists, join the transaction; If there is no transaction currently, a new transaction is created. * SUPPORTS: Joins a transaction if it currently exists; If there is no transaction currently, it continues in a non-transactional manner. * MANDATORY: Joins a transaction if it currently exists. If there is no transaction currently, an exception is thrown. REQUIRES_NEW: Creates a new transaction and suspends the current transaction if one exists. NOT_SUPPORTED: Run in non-transactional mode and suspend the current transaction if one exists. * NEVER: runs non-transactionally and throws an exception if a transaction currently exists. * NESTED: Creates a transaction to run as a NESTED transaction of the current transaction if a transaction exists; If there are no transactions, this value is equivalent to REQUIRED. Transactional(Propagation = Propagation.REQUIRED) * See article; https://blog.csdn.net/schcilin/article/details/93306826 */@Aspect@Configurationpublic class TransactionAdviceConfig { Private final static int tx_time_out = 10; private final static int tx_time_out = 10; /** * The global transaction location configures where transactions are required: * Configure the pointcut expression, where the expression body * 2. The first *, said the return type, all types of * * 3. com.schcilin.goods.service said package name * 4 point. Second * : indicates the implementation package * 5.*(..) * indicates all method names,.. Said to all types of parameters * / private static final String POITCUT_EXPRESSION = "execution (* com. Test. Sketelon. Service. *. * (..) ) "; @Autowired private PlatformTransactionManager platformTransactionManager; @bean public TransactionInterceptor txadvice() {/** Config transaction management rule nameMap declaration has the name of the method that needs to manage the transaction. Public void addTransactionalMethod(String methodName, TransactionAttribute attr) */ NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource(); Map<String, TransactionAttribute> nameMap = new HashMap<>(16); / * read-only things, do not update delete * / / * * / transaction management rules RuleBasedTransactionAttribute readOnlyRule = new RuleBasedTransactionAttribute (); /* Sets whether the current transaction is a read-only transaction, true: readonlyrule-setreadOnly (true); /* TransactionDefinition defines the transaction isolation level; * PROPAGATION_REQUIRED If no transaction exists, create a new transaction, or if one already exists, To join in this transaction. * / readOnlyRule setPropagationBehavior (TransactionDefinition. PROPAGATION_REQUIRED); RuleBasedTransactionAttribute requireRule = new RuleBasedTransactionAttribute(); / * throw an Exception executed after cutting the rollback. * / requireRule setRollbackRules (Collections. SingletonList (new RollbackRuleAttribute (Exception. Class))); /*PROPAGATION_REQUIRED: The transaction isolation is 1, and if a transaction exists, join it. If there is no transaction currently, a new transaction is created. This is the default value. */ requireRule.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); SetTimeout (TX_METHOD_TIME_OUT); / / requireRule. SetTimeout (TX_METHOD_TIME_OUT) nameMap.put("add*", requireRule); nameMap.put("save*", requireRule); nameMap.put("insert*", requireRule); nameMap.put("update*", requireRule); nameMap.put("delete*", requireRule); nameMap.put("remove*", requireRule); /* Namemap. put(" Batch *", requireRule); nameMap.put("get*", readOnlyRule); nameMap.put("query*", readOnlyRule); nameMap.put("find*", readOnlyRule); nameMap.put("select*", readOnlyRule); nameMap.put("count*", readOnlyRule); source.setNameMap(nameMap); return new TransactionInterceptor(platformTransactionManager, source); } / * * * Settings section = tangent point pointcut + notice TxAdvice @ return * * * / @ Bean public Advisor txAdviceAdvisor () {AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression(POITCUT_EXPRESSION); return new DefaultPointcutAdvisor(pointcut, txadvice()); }}Copy the code
Spring manages the creation, commit, and rollback of the entire transaction in aop aspect programming. Support for programmatic and declarative transactions. In real projects, declarative transactions are relatively simple to use, but programmatic transactions are needed for smaller operations. In the real world, it is possible to use multiple transaction managers (JDBC and JTA). At this point, you need to specify the transaction manager