常用的开启事务方式
- 注解的方式,通过@Transactional,直接注释在方法上,然后可以通过参数来配置。
- 声明的方式,在需要回滚的代码前加上开启,最后加关闭。
@Autowired
private PlatformTransactionManager transactionManager;
TransactionStatus status = transactionManager
.getTransaction(new DefaultTransactionDefinition());
transactionManager.commit(status);
transactionManager.rollback(status);
- 通过Java Bean代码配置的方式,来全局控制方法名来控制事务,通过方法名的前缀来使用事务,比如简单的get,find这类的是简单的查询,我们就用只读事务,如 add,update 开头的方法,我们用传播事务,还有一些特殊的,可以直接通过
source.addTransactionalMethod(“action*”, required);
来添加。
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionInterceptor;
import java.util.Collections;
@Configuration
@MapperScan(basePackages = "com.xxx.xxx.mapper.*")
public class TransactionConfig {
public static final String[] REQUIRED_READONLY = new String[]{"find*", "get*", "select*", "list*", "search*", "query*", "count*", "look*", "is*"};
public static final String[] REQUIRED = new String[]{"add*", "new*", "insert*", "create*", "save*", "edit*", "modify*", "update*", "set*", "exchange*", "exec*", "execute*", "delete*", "handle*", "batch*", "bind*", "gain*", "unload*", "invalid*", "liquidation*", "cancle*", "rollback*", "build*", "draw*", "reward*", "accept*", "complete*", "send*", "give*", "push*", "put*"};
@Bean(name = "txAdvice")
@Primary
public TransactionInterceptor txAdvice(TransactionManager transactionManager) {
RuleBasedTransactionAttribute required = new RuleBasedTransactionAttribute();
required.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
required.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
required.setTimeout(TransactionDefinition.TIMEOUT_DEFAULT);
RuleBasedTransactionAttribute readOnly = new RuleBasedTransactionAttribute();
readOnly.setReadOnly(true);
readOnly.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
for (String methodName : REQUIRED_READONLY) {
source.addTransactionalMethod(methodName, readOnly);
}
for (String methodName : REQUIRED) {
source.addTransactionalMethod(methodName, required);
}
source.addTransactionalMethod("action*", required);
return new TransactionInterceptor(transactionManager, source);
}
@Bean
@Primary
public BeanNameAutoProxyCreator txProxy() {
BeanNameAutoProxyCreator creator = new BeanNameAutoProxyCreator();
creator.setInterceptorNames("txAdvice");
creator.setBeanNames("*Service", "*ServiceImpl");
creator.setProxyTargetClass(true);
return creator;
}
}