Halfway through the execution of the transaction, the business system is down, what will happen to the data in the database?

Halfway through the execution of the transaction, the business system is down, what will happen to the data in the database?

The business system submitted a database dml command, but before submitting it, the system crashed. So will the data in the database be before modification or after modification? Will the connection be dropped in the database and will the data be locked? With these questions in mind, we conducted the following tests:

1. Insert a piece of data into the database

insert image description here

At this point, the user password in the database is: test

2. View the connection to the database

show FULL PROCESSLIST;

insert image description here

There is a connection at this time, which is the client connection of navicat

3. View database transactions

SELECT * FROM information_schema.INNODB_TRX;

insert image description here

no transaction

4. Execute the modification statement in the business system

@Transactional(rollbackFor = Exception.class)
    public void update(){
    
    
        UserEntity userEntity = new UserEntity();
        userEntity.setId(2);
        //修改数据库的用户密码为 test112
        userEntity.setPassword("test112");
 
        userMapper.updateById(userEntity);
    }
 
    @Transactional(rollbackFor = Exception.class)
    public void shutdown(){
    
    
        update();
 
        try {
    
    
            //模拟长事务,方便我们测试系统宕机
            Thread.sleep(5000000);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
    }
 
@Test
    public void testShutDown(){
    
    
 
        userService.shutdown();
    }

Execute the testShutDown method

5. Check the database connection again

insert image description here

There are a lot more connections, because I use the database connection pool in the code, and 5 connections are created by default

6. Check the database transaction again

insert image description here

insert image description here

At this point there is a running transaction and a row is locked

7. We directly kill the process of the business system

taskkill /f /pid 13964

8. Check the database connection

insert image description here

The database connection has been closed

9. View database transactions

insert image description here

business is gone

10. View data

insert image description here

data unchanged

Experimental parameters:

The above test was tested on a windows 10 machine, mysql version 5.7.30 community edition, using the druid connection pool

On centos 7.5, other conditions remain unchanged, and the same result as above is obtained

On windows10, use jdbc connection, do not use connection pool, other conditions remain unchanged, get the same result as above

in conclusion:

When the system submits the database dml command but does not execute the commit, the system crashes. At this time, the data in the database will not change, the database connection will be cleared, the database transaction will also be cleared, and the data will not be locked

Guess you like

Origin blog.csdn.net/qq_57581439/article/details/130605905