Spring Boot—13 Transaction Support

pom.xml

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

App class

package com.smartmap.sample.ch1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
@SpringBootApplication
public class Ch1Application {

    public static void main(String[] args) {
        SpringApplication.run(Ch1Application.class, args);

    }

}


Service class

package com.smartmap.sample.ch1.service.impl;

import java.util.LinkedList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.smartmap.sample.ch1.entity.User;
import com.smartmap.sample.ch1.service.UserService;

@Service
public class UserServiceImpl implements UserService {

    @Override
    public List<User> allUser() {
        List<User> userList = new LinkedList<User>();
        userList.add(new User("123", "root"));
        return userList;
    }

    @Override
    public User getUserById(Long id) {
        return new User("123", "root");
    }

    @Transactional
    @Override
    public User save(User user) {
        return new User("123", "root");
    }

   @Transactional
    @Override
    public int delete(Long userId) {
        return 1;
    }

}


1. Isolation : isolation level

The isolation level refers to the degree of isolation between several concurrent transactions. The main scenarios related to our development include: dirty reads, repeated reads, and phantom reads.

We can see that the org.springframework.transaction.annotation.Isolation enumeration class defines five values ​​representing isolation levels:

public enum Isolation {  
    DEFAULT(-1),
    READ_UNCOMMITTED(1),
    READ_COMMITTED(2),
    REPEATABLE_READ(4),
    SERIALIZABLE(8);
}


DEFAULT : This is the default value, which means to use the default isolation level of the underlying database. For most databases, this value is usually: READ_COMMITTED .
READ_UNCOMMITTED : This isolation level indicates that a transaction can read data modified by another transaction but not yet committed. This level does not prevent dirty reads and non-repeatable reads, so this isolation level is rarely used.
READ_COMMITTED : This isolation level means that a transaction can only read data that has been committed by another transaction. This level prevents dirty reads and is the recommended value in most cases.
REPEATABLE_READ : This isolation level means that a transaction can repeat a query multiple times throughout the process, and each time the records are returned the same. Even if there is new data between multiple queries to satisfy the query, these new records will be ignored. This level prevents dirty reads and non-repeatable reads.
SERIALIZABLE: All transactions are executed one by one, so that there is absolutely no possibility of interference between transactions, that is, this level can prevent dirty reads, non-repeatable reads, and phantom reads. But this will seriously affect the performance of the program. Normally this level is not used either.
Specify the method: by using the isolation property setting, for example: @Transactional(isolation = Isolation.DEFAULT)

2. Propagation: Propagation Behavior

The so-called transaction propagation behavior means that if a transaction context already exists before starting the current transaction, there are several options to specify the execution behavior of a transactional method.

We can see that the org.springframework.transaction.annotation.Propagation enumeration class defines six enumeration values ​​representing propagation behavior:

public enum Propagation {  
    REQUIRED(0),
    SUPPORTS(1),
    MANDATORY(2),
    REQUIRES_NEW(3),
    NOT_SUPPORTED(4),
    NEVER(5),
    NESTED(6);
}


REQUIRED : If a transaction currently exists, join the transaction; if there is no current transaction, create a new transaction.
SUPPORTS: If a transaction currently exists, join the transaction; if there is no current transaction, continue running in a non-transactional manner.
MANDATORY : If there is currently a transaction, join the transaction; if there is no current transaction, throw an exception.
REQUIRES_NEW : Create a new transaction, if there is a current transaction, suspend the current transaction.
NOT_SUPPORTED: Runs in non-transactional mode, suspends the current transaction if there is a current transaction.
NEVER : Runs non-transactionally, throws an exception if a transaction currently exists.
NESTED : If a transaction currently exists, create a transaction to run as a nested transaction of the current transaction; if there is no current transaction, the value is equivalent to REQUIRED.
Specify the method: by using the propagation property setting, for example: @Transactional(propagation = Propagation.REQUIRED)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324771514&siteId=291194637