@Override @Transactional public String putIfAbsent(String key, String value) { Assert.notNull(key, "'key' cannot be null"); Assert.notNull(value, "'value' cannot be null"); while (true) { //try to insert if does not exists int affectedRows = tryToPutIfAbsent(key, value); if (affectedRows > 0) { //it was not in the table, so we have just inserted it return null; } else { //value should be in table. try to return it try { return this.jdbcTemplate.queryForObject(this.getValueQuery, String.class, key, this.region); } catch (EmptyResultDataAccessException e) { //somebody deleted it between calls. try to insert again (go to beginning of while loop) } } } }
@Override @Transactional public void put(String key, String value) { Assert.notNull(key, "'key' cannot be null"); Assert.notNull(value, "'value' cannot be null"); while (true) { //try to insert if does not exist, if exists we will try to update it int affectedRows = tryToPutIfAbsent(key, value); if (affectedRows == 0) { //since value is not inserted, means it is already present try { //lock row for updating this.jdbcTemplate.queryForObject(this.getValueForUpdateQuery, String.class, key, this.region); } catch (EmptyResultDataAccessException e) { //if there are no rows with this key, somebody deleted it in between two calls continue; //try to insert again from beginning } //lock successful, so - replace this.jdbcTemplate.update(this.replaceValueByKeyQuery, ps -> { ps.setString(1, value); ps.setString(2, key); ps.setString(3, this.region); }); } return; } }
@Override @Transactional public String putIfAbsent(String key, String value) { Assert.notNull(key, "'key' cannot be null"); Assert.notNull(value, "'value' cannot be null"); while (true) { //try to insert if does not exists int affectedRows = tryToPutIfAbsent(key, value); if (affectedRows > 0) { //it was not in the table, so we have just inserted it return null; } else { //value should be in table. try to return it try { return this.jdbcTemplate.queryForObject(this.getValueQuery, String.class, key, this.region); } catch (EmptyResultDataAccessException e) { //somebody deleted it between calls. try to insert again (go to beginning of while loop) } } } }
@Override @Transactional public void put(String key, String value) { Assert.notNull(key, "'key' cannot be null"); Assert.notNull(value, "'value' cannot be null"); while (true) { //try to insert if does not exist, if exists we will try to update it int affectedRows = tryToPutIfAbsent(key, value); if (affectedRows == 0) { //since value is not inserted, means it is already present try { //lock row for updating this.jdbcTemplate.queryForObject(this.getValueForUpdateQuery, String.class, key, this.region); } catch (EmptyResultDataAccessException e) { //if there are no rows with this key, somebody deleted it in between two calls continue; //try to insert again from beginning } //lock successful, so - replace this.jdbcTemplate.update(this.replaceValueByKeyQuery, ps -> { ps.setString(1, value); ps.setString(2, key); ps.setString(3, this.region); }); } return; } }