redis防止重复提交
public interface DistributedLock {
boolean getLock(String var1, String var2, int var3);//加锁
void unLock(String var1, String var2);//释放
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package com.pt.platform.core.redis.lock; import com.pt.platform.core.ehcache.ObtainPropertiesInfo;
import com.pt.platform.core.redis.JedisSentinelPool;
import com.pt.platform.core.redis.lock.DistributedLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.ShardedJedis;
import redis.clients.util.SafeEncoder; public class RedisLock implements DistributedLock {
private static final Logger logger = LoggerFactory.getLogger(RedisLock.class);
private static final int DEFAULT_EXPIRE_TIME = 60;
private static final String LOCK_KEY_PREFIX = "REDIS_LOCK";
private JedisSentinelPool pool; public RedisLock() {
} public JedisSentinelPool getPool() {
return this.pool;
} public void setPool(JedisSentinelPool pool) {
this.pool = pool;
} public boolean getLock(String module, String bizKey, int expireTime) {
if(module != null && !"".equals(module) && bizKey != null && !"".equals(bizKey)) {
if(this.getPool() != null) {
ShardedJedis jedis = null;
String lockKey = this.getLockKey(module, bizKey); boolean var10;
try {
jedis = (ShardedJedis)this.getPool().getResource();//获取到数据源
long e = System.currentTimeMillis();
long result = jedis.setnx(SafeEncoder.encode(lockKey), SafeEncoder.encode(e + "")).longValue();
if(result == 1L) {
if(expireTime > 0) {
jedis.expire(SafeEncoder.encode(lockKey), expireTime);
if(logger.isDebugEnabled()) {
logger.debug("key:" + lockKey + " locked and expire time:" + expireTime + "s");
}
} else {
jedis.expire(SafeEncoder.encode(lockKey), 60);
if(logger.isDebugEnabled()) {
logger.debug("key:" + lockKey + " locked and expire time:" + 60 + "s");
}
} var10 = true;
return var10;
} if(logger.isDebugEnabled()) {
logger.debug("key:" + lockKey + " has already bean locked");
} var10 = false;
} catch (Exception var14) {
logger.error("lock error", var14);
boolean var7 = false;
return var7;
} finally {
this.getPool().returnResource(jedis);
} return var10;
} else {
logger.error("jedisSentinelPool is null");
return true;
}
} else {
logger.error("parameters is null");
return false;
}
}
//删除key值释放锁
public void unLock(String module, String bizKey) {
if(module == null || "".equals(module) || bizKey == null || "".equals(bizKey)) {
logger.error("parameters is null");
} if(this.getPool() != null) {
ShardedJedis jedis = null;
String lockKey = this.getLockKey(module, bizKey); try {
jedis = (ShardedJedis)this.getPool().getResource();
jedis.del(SafeEncoder.encode(lockKey));
} catch (Exception var9) {
logger.error("unlock error", var9);
} finally {
this.getPool().returnResource(jedis);
}
} else {
logger.error("jedisSentinelPool is null");
} }
//组装key值
private String getLockKey(String module, String bizKey) {
StringBuffer sb = new StringBuffer();
sb.append("REDIS_LOCK").append(":").append(ObtainPropertiesInfo.getValByKey("app.code")).append(":").append(module).append(":").append(bizKey);
return sb.toString();
}
}
private Map<String ,String> getResult(LaTFundRegDTO dto,UserInfo userInfo){
Map<String,String> map=null;
//加锁
if(redisLock.getLock(FundConstant.REDIS_LOCK_MODEL_FUND, dto.getCreditorRightsNo(), 120)) {
try{
if (FundConstant.FUND_STATUS_3.equals(dto.getFundStatus())) {
service.updateReFund(dto, userInfo);
} else if (FundConstant.FUND_STATUS_1.equals(dto.getFundStatus())) {
service.updatedoFundData(dto, userInfo);
}
}catch(Exception e){
e.printStackTrace();
map=new HashMap<String,String>();
map.put("id",dto.getCreditorRightsNo());
map.put("msg",e.getMessage());
logger.error("",e);
}finally{
redisLock.unLock(FundConstant.REDIS_LOCK_MODEL_FUND,dto.getCreditorRightsNo());
}
}else{
map=new HashMap<String,String>();
map.put("id",dto.getId()+"");
map.put("msg","不允许重复发起放款操作");
logger.error("不允许重复发起放款操作");
}
return map;
}
redis防止重复提交的更多相关文章
- 使用redis防止重复提交
使用redis防止重复提交 其实主要思路是他的https://blog.csdn.net/u013378306/article/details/52944780 主要目前我的情况是,前后端分离的, ...
- Java使用Redis实现分布式锁来防止重复提交问题
如何用消息系统避免分布式事务? - 少年阿宾 - BlogJavahttp://www.blogjava.net/stevenjohn/archive/2018/01/04/433004.html [ ...
- 【Redis使用系列】使用Redis做防止重复提交
前言 在平时的开发中我们都需要处理重复提交的问题,避免业务出错或者产生脏数据,虽然可以通过前端控制但这并不是可以完全避免,最好的方式还是前后端均进行控制,这样的话就可以更有效,尽可能全面的去减少错误的 ...
- 浅谈C#在网络波动时防重复提交
前几天,公司数据库出现了两条相同的数据,而且时间相同(毫秒也相同).排查原因,发现是网络波动造成了重复提交. 由于网络波动而重复提交的例子也比较多: 网络上,防重复提交的方法也很多,使用redis锁, ...
- Restful api 防止重复提交
当前很多网站是前后分离的,前端(android,iso,h5)通过restful API 调用 后端服务器,这就存在一个问题,对于创建操作,比如购买某个商品,如果由于某种原因,手抖,控件bug,网络错 ...
- springmvc防止重复提交拦截器
一.拦截器实现,ResubmitInterceptorHandler.java import org.apache.commons.lang3.StringUtils; import org.spri ...
- 防CSRF攻击:一场由重复提交的问题引发的前端后端测试口水战
重复提交,这是一直以来都会存在的问题,当在网站某个接口调用缓慢的时候就会有可能引起表单重复提交的问题,不论form提交,还是ajax提交都会有这样的问题,最近在某社交app上看到这么一幕,这个团队没有 ...
- Token机制,防止web页面重复提交
1.业务要求:页面的数据只能被点击提交一次 2.发生原因: 由于重复点击或者网络重发,或者nginx重发等情况会导致数据被重复提交 3.解决办法: 集群环境:采用token加redis(redis单线 ...
- 由防止表单重复提交引发的一系列问题--servletRequest的复制、body值的获取
@Time:2019年1月4日 16:19:19 @Author:QGuo 背景:最开始打算写个防止表单重复提交的拦截器:网上见到一种不错的方式,比较合适前后端分离,校验在后台实现: 我在此基础上 ...
随机推荐
- 多图详解Go中的Channel源码
转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 本文使用的go的源码时14.4 chan介绍 package main import & ...
- C#处理医学图像(一):基于Hessian矩阵的血管肺纹理骨骼增强对比
在医院实际环境中,经常遇到有问题的患者,对于一些特殊的场景,比如骨折,肺结节,心脑血管问题 需要图像对比增强来更为清晰的显示病灶助于医生确诊,先看效果: 肺纹理增强: 肺结节增强: 血管对比增强: 骨 ...
- 【函数分享】每日PHP函数分享(2021-1-11)
str_shuffle() 随机打乱一个字符串. string str_shuffle ( string $str ) 参数描述 str 输入字符串.返回值:返回打乱后的字符串.实例: < ...
- Laya 踩坑日记-BitmapFont 不显示空格
项目中有用到艺术字,美术通过 bmfont64 将字体导给我了,结果发现在应用上 空格不显示 如图: 今天去深究了一下这个问题,发现是底层没封装好,然后自己改了一下下面是改过的 BitmapFont ...
- LeetCode106 从中序和后序序列构造二叉树
题目描述: 根据一棵树的中序遍历与后序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [ ...
- PAT甲级 Perfect Sequence (25) 记忆化搜索
题目分析: 意思是要求对于一个给出的数组,我们在其中尽可能多选数字,使得所选数字的max <= min * p,而由于数据量较大直接二层循环不加优化实现是不现实的,由题意得知,对于数字序列的子序 ...
- Spring框架之websocket源码完全解析
Spring框架之websocket源码完全解析 Spring框架从4.0版开始支持WebSocket,先简单介绍WebSocket协议(详细介绍参见"WebSocket协议中文版" ...
- 关联实现下-jsonpath取值(有难度!!耗时长)
re的使用参考:正则表达式基础及re模块:https://www.cnblogs.com/dream66/p/12953729.html import restr1 = '{"access_ ...
- 【Oracle】查看表空间是否为自动扩展
查看指定的表空间是否为自动扩展 SQL> select file_name,autoextensible,increment_by from dba_data_files where tab ...
- 通过LOGMNR查找程式带入的实际值
生产库中出现了大量的锁表,需要得到当时程式执行的SQL以及其带入的值 1.查看SQL SELECT SQL_ID FROM V$SESSION WHERE SID=(SELECT FINAL_BLOC ...