Home > Java > javaTutorial > How to use springboot programmatic transaction TransactionTemplate

How to use springboot programmatic transaction TransactionTemplate

PHPz
Release: 2023-06-01 17:40:24
forward
2100 people have browsed it

Use of TransactionTemplate

Summary: Inject TransactionTemplate into the class to use programmatic transactions in springboot.

spring supports two methods: programmatic transaction management and declarative transaction management.

Programmatic transaction management uses TransactionTemplate or directly uses the underlying PlatformTransactionManager. Spring recommends using TransactionTemplate to manage programming transactions.

Declarative transaction management is built on AOP. Its essence is to intercept the method before and after, and then create or join a transaction before the target method starts. After the target method is executed, the transaction is committed or rolled back according to the execution status. Spring Boot recommends using the @Transactional annotation to implement declarative transaction management.

1. Why use it?

In most cases, just declare the @Transactional annotation on the method to declare the transaction, which is simple, fast and convenient. However, the controllability of the @Transactional declarative transaction is too weak, and it can only be declared on the method or class. Fine-grained transaction control is not possible.

If the first 10 sql statements of a method are all select query statements, and only the last 2 sql statements are update statements, then only the last 2 sql statements can be processed.

2. How to use

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
Copy after login

to introduce the mybatis-spring-boot-starter dependency package into springboot.

The mybatis-spring-boot-starter dependency package contains the dependencies of spring-boot-starter-jdbc. spring-boot-starter-jdbc contains the DataSourceTransactionManager transaction manager and the automatic injection configuration class DataSourceTransactionManagerAutoConfiguration.

Used in code, just inject TransactionTemplate into the bean:

@Service
public class TestServiceImpl {
    @Resource
    private TransactionTemplate transactionTemplate;
 
    public Object testTransaction() {
        //数据库查询
        dao.select(1);
        return transactionTemplate.execute(status -> {
            //数据库新增
            dao.insert(2);
            dao.insert(3);
            return new Object();
        });
    }
}
Copy after login

TransactionTemplate is simple to use

/**
 * 事务模板
 * @author zz
 *
 */ 
public class TransactionTemplateSupport {
 
	@Autowired
	private PlatformTransactionManager transactionManager;	
	private TransactionTemplate requiredTransactionTemplate;	
	protected TransactionTemplate getRequiresNewTransactionTemplate(){
		if (requiredTransactionTemplate == null){
			requiredTransactionTemplate = new TransactionTemplate(transactionManager);
			requiredTransactionTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRED);
		//	requiredTransactionTemplate.setReadOnly(true);
		//	requiredTransactionTemplate.setTimeout(30000);
		}
		
		return requiredTransactionTemplate;
	} 
}
Copy after login
 @Service
public class TestTransaction extends TransactionTemplateSupport  { 
    @Autowired
    private JdbcTemplate jdbcTemplate ;
 
    @Autowired
    private TransactionTemplate transactionTemplate;
 
   // @Transactional
    public void test(){
 
        jdbcTemplate.execute("insert into user value (1,&#39;aaa&#39;,&#39;aaa&#39;,&#39;aaa&#39;)"); 
        int i = 1/0; 
        jdbcTemplate.execute("insert into user value (2,&#39;aaa&#39;,&#39;aaa&#39;,&#39;aaa&#39;)"); 
    } 
 
    public void test2(){ 
      getRequiresNewTransactionTemplate()
      //  transactionTemplate
                .execute(new TransactionCallback<Void>() {
 
            @Override
            public Void doInTransaction(TransactionStatus status) {  
                jdbcTemplate.execute("insert into user value (11,&#39;BBBB&#39;,&#39;aaa&#39;,&#39;aaa&#39;)");
 
                int i = 1/0; 
                jdbcTemplate.execute("insert into user value (21,&#39;aaa&#39;,&#39;NNNN&#39;,&#39;aaa&#39;)");  
                return null;
            }
        });
    }  
}
Copy after login

The above is the detailed content of How to use springboot programmatic transaction TransactionTemplate. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template