spring--(26)事务传播行为

当一个事务方法(假定A)被另外一个事务方法(假定B)调用时,必须指定事务如何传播。存在以下两种状况
1>该方法(A)继续在现有事务(方法B的事务)中运行
2>新开启一个事务,并在本身的事务中运行(独立事务)
依赖于上篇文件类,这里新增一个接口和类
//接口文件app

public interface Cashier {
	/**
	 * 一个用户一次买多本书
	 */
	public void checkout(Integer userId,List<Integer> bookIds);
}

//接口实现类ide

@Service("cashier")
public class CashierImpl implements Cashier {
	@Autowired
	private BookShopService bookShopService;

	@Transactional
	@Override
	public void checkout(Integer userId, List<Integer> bookIds) {

		for (int bookId : bookIds) {
			bookShopService.purchase(userId, bookId);
		}
	}

}

测试类测试

public class TestTx {
	
	private ApplicationContext ctx;
	private Cashier cashier;
	
	{
		ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		cashier = ctx.getBean(Cashier.class);
	}
	
	@Test
	public void testTransaction(){
		cashier.checkout(1000, Arrays.asList(1001,1002));
	}

}
相关文章
相关标签/搜索