1-4 事务抽象 事务的传播性与隔离性

博客介绍了Spring Boot有一致的事务模型,涉及JDBC、Hibernate、myBatis等,阐述了事务抽象的核心接口,如PlatformTransactionManager等,还介绍了TransactionDefinition的相关属性。同时讲解了编程式事务,包括TransactionTemplate等,以及注解使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Springboot有一致的事务模型

JDBC/Hibernate/myBatis

DataSource/JTA

事务抽象的核心接口

PlatformTransactionManager

    DataSourceTransationManager

    HibernateTransactionManager

    JtaTransactionManager

 

TransactionDefinition

    Propagation //传播性

    Isolation //隔离性

    Timeout //超时

    Read-only status //是否只读

 

事务的传播特性(7种,文档中只提了3项)
传播性描述
PROPAGATION_REQUIRED0当前有事务用当前的,没有就用新的
PROPAGATION_SUPPORTS1事务可有可无,不是必须的
PROPAGATION_MANDATORY2当前一定要有事务,不然就抛异常
PROPAGATION_REQUIRES_NEW3无论是否有事务都起个新事务
PROPAGATION_NOT_SUPPORTED4不支持事务,按非事务方式运行
PROPAGATION_NEVER5不支持事务,如果有事务则抛异常
PROPAGATION_NESTED6当前有事务就在当前是事务里再起一个事务

 

 

 

 

 

 

 

 

事务的隔离特性
隔离性脏读不可重复读幻读
ISOLATION_READ_UNCOMMITTER1TTT
ISOLATION_READ_COMMITTED2FTT
ISOLATION_REPEATABLE_READ3FFT
ISOLATION_SERIALIZABLE4FFF

 

 

 

 

 

 

 

编程式事务

TransactionTemplate

    TransactionCallback

    TransactionCallbackWithoutResult

PlatformTransactionManager

    可以传入TransactionDefinition进行定义

 

通过编程式事务编写

package com.example.ribi;

import com.example.ribi.example.User;
import com.example.ribi.mapper.UserRepository;
import com.example.ribi.service.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootApplication
@EnableCaching(proxyTargetClass = true)
@EnableTransactionManagement
@Slf4j
public class RibiApplication implements ApplicationRunner {

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

	@Autowired
    private JdbcTemplate jdbcTemplate;

	@Autowired
    private TransactionTemplate transactionTemplate;

    @Override
    public void run(ApplicationArguments args) throws Exception{
        log.info("0s count is {}",getCount()); //0s 一开始表里有的记录
        jdbcTemplate.execute("insert into user values (10,'aaa','bbb')");
        log.info("1s count is {}",getCount()); //1s 插入一条记录后
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                jdbcTemplate.execute("insert into user values (11,'aaa','bbb')");
                log.info("2s count is {}",getCount()); //2s 开启事务设置回滚,插入一条记录再查询
                status.setRollbackOnly();
            }
        });
        log.info("3s count is {}",getCount()); //3s 查询事务回滚后的表记录数
    }

    public Long getCount(){
        return (Long)jdbcTemplate.queryForList("select count(1) as CNT from user").get(0).get("CNT");
    }
}

运行结果

2019-05-21 17:57:24.745  INFO 48768 --- [main] com.example.ribi.RibiApplication : 0s count is 4 // 一开始4条
2019-05-21 17:57:24.786  INFO 48768 --- [main] com.example.ribi.RibiApplication : 1s count is 5 // 插入一条后变5条
2019-05-21 17:57:24.805  INFO 48768 --- [main] com.example.ribi.RibiApplication : 2s count is 6 // 开启事务再插入一条查询变成6条 然后再回滚
2019-05-21 17:57:24.820  INFO 48768 --- [main] com.example.ribi.RibiApplication : 3s count is 5 // 回滚后变成5条

注解:

Application类添加

@EnableTransactionManagement

注解在方法头上添加

@Transactional

-- 部分摘自 极客时间 玩转Spring全家桶 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值