/** * Performs a Consul transaction. * * PUT /v1/tx * * @param operations A list of KV operations. * @return A {@link ConsulResponse} containing results and potential errors. */ public ConsulResponse<TxResponse> performTransaction(Operation... operations) { ImmutableTransactionOptions immutableTransactionOptions = ImmutableTransactionOptions.builder().consistencyMode(ConsistencyMode.DEFAULT).build(); return performTransaction(immutableTransactionOptions, operations); }
/** * Returns {@code true} if the given session owned the lock, otherwise {@code false}. */ public static boolean unlockAndDelete(KeyValueClient kv, String key, String sessionId) { try { kv.performTransaction( ImmutableOperation.builder(Verb.UNLOCK).key(key).session(sessionId).build(), ImmutableOperation.builder(Verb.DELETE).key(key).build()); return true; } catch (ConsulException e) { if (e.getCode() == HTTP_CONFLICT) { return false; // didn't own lock; no worries } throw e; } }