什么是事务
事务指的是逻辑上的一组操作,这组操作要么全部成功,要么全部失败。
事务的特性
原子性:指的是事务是一个不可分割的工作单位,事务中操作要么全部发生,要么都不发生。
一致性:指的是事务前后数据的完整性必须保持一致。
隔离性:指多个用户并发访问数据库时,一个用户的事务不能被其他用户的事务所干扰,多个并发事务之间数据要相互隔离。
持久性:指一个事务一旦被提交,它对数据库中的数据的改变就是永久性的,即使数据库发生故障也不应该对其有任何影响
spring事务管理
事务的传播行为:解决业务层方法之间的相互调用问题。
重点为1、4、7
eg:
Dao层
package com.imooc.passbook.merchants.transcation;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
/**
*
*/
@Mapper
@Repository
public interface AccountMapper {
/**
*
* @param out 转出账号
* @param money 装出金额
* @return
*/
int outMoney(@Param("out") String out, @Param("money") Double money);
/**
*
* @param in 转入账号
* @param money 装入金额
* @return
*/
int inMoney(@Param("in") String in, @Param("money") Double money);
}
service层
package com.imooc.passbook.merchants.transcation;
/**
* 转账案例的业务接口
*/
public interface AccountService {
/**
*
* @param out :转出账号
* @param in :转入账号
* @param money :转账金额
*/
public void transfer(String out, String in, Double money);
}
package com.imooc.passbook.merchants.transcation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 转账业务实现类
*/
@Service
public class AccountServiceImpl implements AccountService {
// 注入转账dao
@Autowired
private AccountMapper accountMapper;
@Override
public void transfer(String out, String in, Double money) {
accountMapper.outMoney(out,money);
accountMapper.inMoney(in,money);
}
}
测试代码
@Autowired
private AccountService accountService;
@Test
public void demmo1(){
accountService.transfer("aaa","bbb",200d);
}
如果修改service 代码
运行后出问题
(三个人都是1000)