use of org.jumpmind.symmetric.model.Lock in project symmetric-ds by JumpMind.
the class ClusterService method lockShared.
protected boolean lockShared(final String action) {
final Date timeout = DateUtils.addMilliseconds(new Date(), (int) -parameterService.getLong(ParameterConstants.LOCK_TIMEOUT_MS));
if (isClusteringEnabled()) {
return sqlTemplate.update(getSql("acquireSharedLockSql"), new Object[] { TYPE_SHARED, getServerId(), new Date(), action, TYPE_SHARED, timeout }) == 1;
} else {
Lock lock = lockCache.get(action);
if (lock != null) {
synchronized (lock) {
if ((lock.getLockType().equals(TYPE_SHARED) || lock.getLockTime() == null || lock.getLockTime().before(timeout)) && (lock.isSharedEnable() || lock.getSharedCount() == 0)) {
lock.setLockType(TYPE_SHARED);
lock.setLockingServerId(getServerId());
lock.setLockTime(new Date());
if (lock.getSharedCount() == 0) {
lock.setSharedEnable(true);
}
lock.setSharedCount(lock.getSharedCount() + 1);
return true;
}
}
}
}
return false;
}
use of org.jumpmind.symmetric.model.Lock in project symmetric-ds by JumpMind.
the class ClusterService method unlockCluster.
protected boolean unlockCluster(String action, String serverId) {
if (isClusteringEnabled()) {
return sqlTemplate.update(getSql("releaseClusterLockSql"), new Object[] { action, TYPE_CLUSTER, serverId }) > 0;
} else {
Lock lock = lockCache.get(action);
if (lock != null) {
synchronized (lock) {
if (lock.getLockType().equals(TYPE_CLUSTER) && serverId.equals(lock.getLockingServerId())) {
lock.setLastLockingServerId(lock.getLockingServerId());
lock.setLockingServerId(null);
lock.setLastLockTime(lock.getLockTime());
lock.setLockTime(null);
return true;
}
}
}
}
return false;
}
use of org.jumpmind.symmetric.model.Lock in project symmetric-ds by JumpMind.
the class ClusterService method unlockShared.
protected boolean unlockShared(final String action) {
if (isClusteringEnabled()) {
return sqlTemplate.update(getSql("releaseSharedLockSql"), new Object[] { action, TYPE_SHARED }) == 1;
} else {
Lock lock = lockCache.get(action);
if (lock != null) {
synchronized (lock) {
if (lock.getLockType().equals(TYPE_SHARED)) {
lock.setLastLockTime(lock.getLockTime());
lock.setLastLockingServerId(lock.getLockingServerId());
if (lock.getSharedCount() == 1) {
lock.setSharedEnable(false);
lock.setLockingServerId(null);
lock.setLockTime(null);
}
if (lock.getSharedCount() > 1) {
lock.setSharedCount(lock.getSharedCount() - 1);
} else {
lock.setSharedCount(0);
}
return true;
}
}
}
}
return false;
}
use of org.jumpmind.symmetric.model.Lock in project symmetric-ds by JumpMind.
the class ClusterService method isInfiniteLocked.
public boolean isInfiniteLocked(String action) {
Map<String, Lock> locks = findLocks();
Lock lock = locks.get(action);
if (lock != null && lock.getLockTime() != null && new Date().before(lock.getLockTime()) && Lock.STOPPED.equals(lock.getLockingServerId())) {
return true;
} else {
return false;
}
}
use of org.jumpmind.symmetric.model.Lock in project symmetric-ds by JumpMind.
the class AbstractClusterServiceTest method checkLock.
private Lock checkLock(String action, String lockType, int expectedSharedCount, boolean expectedSharedEnable) {
Lock lock = getClusterService().findLocks().get(action);
Assert.assertEquals(lockType, lock.getLockType());
Assert.assertNotNull(lock.getLockingServerId());
Assert.assertNotNull(lock.getLockTime());
Assert.assertEquals(expectedSharedCount, lock.getSharedCount());
if (expectedSharedCount > 0) {
Assert.assertEquals(expectedSharedEnable, lock.isSharedEnable());
}
return lock;
}
Aggregations