1.事务的特性
a.原子性:事务是一个不可分割的单位
b.一致性:事务前后的数据保持一致
c.隔离性:一个事务的操作不被其他事务干扰
d.持久性:一旦提交数据库,其数据是持久的
2.事务的接口
主要包括三个接口:
a.PlatFormTransactionManager 事务管理器
b.TransactionDefinition 事务定义信息(隔离,传播,超时,只读)
c.TransactionSatuts 事务具体运行状态
a.PlatFormTransactionManager
image.pngb.TransactionDefinition
事务本身的隔离级别:
image.png
事务间的传播行为:
image.png
c.TransactionSatuts
image.png3.在springboot中使用事务
只需要在类或者public方法上加上@Transactional
@Transactional
public void moneyAToB() {
//aaa转出200
AccountExample a = new AccountExample();
a.createCriteria().andAccountEqualTo("aaa");
Account aaa = accountMapper.selectByExample(a).get(0);
aaa.setMoney(aaa.getMoney()-200);
accountMapper.updateByExample(aaa, a);
int c = 1/0;
//bbb收到200
AccountExample b = new AccountExample();
b.createCriteria().andAccountEqualTo("bbb");
Account bbb = accountMapper.selectByExample(b).get(0);
bbb.setMoney(bbb.getMoney()+200);
accountMapper.updateByExample(bbb, b);
}
定义隔离级别
@Transactional(isolation=Isolation.READ_COMMITTED)
定义传播行为
@Transactional(propagation=Propagation.NESTED)
网友评论