Home  >  Article  >  Java  >  SpringBoot transaction management example analysis

SpringBoot transaction management example analysis

WBOY
WBOYforward
2023-05-13 14:13:20624browse

1. Definition of transaction

A transaction is a logical execution unit consisting of a sequence of N-step database operations. This series of operations is either all executed or all given up.

2. Characteristics of transactions

Characteristics of transactionsACID:

  • Atomicity: Transactions are indivisible in the application Minimum execution body

  • Consistency: The result of transaction execution must make the data change from one consistency state to another consistency state

  • Isolation Persistence: The execution of each transaction does not interfere with each other, and the internal operations of any transaction are isolated from other transactions

  • Persistence: Once the transaction is submitted, any modifications to the data must Recorded in permanent storage

3. Transaction isolation

Common concurrency exceptions

  • The first type of lost update , The second type of lost update

  • Dirty read, non-repeatable read, phantom read

Common isolation levels

  • Read Uncommitted: Read uncommitted data

  • Read Committed: Read submitted data

  • Repeatable Read: Repeatable read

  • Serializable: Serialization

The first type of update loss: the rollback of a transaction causes another transaction to have Updated data is lost.

The second type of update loss: the submission of a certain transaction causes the data updated by another transaction to be lost.

Dirty read: A certain transaction reads uncommitted data from another transaction.

Non-repeatable reading: The results of a certain transaction reading the same data before and after are inconsistent.

Phantom reading: In a certain transaction, the number of rows queried before and after the same table is inconsistent.

##Read UncommittedYesYesYesYesYes##Read Commited##Repeatable ReadNoNoNoNoYesRepeatable ReadNoNoNoNoNo4. Transaction management
Isolation level First type of lost update Dirty read Second type of lost update Unrepeatable Read Phantom Read
No No Yes Yes Yes
Implementation mechanism

pessimistic Lock (database)

Shared lock (S lock): After transaction A adds a shared lock to certain data, other transactions can only add shared locks to the data, but cannot add exclusive locks.
  • Exclusive lock (X lock): After transaction A adds an exclusive lock to certain data, other transactions can neither add shared locks nor exclusive locks to the data.
  • Optimistic locking (custom)

Version number, timestamp, etc.
  • Updating Before retrieving data, check whether the version number has changed. If a change occurs, cancel this update, otherwise update the data (version number 1)
  • Spring transaction management
Declarative transaction

Declare the transaction characteristics of a method through XML configuration.
  • Declare the transaction characteristics of a method through annotations.
  • Programmatic transactions

Manage transactions through TransactionTemplate and perform database operations through it.
  • 5. Example
  • package com.nowcoder.community.service;
    import com.nowcoder.community.dao.AlphaDao;
    import com.nowcoder.community.dao.DiscussPostMapper;
    import com.nowcoder.community.dao.UserMapper;
    import com.nowcoder.community.entity.DiscussPost;
    import com.nowcoder.community.entity.User;
    import com.nowcoder.community.util.CommunityUtil;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.TransactionDefinition;
    import org.springframework.transaction.TransactionStatus;
    import org.springframework.transaction.annotation.Isolation;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    import org.springframework.transaction.support.TransactionCallback;
    import org.springframework.transaction.support.TransactionTemplate;
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    import java.util.Date;
    @Service
    //@Scope("prototype")
    public class AlphaService {
        @Autowired
        private AlphaDao alphaDao;
        @Autowired
        private UserMapper userMapper;
        @Autowired
        private DiscussPostMapper discussPostMapper;
        @Autowired
        private TransactionTemplate transactionTemplate;
        public AlphaService() {
    //        System.out.println("实例化AlphaService");
        }
        @PostConstruct
        public void init() {
    //        System.out.println("初始化AlphaService");
        }
        @PreDestroy
        public void destroy() {
    //        System.out.println("销毁AlphaService");
        }
        public String find() {
            return alphaDao.select();
        }
        // REQUIRED: 支持当前事务(外部事务),如果不存在则创建新事务.
        // REQUIRES_NEW: 创建一个新事务,并且暂停当前事务(外部事务).
        // NESTED: 如果当前存在事务(外部事务),则嵌套在该事务中执行(独立的提交和回滚),否则就会REQUIRED一样.
        @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
        public Object save1() {
            // 新增用户
            User user = new User();
            user.setUsername("alpha");
            user.setSalt(CommunityUtil.generateUUID().substring(0, 5));
            user.setPassword(CommunityUtil.md5("123" + user.getSalt()));
            user.setEmail("alpha@qq.com");
            user.setHeaderUrl("https://cache.yisu.com/upload/information/20220823/112/6198.png");
            user.setCreateTime(new Date());
            userMapper.insertUser(user);
            // 新增帖子
            DiscussPost post = new DiscussPost();
            post.setUserId(user.getId());
            post.setTitle("Hello");
            post.setContent("新人报道!");
            post.setCreateTime(new Date());
            discussPostMapper.insertDiscussPost(post);
            Integer.valueOf("abc");
            return "ok";
        }
        public Object save2() {
            transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
            transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
            return transactionTemplate.execute(new TransactionCallback<Object>() {
                @Override
                public Object doInTransaction(TransactionStatus status) {
                    // 新增用户
                    User user = new User();
                    user.setUsername("beta");
                    user.setSalt(CommunityUtil.generateUUID().substring(0, 5));
                    user.setPassword(CommunityUtil.md5("123" + user.getSalt()));
                    user.setEmail("beta@qq.com");
                    user.setHeaderUrl("https://cache.yisu.com/upload/information/20220823/112/6199.png");
                    user.setCreateTime(new Date());
                    userMapper.insertUser(user);
                    // 新增帖子
                    DiscussPost post = new DiscussPost();
                    post.setUserId(user.getId());
                    post.setTitle("你好");
                    post.setContent("我是新人!");
                    post.setCreateTime(new Date());
                    discussPostMapper.insertDiscussPost(post);
                    Integer.valueOf("abc");
                    return "ok";
                }
            });
        }
    }

The above is the detailed content of SpringBoot transaction management example analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete