try (Connection conn = dataSource.getConnection();
PreparedStatement updateStatement = conn.prepareStatement(
- "UPDATE pooling.locks SET host = ?, owner = ?, expirationTime = timestampadd(second, ?, now()) WHERE resourceId = ? AND (owner = ? OR expirationTime < now())");
+ "UPDATE pooling.locks SET host = ?, owner = ?, expirationTime = timestampadd(second, ?, now()) WHERE resourceId = ? AND expirationTime < now()");
PreparedStatement insertStatement = conn.prepareStatement(
"INSERT INTO pooling.locks (resourceId, host, owner, expirationTime) values (?, ?, ?, timestampadd(second, ?, now()))");) {
updateStatement.setString(i++, this.owner);
updateStatement.setInt(i++, holdSec);
updateStatement.setString(i++, this.resourceId);
- updateStatement.setString(i++, this.owner);
// The lock was expired and we grabbed it.
// return true
assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC));
- // extend the lock
- assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC));
+ // cannot re-lock
+ assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC));
+
+ assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeIsLockedBy("resource1", "owner1"));
}
@Test
// refresh should work now
assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeRefresh("resource1", "owner1", MAX_AGE_SEC));
+ assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeIsLockedBy("resource1", "owner1"));
+
// expire the lock
try (PreparedStatement updateStatement = conn.prepareStatement("UPDATE pooling.locks SET expirationTime = timestampadd(second, -1, now()) WHERE resourceId = ?");)
{
// refresh should fail now
assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeRefresh("resource1", "owner1", MAX_AGE_SEC));
+
+ assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeIsLockedBy("resource1", "owner1"));
}
}
/**
- * This method is called before a lock is acquired on a resource. It may be
- * invoked repeatedly to extend the time that a lock is held.
+ * This method is called before a lock is acquired on a resource.
*
* @param resourceId
* @param owner
}
/**
- * Attempts to lock a resource, extending the lock if the owner already holds it.
+ * Attempts to lock a resource, rejecting the lock if it is already owned, even if
+ * it's the same owner; the same owner can use {@link #refresh(String, String, int)
+ * refresh()}, instead, to extend a lock on a resource.
*
* @param resourceId
* @param owner
throw makeNullArgException(MSG_NULL_OWNER);
}
- Data existingLock;
+ boolean locked = false;
synchronized(locker) {
cleanUpLocks();
- if ((existingLock = resource2data.get(resourceId)) != null && existingLock.getOwner().equals(owner)) {
- // replace the existing lock with an extended lock
- locks.remove(existingLock);
- existingLock = null;
- }
-
- if (existingLock == null) {
+ if(!resource2data.containsKey(resourceId)) {
Data data = new Data(owner, resourceId, currentTime.getMillis() + TimeUnit.SECONDS.toMillis(holdSec));
resource2data.put(resourceId, data);
locks.add(data);
+ locked = true;
}
}
- boolean locked = (existingLock == null);
logger.info("lock {} for resource {} owner {}", locked, resourceId, owner);
return locked;
assertTrue(mgr.isLockedBy(RESOURCE_A, OWNER1));
// extend the lock
- mgr.lock(RESOURCE_A, OWNER1, MAX_AGE_SEC);
+ mgr.refresh(RESOURCE_A, OWNER1, MAX_AGE_SEC);
// verify still locked after sleeping the other half of the cycle
testTime.sleep(MAX_AGE_MS/2+1);
mgr.lock(RESOURCE_A, OWNER1, MAX_AGE_SEC);
// same owner
- assertTrue(mgr.lock(RESOURCE_A, OWNER1, MAX_AGE_SEC));
+ assertFalse(mgr.lock(RESOURCE_A, OWNER1, MAX_AGE_SEC));
// different owner
assertFalse(mgr.lock(RESOURCE_A, OWNER2, MAX_AGE_SEC));