Java_Spring_事务管理

一、事务用到的注解

@EnableTransactionManagement:加入spring提供好的“通知类”,写在SpringConfig这个配置类上
@Transactional:这个注解添加到类上面,也可以添加方法上面
如果@Transactional添加在类上面,这个类里面所有的方法都添加事务
如果@Transactional添加在方法上面,为这个方法添加事务

二、Spirng中事务的角色

如果是开启了两个事务,那么由于事务的特性:隔离性则会互不干扰,如果将两个事务都加入到spring开启的总事务中,那么在总事务中如果发生异常则会同时影响其中的两个事务,使他们也参加回滚。
spring发起的事务称作:事务管理员
spring中加入的事务称作:事务调节员

三、事务的传播行为

当一个事务的方法被另外的一个事务方法调用时,这个事务方法如何进行?
在这里插入图片描述

REQUIRED:

在这里插入图片描述

REQUIRED_NEW:

![在这里插入图片描述](https://img-blog.csdnimg.cn/4c357ce391404086851c7d8ad9d7da6a.png#pic_center

3.1 ioslation:事务的隔离级别

多事务操作之间不会产生影响。不考虑隔离性产生很多问题
有三个读问题:脏读、不可重读、虚读
(1)脏读:一个未提交的事务,读取到另一个未提交事务的数据

(2)不可重读:一个未提交事务读取到另一提交事务修改过的数据

(3)虚读(幻读):一个未提交的事务读取到另一个条事务添加的数据

通过设置事务的隔离级别就能够解决读的问题:
在这里插入图片描述
如何设置:
@Transactional(propagation = Propagation.REQUIRED,isolation=Isolation.REPEATABLE_READ)

3.2timeout:超时时间

1.事务需要在一定时间内进行提交,如果不提交进行回滚
2.默认值是-1,设置时间以秒单位进行计算
@Transactiona(timeout = -1)

3.3realOnly:是否只读

读:查询操作,写:添加修改删除操作
readOnly默认值false,表示可以查询,可以添加修改删除操作
设置成true,就只能读

3.4rollbackFor:回滚

设置出现哪些异常进行事务回滚

3.4noRollbackFor:不回滚

四、事务传播特性的应用

直接写service包,在接口处添加事务:
AccountService:

import org.springframework.transaction.annotation.Transactional;

public interface AccountService {
     @Transactional
     void changMoney(String name1,String name2,Double money);
}

LogService:

public interface LogService {
//    设置事务的传播行为,新建一个事务
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    void info(String out,String in,Double money);
}

AccountService实现类:

import cn.itheima.mapper.AccountMapper;
import cn.itheima.mapper.LogMapper;
import cn.itheima.service.AccountService;
import cn.itheima.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceimpl implements AccountService {
    @Autowired
    AccountMapper accountMapper;
    @Autowired
    LogService logService;


    @Override
    public void changMoney(String name1, String name2, Double money) {
    //因为logService设置了事务的传播行为(Propagation.REQUIRED),所以当出现异常时依然可以提交
        logService.info(name1, name2, money);
        accountMapper.outMoney(money, name1);
        int a = 1 / 0;//添加一个异常,
        accountMapper.inMoney(money, name2);
    }
}

LogService实现类:

@Service
public class LogServiceImpl implements LogService {
    @Autowired
    LogMapper logMapper;
    @Override
    public void info(String out, String in, Double money) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        String infor = out+"取出"+money+"存入"+in+"的账户";
        logMapper.addlog(infor,format);
    }
}

测试类:

 //    事务管理
    @Test
    public void transactionTest() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
        AccountService accountService = applicationContext.getBean(AccountService.class);
        accountService.changMoney("甲", "乙", 100D);
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值