Transaction management (keep in mind after reading)

Posted by phpconnect on Sat, 25 Dec 2021 15:24:24 +0100

transaction management

In short, I need to manage these affairs, hahaha
So why is there such a thing? Because when the program is in the concurrent state, the data may be read and modified by multiple people at the same time, and errors will occur. Therefore, transaction management is required

Possible errors in concurrent state:

Type I missing updates

  • To put it simply, I read a number, and then you read it, and then you modify it and commit it, and I rollback the transaction, and then your update is overwritten by my update.

Type 2 missing updates

  • It's not very different from the first type, that is, my update overwrites your update, and then you wonder why?
  • Because the programmer didn't handle the affairs, ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha

Dirty reading

  • This is the problem that occurs only when there is no transaction at all. Ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha

Non repeatable read

  • Is that you read the same data twice and find it different. Are you confused?

Unreal reading

  • The only difference is that the data is different, and the number of rows I read is different.
  • In fact, we can accept this mistake. It's not a big problem.

Then, the five kinds of errors can be handled in our transaction management. With our transaction isolation level, we can solve the corresponding problems

The four isolation levels are:

  1. Read uncommitted
  2. Read committed
  3. Repeat reading
  4. Serialization

Of course, the higher the level, the more secure it is, but correspondingly, its performance will be worse and worse. Because we will keep locking and unlocking the data.

So how do they implement it in code? Let's have a look

  • First of all, there are two solutions in idea,
  • One way is to use annotations, called declarative transactions, which is simple, clear, non verbose and convenient.
  • Another is programmatic transaction management, which requires us to manually write the program logically.

Declarative transaction:

    /**
     * This is an annotated transaction, which is very convenient to use
     * REQUIRED         The current transaction is supported. If it does not exist, a new transaction will be created
     * REQUIRES_NEW     Create a new transaction and pause the current transaction,
     * NESTED           If the current transaction exists, it is nested in the transaction (independent commit and rollback). Otherwise, it is the same as REQUIRED.
     */
    @    /**
     * This is an annotated transaction, which is very convenient to use
     * REQUIRED         The current transaction is supported. If it does not exist, a new transaction will be created
     * REQUIRES_NEW     Create a new transaction and pause the current transaction,
     * NESTED           If the current transaction exists, it is nested in the transaction (independent commit and rollback). Otherwise, it is the same as REQUIRED.
     */
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public String save1(){
        User user = new User();
        user.setUsername("Amumu Test No. 1");
        userMapper.insertUser(user);
        Integer.parseInt("abc");
        return "ok";
    }(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public String save1(){
        User user = new User();
        user.setUsername("Amumu Test No. 1");
        userMapper.insertUser(user);
        Integer.parseInt("abc");
        return "ok";
    }
  • Just use the Transactional annotation directly. The parameters to be placed in it are the isolation level and propagation method
  • Then it's OK. When an error is encountered, it will be rolled back and no commit operation will be performed.

Programming transactions:

  • This is provided by a Bean of string, so we need to inject this Bean first
    
    @Autowired
    private TransactionTemplate transactionTemplate;
    
    public Object save2(){
        transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        // These do not require transaction management
        User user = new User();
        user.setUsername("Amumu Test No. 1");
        // These require transaction management
        return transactionTemplate.execute(new TransactionCallback<Object>() {
            @Override
            public Object doInTransaction(TransactionStatus transactionStatus) {

                userMapper.insertUser(user);
                Integer.parseInt("abc");
                return "ok";
            }
        });
    }
  • The TransactionTemplate object is a template for transaction management, which sets how to manage transactions.
  • okok. I'll come back and have a look when I forget.

Topics: Java Database MySQL