- public class OrderService{
- private final int count = 500; //假设促销商品只有500个
- private List<Box> boxs = ActivityCommodityDao.getActiveCommodity(BoxID); //获取参与活动的商品
- private volatile int remain = count;
- public boolean doRequest(){
- if(remain > 0){
- synchronized(boxs){
- if(!createOrder(boxs.get(0))){
- return false;
- }
- boxs.remove(0);
- remain--;
- return true;
- }
- }
- return false;
- }
- @Transaction
- public boolean createOrder(Box box){
- try{
- OrderDao.createOrder(box....);
- ActivityCommodityDao.updateSoldFlag(box);
- }catch(Exception e){
- return false;
- }
- return true;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0906201512832.html
来源: http://www.codesnippet.cn/detail/0906201512832.html