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;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM