@Transactional cause synchronized invalid
springboot version 2.1.7.release My code is `public interface GoodsMapper {
@Update(" update goods set stock = stock-1 where id= #{id} ") int reduceStock(@Param("id") Integer id);
@Select(" select * from goods where id= #{id} ") Goods getGoodsById(@Param("id") Integer id); }
public interface OrderMapper {
@Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
@Insert(" insert into order
(user_id) values(#{userId}) ")
int addOrder(Order order);
}
@Component public class GoodsService { @Autowired GoodsMapper goodsMapper;
@Autowired
OrderMapper orderMapper;
@Transactional
public void reductStock(Integer id) throws Exception {
synchronized (this){
Goods goods = goodsMapper.getGoodsById(id);
if (goods.getStock()>0){
goodsMapper.reduceStock(id);
Order order = new Order();
order.setUserId(UUID.randomUUID().toString());
orderMapper.addOrder(order);
}else{
throw new RuntimeException(" 库存不足");
}
}
}
}
Controller
@GetMapping("/stock/reduce")
public Object reduceStock(Integer id) throws Exception {
goodsService.reductStock(id);
return "ok";
}
`
Comment From: snicoll
Sorry but I didn't get what you mean to report. If you want support, please move all that code in text into a small app that we can run ourselves. You can attach a zip to this issue or push the code to a separate GitHub repository.
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.