diff --git a/src/main/java/com/ctrip/zeus/lock/impl/MysqlDistLock.java b/src/main/java/com/ctrip/zeus/lock/impl/MysqlDistLock.java index 6c6a3517..426f4ad2 100644 --- a/src/main/java/com/ctrip/zeus/lock/impl/MysqlDistLock.java +++ b/src/main/java/com/ctrip/zeus/lock/impl/MysqlDistLock.java @@ -55,7 +55,7 @@ public class MysqlDistLock implements DistLock { DistLockDo d = new DistLockDo().setLockKey(key) .setOwner(Thread.currentThread().getId()).setServer(S.getIp()) .setCreatedTime(System.currentTimeMillis()); - while(System.currentTimeMillis() < end) { + while (System.currentTimeMillis() < end) { try { if (tryAddLock(d)) return; @@ -73,7 +73,7 @@ public class MysqlDistLock implements DistLock { .setOwner(Thread.currentThread().getId()).setServer(S.getIp()) .setCreatedTime(System.currentTimeMillis()); int count = 1; - while (true){ + while (true) { try { if (tryAddLock(d)) return; @@ -86,22 +86,25 @@ public class MysqlDistLock implements DistLock { @Override public void unlock() { + DistLockDo d = new DistLockDo().setLockKey(key); + DalException logEx = null; try { - DistLockDo d = new DistLockDo().setLockKey(key); if (unlock(d)) return; - for (int i = 1; i < MAX_RETRIES; i++) { - try { - if (unlock(d)) - return; - retryDelay(key, i); - } catch (DalException e) { - retryDelay(key, i); - } - } } catch (DalException e) { - logger.warn("Fail to unlock the lock " + key); + logEx = e; } + for (int i = 1; i < MAX_RETRIES; i++) { + try { + if (unlock(d)) + return; + retryDelay(key, i); + } catch (DalException e) { + retryDelay(key, i); + logEx = e; + } + } + logger.warn("Fail to unlock the lock " + key + ", throwing ex: " + (logEx == null ? " Unknown" : logEx.getMessage())); } private boolean tryAddLock(DistLockDo d) throws DalException { diff --git a/src/test/java/com/ctrip/zeus/lock/DistLockTest.java b/src/test/java/com/ctrip/zeus/lock/DistLockTest.java index feb86edd..79edb9a0 100644 --- a/src/test/java/com/ctrip/zeus/lock/DistLockTest.java +++ b/src/test/java/com/ctrip/zeus/lock/DistLockTest.java @@ -1,6 +1,5 @@ package com.ctrip.zeus.lock; -import com.ctrip.zeus.lock.impl.MysqlDistLock; import com.ctrip.zeus.util.S; import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; import org.codehaus.plexus.component.repository.exception.ComponentLookupException;