One, Mysql and Redis error reporting mechanism
When adding existing data in mysql:
dataRepository.save(data);
An error is reported: Duplicate entry'data' for key'xxx'.
When adding existing data in redis:
redisService.setValue(data);
No content will be added, and no errors will be reported.
Two, add binding transactions to mysql and redis data operations
First open the transaction function in RedisService:
private void initStringRedisTemplate(){ stringRedisTemplate.setKeySerializer(keySerializer); stringRedisTemplate.setHashKeySerializer(keySerializer); stringRedisTemplate.setEnableTransactionSupport(true); // 开启事务功能 }
Then add comments on mysql and redis data operations:
@Transactional(rollbackFor = Exception.class)
At this time, if an error is reported during the operation of the mysql database, the redis data that was previously operated will also be rolled back.