Created
November 2, 2016 16:05
-
-
Save jsanda/dd3606786b3f215bceaf40ef56068f6a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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