一、Propagation :
key属性确定代理应该给哪个方法增加事务行为。这样的属性最重要的部份是传播行为。有以下选项可供使用:
PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
PROPAGATION_SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER--以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED -- 而Nested的事务和他的父事务是相依的,他的提交是要等和他的父事务一块提交的。也就是说,如果父事务最后回滚,他也要回滚的。
二,代码清单
Mysql 表,
CREATE TABLE `account` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`money` double DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
初始数据:
aaa 1000
bbb 1000
ccc 1000
public interface InnerService {
void InnerTransfer(String from, String to, double money);
}
@Service
public class InnerServiceImpl implements InnerService {
@Autowired
MyDao myDao;
@Transactional(propagation = Propagation.MANDATORY)
public void InnerTransfer(final String from, final String to, final double money) {
myDao.In(to, money);
// int a = 1 / 0;
myDao.Out(from, money);
}
}
public interface OuterService {
void OuterTransfer();
}
@Service
public class OuterServiceImpl implements OuterService {
@Autowired
InnerService innerService;
@Autowired
MyDao myDao;
public void OuterTransfer() {
myDao.In("ccc", 19);
innerService.InnerTransfer("aaa", "bbb", 100d);
int a = 1 / 0;
}
}
public interface MyDao {
void In(String user, double money);
void Out(String user, double money);
}
@Repository
public class MyDaoImpl extends JdbcDaoSupport implements MyDao {
@Autowired
private DataSource dataSource;
@PostConstruct
private void initialize() {
setDataSource(dataSource);
}
public void In(String user, double money) {
String sql = "update account set money = money + " + money + " where name='" + user + "'";
this.getJdbcTemplate().update(sql);
}
public void Out(String user, double money) {
String sql = "update account set money = money - ? where name=?";
this.getJdbcTemplate().update(sql, money, user);
}
}
配置文件略。
三、事务
OuterTransfer | InnerTransfer | 事务结果 | aaa | bbb | ccc |
MANDATORY | |||||
NOT_SUPPORTED | |||||
SUPPORTS | |||||
REQUIRES_NEW | |||||
REQUIRED | |||||
NEVER | |||||
NESTED | |||||
MANDATORY | |||||
MANDATORY | MANDATORY | ||||
MANDATORY | NOT_SUPPORTED | ||||
MANDATORY | SUPPORTS | ||||
MANDATORY | REQUIRES_NEW | ||||
MANDATORY | REQUIRED | ||||
MANDATORY | NEVER | ||||
MANDATORY | NESTED | ||||
NOT_SUPPORTED | |||||
NOT_SUPPORTED | MANDATORY | ||||
NOT_SUPPORTED | NOT_SUPPORTED | ||||
NOT_SUPPORTED | SUPPORTS | ||||
NOT_SUPPORTED | REQUIRES_NEW | ||||
NOT_SUPPORTED | REQUIRED | ||||
NOT_SUPPORTED | NEVER | ||||
NOT_SUPPORTED | NESTED | ||||
SUPPORTS | |||||
SUPPORTS | MANDATORY | ||||
SUPPORTS | NOT_SUPPORTED | ||||
SUPPORTS | SUPPORTS | ||||
SUPPORTS | REQUIRES_NEW | ||||
SUPPORTS | REQUIRED | ||||
SUPPORTS | NEVER | ||||
SUPPORTS | NESTED | ||||
REQUIRES_NEW | |||||
REQUIRES_NEW | MANDATORY | ||||
REQUIRES_NEW | NOT_SUPPORTED | ||||
REQUIRES_NEW | SUPPORTS | ||||
REQUIRES_NEW | REQUIRES_NEW | ||||
REQUIRES_NEW | REQUIRED | ||||
REQUIRES_NEW | NEVER | ||||
REQUIRES_NEW | NESTED | ||||
REQUIRED | |||||
REQUIRED | MANDATORY | ||||
REQUIRED | NOT_SUPPORTED | ||||
REQUIRED | SUPPORTS | ||||
REQUIRED | REQUIRES_NEW | ||||
REQUIRED | REQUIRED | ||||
REQUIRED | NEVER | ||||
REQUIRED | NESTED | ||||
NEVER | |||||
NEVER | MANDATORY | ||||
NEVER | NOT_SUPPORTED | ||||
NEVER | SUPPORTS | ||||
NEVER | REQUIRES_NEW | ||||
NEVER | REQUIRED | ||||
NEVER | NEVER | ||||
NEVER | NESTED | ||||
NESTED | |||||
NESTED | MANDATORY | ||||
NESTED | NOT_SUPPORTED | ||||
NESTED | SUPPORTS | ||||
NESTED | REQUIRES_NEW | ||||
NESTED | REQUIRED | ||||
NESTED | NEVER | ||||
NESTED | NESTED |