Transaction 01 (Spring Notes 011)

Transaction and save point

Transaction: Transaction, generally refers to something to be done or done. In computer terms, it refers to a program execution unit (unit) that accesses and may update various data items in the database.
A group of operations are either executed at the same time or not executed at the same time, such as transferring money.

Setting of transaction savepoint

Insert picture description here

Connection conn=null;
Savepoint savepoint =null;//保存点
try{
    
    
//1.获得链接
conn=~~~~;
//2.开启事务
conn.setAutoCommit(false);
A,B
savepoint = conn.setSavepoint();
C,D
//3.事务提交
conn.commit();
}catch(e){
    
    
    if(savepoint != null){
    
    
   conn.rollback(savepoint);//C,D异常
    conn.commit();//提交AB
    }else{
    
    conn.rollback();}
}

Spring transaction management interface

Transaction management interface {T ransaction D efinition (transaction definition, attributes, details) T ransaction Starus (transaction status) P latform T ransaction Manage (platform transaction manager) \mathrm{transaction management interface}\left\ {\begin{array}{l}TransactionDefinition(transaction definition, attributes, details)\\TransactionStatus(transaction status)\\PlatformTransactionManage(platform transaction manager)\end{array}\right. Business affairs tube management of the access portT R & lt A n- S A C T I O n- D E F I n- I T I O n- ( what service is given sense , the genus properties , details of the situation )T R & lt A n- S A C T I O n- S T A T U S ( what service is like state )P L A T F O R & lt m T R & lt A n- S A C T I O n- M A n- A G E ( level station things traffic pipe management device )
事 务 管 理 的 j a r 包 ( W E N − I N F / l i b ) { s p r i n g − t x − 3.2.0. R E L E S A 3 E . j a r P l a t f o r m T r a n s a c t i o n M a n a g e { s p r i n g − j d b c − 3.2.0. R E L E A S E . j a r s p r i n g − o r m − 3.2.0. R E L E A S E . j a r \begin{array}{c}\mathrm{事务管理的}jar包\\(WEN-INF/lib)\end{array}\left\{\begin{array}{l}spring-tx-3.2.0.RELESA3E.jar\\PlatformTransactionManage\left\{\begin{array}{l}spring-jdbc-3.2.0.RELEASE.jar\\spring-orm-3.2.0.RELEASE.jar\end{array}\right.\end{array}\right. What service pipe management of J A R & lt packet( W E NI N F / l i b )springtx3.2.0.RELESA3E.jarPlatformTransactionManage{ springjdbc3.2.0.RELEASE.jarspringo r m3.2.0.RELEASE.jar

The methods of TransactionDefinition transaction details are: description
①getPropagarionBehavior():int Communication behavior (how to share affairs between two businesses)
②getIsolationLevel():int Isolation level
③getTimeout():int Get timeout
④isReadOnly:boolean Read-only
⑤getName():String Configure the transaction details name (general method names such as add, save, etc.)
①There are 7 parameters for getPropagarionBehavior propagation behavior (how to share transactions between two businesses), the more important ones are * Explanation
TransactionDefinition.PROPAGATION_REQUIRED* If there is a transaction currently, Spring will use that transaction; otherwise, a new transaction will be started
TransactionDefinition.PROPAGATION_SUPPORTS If there is a transaction currently, Spring will use that transaction; otherwise, a new transaction will not be started
TransactionDefinition.PROPAGATION_MANDATORY If there is a transaction currently, Spring will use that transaction; otherwise, an exception will be thrown
TransactionDefinition.PROPAGATION_REQUIRES_NEW* Spring always starts a new transaction. If there is a transaction, the transaction is suspended
TransactionDefinition.PROPAGATION_NOT_SUPPORTED Spring will not execute the code in the transaction. The code is always executed in a non-transactional environment, if there is a current transaction, the transaction is suspended
TransactionDefinition. PROPAGATION_NEVER Even if there is a transaction currently, Spring will execute in a non-transactional environment. If there is currently a transaction, throw an exception
TransactionDefinition. PROPAGATION_NESTED If there is a transaction currently, it is executed in a nested transaction. If not, start a new transaction. The bottom layer of transaction A and B uses a savepoint mechanism to form a nested transaction

Guess you like

Origin blog.csdn.net/ResumeProject/article/details/112927386