• 事务三大接口


    1.PlatformTransactionManager 事务管理器

    2.TransactionDefinition 事务的一些基础信息,如超时时间、隔离级别、传播属性等

    3.TransactionStatus 事务的一些状态信息,如是否一个新的事务、是否已被标记为回滚

    1.PlatformTransactionManager 
     1 public interface PlatformTransactionManager {
     2  
     3     //根据事务定义TransactionDefinition,获取事务
     4     TransactionStatus getTransaction(TransactionDefinition definition);
     5  
     6     //提交事务
     7     void commit(TransactionStatus status);
     8  
     9     //回滚事务
    10     void rollback(TransactionStatus status);
    11 
    12 }

    2.事务定义接口TransactionDefinition 

    事务的定义包括: 事务的隔离级别,事务的传播属性,超时时间设置,是否只读

    事务的隔离级别是数据库本身的事务功能,事务的传播属性则是spring为我们提供的功能

    该接口的实现DefaultTransactionDefinition,默认的事务定义

    1 public class DefaultTransactionDefinition implements TransactionDefinition, Serializable {
    2     private int propagationBehavior = PROPAGATION_REQUIRED;
    3     private int isolationLevel = ISOLATION_DEFAULT;
    4     private int timeout = TIMEOUT_DEFAULT;
    5     private boolean readOnly = false;
    6     //
    7 }

    2.事务接口定义 TransactionStatus

    TransactionStatus它继承了SavepointManager接口,SavepointManager是对事务中上述保存点功能的封装,如下:

     

    1 public interface SavepointManager {
    2     Object createSavepoint() throws TransactionException;
    3     void rollbackToSavepoint(Object savepoint) throws TransactionException;
    4     void releaseSavepoint(Object savepoint) throws TransactionException;
    5 
    6 }

    TransactionStatus本身更多存储的是事务的一些状态信息

    是否是一个新的事物 是否有保存点 是否已被标记为回滚

    整个流程:

     1 @Autowired
     2 private PlatformTransactionManager transactionManager;
     3  
     4 TransactionStatus status = null;
     5 // 手动开启事务
     6 status = transactionManager.getTransaction(new DefaultTransactionDefinition());
     7  
     8 // 事务提交
     9 transactionManager.commit(status);
    10  
    11 // 事务回滚
    12 if (StringMoreUtils.checkValNotNull(status)) {
    13     transactionManager.rollback(status);
    14 }

     

  • 相关阅读:
    TeamWork#3,Week5,Scrum Meeting 11.16
    TeamWork#3,Week5,Scrum Meeting 11.6, 11.7, 11.11, 11.12
    TeamWork#3,Week5,Scrum Meeting 11.15
    TeamWork#3,Week5,Scrum Meeting 11.14
    TeamWork#3,Week5,Scrum Meeting 11.13
    TeamWork#3,Week5,Scrum Meeting 11.9
    TeamWork#3,Week5,Bing Input Method vs Sogou Input Method
    TeamWork#3,Week5,Scrum Meeting 11.4
    Daily Scrum10 11.14
    Daily Scrum9 11.13
  • 原文地址:https://www.cnblogs.com/penphy/p/12550927.html
Copyright © 2020-2023  润新知