当前位置:网站首页>Several classes of manual transactions

Several classes of manual transactions

2022-06-24 21:52:00 Jingling cat

The main categories

TransactionManager
 Insert picture description here

PlatformTransactionManager
 Insert picture description here

 Insert picture description here

TransactionStatus

 Insert picture description here

TransactionDefinition,TransactionTemplate

 Insert picture description here

Example

@Service
public class MyService{
    
    @Autowired
    DataSourceTransactionManager transactionManager;
    
    public ResultMap lockStockWhenNewOrder(List<StockChangeByOrderDto> goodsModels) {
    
        //2. Get transaction definition 
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        //3. Set the transaction isolation level , Start a new business 
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
        //4. Get transaction state , Equivalent to opening things 
        TransactionStatus transactionStatus = transactionManager.getTransaction(def);
        try{
    
            //insert or update ...
            transactionManager.commit(transactionStatus);
        } catch (InterruptedException e) {
    
            transactionManager.rollback(transactionStatus);
        }
    }
}

Create a tool class that controls transactions , And to spring management

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

@Component
public class TransationUtils {
    

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    private TransactionStatus transactionStatus;

    /** *  Open transaction  * * @return */
    public TransactionStatus beginTransaction() {
    
        transactionStatus = platformTransactionManager.getTransaction(new DefaultTransactionDefinition());
        return transactionStatus;
    }

    /** *  Commit transaction  */
    public void commitTransaction() {
    
        if (!transactionStatus.isCompleted()) {
    
            platformTransactionManager.commit(transactionStatus);
        }
    }

    /** *  Roll back the transaction  */
    public void rollbackTransaction() {
    
        if (!transactionStatus.isCompleted()) {
    
            platformTransactionManager.rollback(transactionStatus);
        }
    }
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
 
// Programming transactions ( Manual required begin  Manual rollback   Hand in )
@Component
public class TransactionUtils {
    
 
    //  Get transaction source 
    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;
 
    //  Open transaction 
    public TransactionStatus begin() {
    
        TransactionStatus transaction = dataSourceTransactionManager.getTransaction(new DefaultTransactionAttribute());
        return transaction;
    }
 
    //  Commit transaction 
    public void commit(TransactionStatus transaction) {
    
        dataSourceTransactionManager.commit(transaction);
    }
 
    //  Roll back the transaction 
    public void rollback(TransactionStatus transaction) {
    
        dataSourceTransactionManager.rollback(transaction);
    }
 
}

Inject the tool class into the class that needs to be used for transactions , And use

@Autowired
TransationUtils transationUtils;

public void test(){
    

	// Open transaction 
	transationUtils.beginTransaction();

	try {
    
		·············
		// The operation successfully committed the transaction 
		transationUtils.commitTransaction();
	} catch (Exception e) {
    
		// Catch exception rollback transaction 
		transationUtils.rollbackTransaction();
		throw new RuntimeException(" Failure !");
	}
}
 The method of automatically starting a transaction , The principle is to judge whether the transaction ends normally under various circumstances , If an exception is caught, roll back , If normal, submit 

Annotate the business @Transactional

springboot You can use annotations directly on the method @Transactional(rollbackFor = Exception.class)

Turn on transactions automatically , When an exception occurs, it will be rolled back automatically , however If you use try catch Transactions fail when , need Manually roll back the transaction , The specific operation is as follows

try {
    
      ······················
}catch (Exception e) {
    
   	// Manual rollback 
    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
}

Reference resources

Spring Business
Manual transactions and multithreaded transactions

原网站

版权声明
本文为[Jingling cat]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241450296754.html