8.1.拷贝上一章代码
8.2.applicationContext.xml
<!-- 开启spring对注解事务的支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
8.3.service
@Service
@Transactional(readOnly=true,propagation= Propagation.SUPPORTS)
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
/**
* 转账
* @param source
* @param target
* @param money
*/
@Override
@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
public void updateUser(String source, String target, Float money) {
userMapper.updateUserOfSub(source, money);
int a = 6/0;
userMapper.updateUserOfAdd(target, money);
}
}
8.4.测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")//加载配置文件
public class ServiceTest {
@Autowired
private UserService userService;
/**
* 转账业务
*/
@Test
public void testUpdate(){
userService.updateUser("张三丰","宋远桥",1F);
}
}
-
事务回滚: