MVCC principle-software transaction memory STM (shallow entry)

First of all, let ’s first explain the relationship of the topic:
MVCC (multi-version concurrency control): the implementation of
database transactions The transaction mechanism in the database is very simple to use, much stronger than java locks and atomic classes.
Therefore, in order to improve performance, the software transaction memory STM is generated by imitating the transaction mechanism of the database. (STM will be provided by JAVA third parties)

Below we show STM with three pieces of code:

这个是我们用java来模拟实现转账的业务,可以发现这个是线程不安全的,需要我们加锁,同时要防止死锁等问题。
class UnsafeAccount {
  //余额
  private long balance;
  //构造函数
  public UnsafeAccount(long balance) {
    this.balance = balance;
  }
  //转账
  void transfer(UnsafeAccount target, long amt){
    if (this.balance > amt) {
      this.balance -= amt;
      target.balance += amt;
    }
  }
}

If you use database transactions, it will be very simple, you can guarantee the ACID (atomicity, consistency, isolation, durability) of the transaction, and there is no problem such as deadlock.

Connection conn = null;
try{
  //获取数据库连接
  conn = DriverManager.getConnection();
  //设置手动提交事务
  conn.setAutoCommit(false);
  //执行转账SQL
  ......
  //提交事务
  conn.commit();
} catch (Exception e) {
  //出现异常回滚事务
  conn.rollback();
}

Using the STM to achieve the transfer operation, java does not support STM, we may introduce a third party libraries Multiverse achieve

class Account{
  //余额的类型Long->TxnLong
  private TxnLong balance;
  //构造函数
  public Account(long balance){
    this.balance = StmUtils.newTxnLong(balance);
  }
  //转账
  public void transfer(Account to, int amt){
    //原子化操作
    atomic(()->{
      if (this.balance.get() > amt) {
        this.balance.decrement(amt);
        to.balance.increment(amt);
      }
    });
  }
}

MVCC principle

In fact, MVCC can be understood as a snapshot of the database when the database transaction is opened, and all read and write operations in the transaction will be based on this snapshot in the future. When there is no change during the execution of the transaction, the data is submitted. If there is a change, it means that during the execution of the transaction, other transactions were submitted, conflicts occurred, and cannot be submitted.
So how to judge whether the data has changed? Add a version number to each piece of data, and each time you modify the data, you will increase the value of the version number.

Implement STM yourself

According to the MVCC principle, each modification of the data corresponds to a version number, there is no only modification of the data or version number, so here we can useImmutability modelEncapsulate the data and version number into immutable objects. More in line with concurrent programming.

Invariance pattern reference, this concurrent design pattern 1-avoid sharing articles.

If you want to see the specific implementation, please see the implementation of STM

Published 34 original articles · Likes0 · Visits 1089

Guess you like

Origin blog.csdn.net/qq_42634696/article/details/104690673