Spring propagation mechanism-Propagation.REQUIRED

Keywords: Spring Mybatis Apache Session

;

Most of the statements on the Internet are "if there is a transaction, the current transaction is supported.". Open if no transaction "
It's just a silly face...
My understanding at the beginning is that if the save method is called, a transaction will be opened when the transaction is executed to userMapper.save. At this time, if the transaction is not finished, a request will be made
The second call to save will not open a new transaction
And then I tested
http://localhost:9082/coffee.controller/update?phone=15210712345
http://localhost:9082/coffee.controller/update?phone=15210712346
http://localhost:9082/coffee.controller/update?phone=15210712347
When the third request is rolled back, it was supposed to be the same transaction, and then all save operations are rolled back. It turns out that this is not the case, only the third request is rolled back
Description without nesting
Reopen a transaction every time the method save is called

@Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void save(UserRecord userParam) {
        logger.info("Start execution save {}, {}", userParam.getId(), userParam.getPhone());
        userMapper.save(userParam);
        try {
            logger.info("save complete---The value in the database is {} start sleep", userParam.getId());
            if ("15210712347".equals(userParam.getPhone())) {
                throw new RuntimeException();
            }
            Thread.sleep(1000 * 10);
            logger.info("sleep End");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
2017-12-23 21:15:38 [http-nio-9082-exec-7] INFO  c.c.service.impl.UserServiceImpl2 - Start execution save null, 15210712345
2017-12-23 21:15:38 [http-nio-9082-exec-7] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
2017-12-23 21:15:38 [http-nio-9082-exec-7] DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession 
## The first request opens a new transaction
[org.apache.ibatis.session.defaults.DefaultSqlSession@30bcad12]
2017-12-23 21:15:38 [http-nio-9082-exec-7] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [com.mysql.jdbc.JDBC4Connection@458059db] will be managed by Spring
2017-12-23 21:15:38 [http-nio-9082-exec-7] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@30bcad12]
2017-12-23 21:15:38 [http-nio-9082-exec-7] INFO  c.c.service.impl.UserServiceImpl2 - save complete---The value in the database is null start sleep
## The second request came in,
2017-12-23 21:15:40 [http-nio-9082-exec-1] INFO  c.c.service.impl.UserServiceImpl2 - Start execution save null, 15210712346
2017-12-23 21:15:40 [http-nio-9082-exec-1] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
## A new transaction has been opened. This transaction is different from requests? New in that it will not be suspended (that is, it will not be executed until the first transaction is completed),
2017-12-23 21:15:40 [http-nio-9082-exec-1] DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4c4d91be]
2017-12-23 21:15:40 [http-nio-9082-exec-1] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [com.mysql.jdbc.JDBC4Connection@5f424dee] will be managed by Spring
2017-12-23 21:15:40 [http-nio-9082-exec-1] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4c4d91be]
2017-12-23 21:15:40 [http-nio-9082-exec-1] INFO  c.c.service.impl.UserServiceImpl2 - save complete---The value in the database is null start sleep
## The second transaction has been opened. Start sleep
2017-12-23 21:15:48 [http-nio-9082-exec-7] INFO  c.c.service.impl.UserServiceImpl2 - sleepEnd
## First transaction start commit
2017-12-23 21:15:48 [http-nio-9082-exec-7] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@30bcad12]
2017-12-23 21:15:48 [http-nio-9082-exec-7] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@30bcad12]
2017-12-23 21:15:48 [http-nio-9082-exec-7] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@30bcad12]
## First transaction deregistering and then close
2017-12-23 21:15:50 [http-nio-9082-exec-1] INFO  c.c.service.impl.UserServiceImpl2 - sleepEnd
## Next, execute the second transaction, which is the same as the first one
2017-12-23 21:15:50 [http-nio-9082-exec-1] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4c4d91be]
2017-12-23 21:15:50 [http-nio-9082-exec-1] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4c4d91be]
2017-12-23 21:15:50 [http-nio-9082-exec-1] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4c4d91be]

Posted by drakkon on Tue, 05 May 2020 11:53:30 -0700