Skip to content

Instantly share code, notes, and snippets.

@jsanda
Created November 2, 2016 16:05
Show Gist options
  • Save jsanda/dd3606786b3f215bceaf40ef56068f6a to your computer and use it in GitHub Desktop.
Save jsanda/dd3606786b3f215bceaf40ef56068f6a 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();
TransactionManager transactionManager = cache.getTransactionManager();
try {
boolean acquired = false;
while (!acquired) {
try {
transactionManager.begin();
acquired = cache.lock(key);
} catch (TimeoutException e) {
log.infof("Timed out while trying to acquire lock [%s]", key);
transactionManager.rollback();
}
}
runnable.run();
transactionManager.commit();
} catch (Exception e) {
try {
transactionManager.rollback();
} catch (SystemException e1) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment