Springboot学习七 spring事务

一 事务控制

@Service
public class CityServiceImpl implements CityService {

    @Autowired
    private CityMapper cityMapper;
    
    @Override
    @Transactional(value = "primaryTxMan", readOnly = true)
    public List<City> findByState(String state) {
        return this.cityMapper.findByState(state);
    }
}

@Transcational(readOnly=true) 这个注解一般会写在业务类上,或者其方法上,用来对其添加事务控制。当括号中添加readOnly=true, 则会告诉底层数据源,这个是一个只读事务,对于JDBC而言,只读事务会有一定的速度优化

二  Transactional的value

 value这里主要用来指定不同的事务管理器;主要用来满足在同一个系统中,存在不同的事务管理器。比如在Spring中,声明了两种事务管理器txManager1, txManager2.

然后,用户可以根据这个参数来根据需要指定特定的txManager.

   那有同学会问什么情况下会存在多个事务管理器的情况呢? 比如在一个系统中,需要访问多个数据源或者多个数据库,则必然会配置多个事务管理器的。

猜你喜欢

转载自www.cnblogs.com/liufei1983/p/8973158.html
今日推荐