Skip to content

Instantly share code, notes, and snippets.

@jsanda
Created November 2, 2016 16:14
Show Gist options
  • Save jsanda/f9967c1b8d506fb0807a8835a745810d to your computer and use it in GitHub Desktop.
Save jsanda/f9967c1b8d506fb0807a8835a745810d to your computer and use it in GitHub Desktop.
private void initSchema() {
doWithLock("cassalog", () -> {
SchemaService schemaService = new SchemaService();
schemaService.run(session, keyspace, Boolean.parseBoolean(resetDb));
session.execute("USE " + keyspace);
});
}
private void doWithLock(String key, Runnable runnable) {
AdvancedCache<String, String> cache = locksCache.getAdvancedCache();
DistributedLock schemaLock = new DistributedLock(cache);
try {
schemaLock.lock();
while (!schemaLock.isLocked()) {
log.info("Failed to acquire schema lock. Trying again in 10 seconds.");
Thread.sleep(10000);
schemaLock.lock();
}
runnable.run();
} catch (Throwable t) {
throw new RuntimeException(t);
} finally {
schemaLock.release();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment