level 1
问题描述:
1. SpringBoot默认是单例模式。
2. 项目存在更新即时库存的方法,存在并发问题。
3. 项目没有使用乐观锁。
为什么在Controller中的更新库存方法上加入synchronized无法解决并发更新库存的问题?synchronized默认锁住的应该是this对象,而SpringBoot默认单例模式,所有请求应该都走的同一个Controller对象吧?为什么锁不住?
2021年09月23日 01点09分
1
level 1
Controller层
@RestController
@RequestMapping(value = "${adminPath}/wms/")
public class InstockController{
@Autowired
private InstockService instockService;
@RequestMapping(value = "updateInstock")
@ResponseBody
public synchronized String updateInstock(String fnumber,BigDecimal quantity){
return instockService.updateInstock(fnumber,quantity);
}
}
Service层
@Service
@Transactional(readOnly = true)
public class InstockService{
@Transactional(readOnly = false)
public String updateInstock(String fnumber,BigDecimal quantity){
Map params = new HashSet<>(2);
params.put("fnumber",fnumber);
params.put("quantity",quantity);
Integer count = instockDao.updateInstock(params);
if(count==1){
return "0";
}else{
return "1";
}
}
Dao层
@MyBatisDao
public Interface InstockDao{
Integer updateInstock(Map params);
}
xml
update wms_instock set quantity = quantity +
#{quantity} where fnumber = #
{fnumber}
2021年09月23日 08点09分
4