1. Big business:
The transactions corresponding to the overall task have a relatively long run time and have not been committed for a long time.
2. The dangers of major affairs:
a. In concurrency, the database connection pool resources are full. Large transactions are not submitted in time, resulting in slow release of connection resources.
b. Database deadlock and lock waiting. In the context of mysql innodb storage engine, if a transaction occupies an exclusive lock, it will easily lead to data deadlock or lock waiting in concurrency.
c. Large transaction Rt is long, which can easily cause interface timeout.
d. Large transactions roll back for a long time.
e. Under the database master-slave architecture, data synchronization delay
3. Solution
3.1 Reasonably replace the @Transactional method of declarative transactions with the TransactionTemplate method of programming transactions
The minimum granularity of declarative transactions is the entire method, which may cause unnecessary logic to be added to transactions in the business. Programming transaction refinement requires adding transaction logic to form practical and useful transaction blocks.
@Autowired private TransactionTemplate transactionTemplate; public void testTransaction() { (new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) { try { // .... Business code } catch (Exception e){ //rollback (); } } }); }
3.2 Put the query outside the transaction method
Using @Transactional also wants to avoid large transactions, so the method needs to be split to separate the logic that does not require transaction management from transaction operations
@Service public class TransactionTestService{ // Avoid the transaction invalidation of internal methods of the same class by calling each other and the instance method calls the proxy method. @Resource private TransactionTestService service; public void create(ParamDto dto){ queryData1(); queryData2(); (dto); } //Transaction operation @Transactional(rollbackFor = ) public void save(ParamDto dto){ (dto); } }
3.3 Avoid remote calls across services
Communication between services and call time between services may be time-consuming due to the network environment and the Rt time of the remote interface.
Counterexample: //Transaction operation @Transactional(rollbackFor = ) public void save(ParamDto dto){ // Other services have been called otherRemoteApi(); (dto); } Modified to: @Autowired private TransactionTemplate transactionTemplate; public void save(ParamDto dto){ // Other services have been called otherRemoteApi(); (new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) { try { (dto); } catch (Exception e){ //rollback (); } } }); }
3.4 The transaction should not process too much data at once, and it can be executed in batches.
3.5 Methods in transactions can be executed asynchronously according to business usage
This is the end of this article about Springboot @Transactional's major transaction processing. For more related Springboot @Transactional's major transaction content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!