
正文
java中给某个字段加锁
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
private String buildLock(String str) {
StringBuilder sb = new StringBuilder(str);
String lock = sb.toString().intern();
return lock;
}public ResultBean saveYhyhq(String yhqbh, String khh) {
ResultBean res = new ResultBean();
try {
// 加锁
String lock = buildLock(yhqbh);
synchronized (lock) {
// 查询优惠卷是否可用
String yhqxxsql = "select * from tyhqxx where zt = " + GlobalStatusType.YHQZT_QY
+ " and sxsl > 0 and yhqbh = '" + yhqbh + "'";
Tyhqxx tyhqxx = sqlMapper.selectOne(yhqxxsql, Tyhqxx.class);
if (tyhqxx == null) {
res.setSuccess(false);
res.setMessage("来晚了");
return res;
} } catch (Exception e) {
res.setSuccess(false);
res.setMessage("异常:" + e.getMessage());
throw new RuntimeException(e);
}
return res;
}








