redis分布式锁--》死锁问题解决策略研究

死锁场景

redis分布式锁都不可避免会遇到死锁问题,我们先来讨论一下,什么场景会发生死锁问题。

image

因为redis锁对应的key还在,如果不将redis锁对应的key删除,下次获取锁的时候还是会失败。

redis integration原理

有童鞋会说,那还不简单,重启的时候直接将redis锁对应的key删除不就可以了吗?当然不行,再讲这个问题之前,我们先来简单的看一下redis integratio锁的实现原理。

首先integration在获取锁的时候会检验本地是否持有该锁

	@Override
		public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
			long now = System.currentTimeMillis();
			// 检查本地是否持有锁
			if (!this.localLock.tryLock(time, unit)) {
				return false;
			}
			try {
				long expire = now + TimeUnit.MILLISECONDS.convert(time, unit);
				boolean acquired;
				// 获取锁
				while (!(acquired = obtainLock()) && System.currentTimeMillis() < expire) { //NOSONAR
					Thread.sleep(100); //NOSONAR
				}
				if (!acquire
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章