springboot 事务操作示例


springboot 事务操作示例

 

******************************

相关注解

 

@Transactional

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Transactional {
    @AliasFor("transactionManager")
    String value() default "";

    @AliasFor("value")
    String transactionManager() default "";

    Propagation propagation() default Propagation.REQUIRED;
                //传播行为

    Isolation isolation() default Isolation.DEFAULT;
                //隔离级别

    int timeout() default -1;
                //超时时间,默认不限制

    boolean readOnly() default false;
                //是否只读

    Class[] rollbackFor() default {};

    String[] rollbackForClassName() default {};

    Class[] noRollbackFor() default {};

    String[] noRollbackForClassName() default {};
}

  

Propagation

public enum Propagation {
    REQUIRED(0),         //默认,事务不存在新建事务,
                        标注在被调用方法上时,调用方法的事务存在使用调用方法的事务
    SUPPORTS(1),
    MANDATORY(2),
    REQUIRES_NEW(3),     //常用,新建一个事务
    NOT_SUPPORTED(4),
    NEVER(5),
    NESTED(6);  //如果数据库支持保存点(savepoint)则使用保存点,否则新建一个事务

    private final int value;

    private Propagation(int value) {
        this.value = value;
    }

    public int value() {
        return this.value;
    }
}

 

Isolation

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

    private final int value;

    private Isolation(int value) {
        this.value = value;
    }

    public int value() {
        return this.value;
    }
}

 

******************************

示例

 

********************

config 层

 

  配置事务管理器,并开启事务

@Configuration
@EnableTransactionManagement
public class TransactionConfig {

    @Bean
    public PlatformTransactionManager initPlatformTransactionManager(DataSource dataSource){

        return new DataSourceTransactionManager(dataSource);
    }
}

 

********************

service 层

 

public interface CustomPersonService {

    void update(Person person);

    void insert(Person person);

}

 

*************************

serviceImpl 层

 

@Service
public class CustomPersonServiceImpl implements CustomPersonService {

    @Autowired
    private PersonMapper personMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void update(Person person) {
        personMapper.updateById(person);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void insert(Person person) {
        personMapper.insert(person);
    }

}

 

@Service
public class PersonServiceTest {

    @Autowired
    private CustomPersonService personService;

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void insertList(List personList){
        for(Person person:personList){
            personService.insert(person);
        }
    }
}

 

**********************

controller 层

 

@RestController
@RequestMapping("/person")
public class PersonController {

    @Autowired
    private PersonService personService;

    @Autowired
    private CustomPersonService customPersonService;

    @Autowired
    private PersonServiceTest personServiceTest;

    @RequestMapping("/save")
    public String save(){
        List list=new ArrayList<>();
        for(int i=0;i<10;i++){
            Person person=new Person();
            person.setName("瓜田李下"+i);
            person.setAge(i);

            list.add(person);
        }

        personServiceTest.insertList(list);

        return "success";
    }

    @RequestMapping("/update")
    public String update(){
        Person person=personService.getById(2);
        person.setName("海贼王");
        customPersonService.update(person);

        return "update success";
    }
}

 

 

****************************

控制台输出

 

2019-09-26 10:30:53.758 DEBUG 12032 --- [nio-8080-exec-1] o.s.j.d.DataSourceTransactionManager     : Initiating transaction commit
2019-09-26 10:30:53.758 DEBUG 12032 --- [nio-8080-exec-1] o.s.j.d.DataSourceTransactionManager     : Committing JDBC transaction on Connection [com.mysql.cj.jdbc.ConnectionImpl@1f647ec5]
2019-09-26 10:30:53.843 DEBUG 12032 --- [nio-8080-exec-1] o.s.j.d.DataSourceTransactionManager     : Releasing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@1f647ec5] after transaction
2019-09-26 10:30:53.844 DEBUG 12032 --- [nio-8080-exec-1] o.s.j.d.DataSourceTransactionManager     : Resuming suspended transaction after completion of inner transaction
2019-09-26 10:30:53.844 DEBUG 12032 --- [nio-8080-exec-1] o.s.j.d.DataSourceTransactionManager     : Initiating transaction commit
2019-09-26 10:30:53.844 DEBUG 12032 --- [nio-8080-exec-1] o.s.j.d.DataSourceTransactionManager     : Committing JDBC transaction on Connection [com.mysql.cj.jdbc.ConnectionImpl@321b632d]
2019-09-26 10:30:53.848 DEBUG 12032 --- [nio-8080-exec-1] o.s.j.d.DataSourceTransactionManager     : Releasing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@321b632d] after transaction
2019-09-26 10:30:53.880 DEBUG 12032 --- [nio-8080-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/html', given [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2019-09-26 10:30:53.880 DEBUG 12032 --- [nio-8080-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Writing ["success"]
2019-09-26 10:30:53.892 DEBUG 12032 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed 200 OK
2019-09-26 10:31:34.072 DEBUG 12032 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : GET "/person/update", parameters={}
2019-09-26 10:31:34.072 DEBUG 12032 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public java.lang.String com.example.demo.controller.PersonController.update()
2019-09-26 10:31:34.075 DEBUG 12032 --- [nio-8080-exec-3] o.s.jdbc.datasource.DataSourceUtils      : Fetching JDBC Connection from DataSource
2019-09-26 10:31:34.085 DEBUG 12032 --- [nio-8080-exec-3] o.s.j.d.DataSourceTransactionManager     : Creating new transaction with name [com.example.demo.serviceImpl.CustomPersonServiceImpl.update]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
2019-09-26 10:31:34.085 DEBUG 12032 --- [nio-8080-exec-3] o.s.j.d.DataSourceTransactionManager     : Acquired Connection [com.mysql.cj.jdbc.ConnectionImpl@321b632d] for JDBC transaction
2019-09-26 10:31:34.085 DEBUG 12032 --- [nio-8080-exec-3] o.s.j.d.DataSourceTransactionManager     : Switching JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@321b632d] to manual commit
2019-09-26 10:31:34.222 DEBUG 12032 --- [nio-8080-exec-3] o.s.j.d.DataSourceTransactionManager     : Initiating transaction commit
2019-09-26 10:31:34.222 DEBUG 12032 --- [nio-8080-exec-3] o.s.j.d.DataSourceTransactionManager     : Committing JDBC transaction on Connection [com.mysql.cj.jdbc.ConnectionImpl@321b632d]
2019-09-26 10:31:34.284 DEBUG 12032 --- [nio-8080-exec-3] o.s.j.d.DataSourceTransactionManager     : Releasing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@321b632d] after transaction
2019-09-26 10:31:34.288 DEBUG 12032 --- [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/html', given [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2019-09-26 10:31:34.288 DEBUG 12032 --- [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Writing ["update success"]
2019-09-26 10:31:34.290 DEBUG 12032 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : Completed 200 OK

 

 

你可能感兴趣的:(事务)