SpingBoot-tx

1.springboot与事务管理
  • 因为导入了mybatis-springboot依赖,所以自动导入了spring-tx依赖,导入的是DataSourceTransactionManager

  • jpa导入的是JpaTransactionManager

    1

1.不加事务管理时,不管是运行异常还是受查异常数据都会插入

@Service
public class StudentServiceImp implements StudentService {

    @Resource
    private IStudentDao studentDao;

    @Override
    public void addStudent(Student student) throws Exception {

        studentDao.addStudent(student);
        //这里发生运行异常
//        int a=9/0;
        
        //这里发生的是受查异常
        if (true){
            throw new Exception("受查异常");
        }
        studentDao.addStudent(student);
    }
}

2.添加了事务管理后,运行异常不会插入,受查异常还是会插入(加入@Transactional)

@Service
@Transactional
public class StudentServiceImp implements StudentService {

    @Resource
    private IStudentDao studentDao;

    @Override
    public void addStudent(Student student) throws Exception {

        studentDao.addStudent(student);
        //这里发生运行异常
//        int a=9/0;
        
        //这里发生的是受查异常
        if (true){
            throw new Exception("受查异常");
        }
        studentDao.addStudent(student);
    }
}

3.发生受查异常错误时,为了不让数据插入,在serviceImp中加入@Transactional(rollbackFor = Exction.class)(这个Exception范围有点大)

@Service
@Transactional(rollbackFor = Exception.class)
public class StudentServiceImp implements StudentService {

    @Resource
    private IStudentDao studentDao;

    @Override
    public void addStudent(Student student) throws Exception {

        studentDao.addStudent(student);
        //这里发生运行异常
//        int a=9/0;
        
        //这里发生的是受查异常
        if (true){
            throw new Exception("受查异常");
        }
        studentDao.addStudent(student);
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Seata is an open-source distributed transaction solution that provides a high-performance and easy-to-use distributed transaction coordination service for Spring Boot applications. It supports distributed transaction management across multiple databases, message queues, and other resources, ensuring the consistency and reliability of data operations. To integrate Seata into a Spring Boot application, you need to follow the following steps: 1. Add Seata dependencies to your pom.xml file: ``` <dependency> <groupId>io.seata</groupId> <artifactId>seata-all</artifactId> <version>1.4.1</version> </dependency> ``` 2. Configure Seata in your application.yml file: ``` spring: cloud: alibaba: seata: tx-service-group: my_test_tx_group enable-auto-data-source-proxy: true application-id: my_test_app tx-log: dir: {your_log_dir} name: {your_log_name} max-size: {your_log_max_size} max-history: {your_log_max_history} ``` 3. Enable Seata in your Spring Boot application: ``` @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients @EnableAutoDataSourceProxy @EnableHystrix @EnableAspectJAutoProxy(exposeProxy = true) public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Bean public GlobalTransactionScanner globalTransactionScanner() { return new GlobalTransactionScanner("my_test_tx_group", "my_test_app"); } } ``` 4. Use Seata in your business logic: ``` @RestController public class DemoController { @Autowired private DemoService demoService; @RequestMapping("/test") public String test() { demoService.doBusiness(); return "OK"; } } @Service public class DemoService { @Autowired private DemoMapper demoMapper; @GlobalTransactional public void doBusiness() { // business logic using demoMapper } } ``` In the above example, the `@GlobalTransactional` annotation is used to wrap the business logic in a distributed transaction. This ensures that all the database operations performed by the `demoMapper` are atomic and consistent, even if they involve multiple resources. Overall, Seata is a powerful and flexible solution for managing distributed transactions in Spring Boot applications. By following the above steps, you can easily integrate Seata into your project and ensure the reliability and consistency of your data operations.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值