当前位置: 首页 > news >正文

springboot中如何同时操作同一功能

问题描述

测试阶段,由于存在某一功能的同时操作,该功能还是入库逻辑,此时若不进行处理,会造成插入表中多条重复数据,为此该问题需要修复。

解决办法

在接口开始进行对是否存在某个key值的判断,若不存在,则插入一条到redis中并加锁;若存在,则提示“正在处理中”;若中间出现逻辑处理异常,则需要对该key值删除;最后进行对锁的释放;

话不多说,上代码

pom.xml 依赖补充
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
application.yml文件中redis配置
redis:host: 127.0.0.1port: 6379timeout: 10poolMaxTotal: 1000poolMaxIdle: 500poolMaxWait: 500
UserMapper.java
import com.example.demo.entity.User;import org.apache.ibatis.annotations.Mapper;@Mapper
public interface UserMapper {int add(User user);User queryByName(String name);
}
 UserMapper.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper"><select id="queryByName" resultMap="userResult">select  id,name,age  from  "USER"where name=#{name}</select><insert id="add" parameterType="com.example.demo.entity.User">INSERT INTO "USER" (id,name, age)VALUES (SYS_GUID(),#{name},#{age})</insert>
</mapper>
RedisService类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit;@Service
public class RedisService {@AutowiredRedisTemplate<String,Object> redisTemplate;/*** 加锁* @param key* @param value* @param expireTime* @return*/public boolean lock(String key, String value, Long expireTime) {Boolean success = redisTemplate.opsForValue().setIfAbsent(key, value);if (expireTime != null && expireTime > 0) {redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);}return success != null && success;}/*** 释放锁* @param key*/public void unlock(String key) {redisTemplate.opsForValue().getOperations().delete(key);}/*** 根据key删除信息* @param key*/public void deleteStr(String key) {redisTemplate.delete(key);}
}
@bean配置 解决redis内容乱码,为了方便,我这边直接在启动类中配置
@Bean@SuppressWarnings("all")public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();template.setConnectionFactory(factory);Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();// key采用String的序列化方式template.setKeySerializer(stringRedisSerializer);// hash的key也采用jackson的序列化方式template.setHashKeySerializer(jackson2JsonRedisSerializer);// value序列化方式采用jacksontemplate.setValueSerializer(jackson2JsonRedisSerializer);// hash的value序列化方式采用jacksontemplate.setHashValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;}
controller类

该程序对新增用户功能同时操作的模拟,补充redis中key的判断,具体开发逻辑或内容可以视情况而定!

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.service.RedisService;import java.util.List;@RestController
@RequestMapping("/user")
public class UserController {private static final Logger logger = LoggerFactory.getLogger(UserController.class);@Autowiredprivate UserMapper userMapper;@Autowiredprivate RedisService redisService;@PostMapping("/addTest")public void addTest(@RequestBody User user) throws Exception {//todo 该功能的状态校验//1.判断该用户在redis是否存在if (!redisService.lock("addUser", String.valueOf(System.currentTimeMillis()), 15L)) {throw new Exception("正在操作中");}try {//2.逻辑处理User user1 = userMapper.queryByName(user.getName());if (user1 != null) {throw new Exception("该用户" + user.getName() + "已存在!");}for (int i = 0; i < 1000; i++) {for (int j = 0; j < 1000; j++) {logger.info("i*j={}", i * j);}}userMapper.add(user);} catch (Exception e) {redisService.deleteStr("addUser");throw e;} finally {redisService.unlock("addUser");}}
}

测试结果

一用户信息操作结果:

另一用户操作结果:

等待2分钟,该用户继续操作该数据,会提示“该用户已存在!”

http://www.lryc.cn/news/223004.html

相关文章:

  • YOLOWeeds: 用于棉花生产系统中多类杂草检测的 YOLO 目标检测器的新基准
  • Vue3:自定义图标选择器(包含 SVG 图标封装)
  • NIO讲解
  • react中jest配置,解决node_modules报错esm无法解析的问题
  • Qt6,使用 UI 界面完成命令执行自动化的设计
  • Apache Maven;会话技术
  • Azure - 机器学习:使用自动化机器学习训练计算机视觉模型的数据架构
  • 【C++】stack | queue | priority_queue | deque
  • 华为gre带验证key案例
  • Java算法(三): 判断两个数组是否为相等 → (要求:长度、顺序、元素)相等
  • 基于STM32的设计智慧超市管理系统(带收银系统+物联网环境监测)
  • 深入浅出理解ResNet网络模型+PyTorch实现
  • 【C++】万字一文全解【继承】及其特性__[剖析底层化繁为简](20)
  • 微信小程序之自定义组件开发
  • MCU系统的调试技巧
  • 【机器学习基础】机器学习概述
  • Python Selenium 执行 JavaScript
  • HTML的表单标签和无语义标签的讲解
  • 8.spark自适应查询-AQE之自适应调整Shuffle分区数量
  • 【Java 进阶篇】Java Filter 快速入门
  • Pytorch R-CNN目标检测-汽车car
  • 【PG】PostgreSQL13主从流复制部署(详细可用)
  • 学习pytorch15 优化器
  • [算法日志]图论刷题 沉岛思想的运用
  • Web服务器的搭建
  • 如何使用 GTX750 或 1050 显卡安装 CUDA11+
  • 跟着森老师学React Hooks(1)——使用Vite构建React项目
  • 强力解决使用node版本管理工具 NVM 出现的问题(找不到 node,或者找不到 npm)
  • Docker指定容器使用内存
  • 做什么数据表格啊,要做就做数据可视化