### Question:
StateMachineDao implements GetHandle { public boolean unlock(String smId) { return getHandle().inTransaction((conn, status) -> { final Update statement = conn.createStatement("DO RELEASE_LOCK(:lock);"); statement.bind("lock", PURE_FSM_LOCK_PREFIX + smId); statement.execute(); return true; }); }  Set<String> getAllIds(); String getNextId(); void insertStateMachineData(String smId, Transition transition); void updateStateMachineData(String smId, Transition transition); boolean removeStateMachineData(String smId); boolean lock(String smId, long timeout); boolean unlock(String smId); Transition getStateMachineData(String smId);  }### Answer:
@Test public void shouldOnlyAllowOneHandleToObtainLockAtOneTime() throws InterruptedException { final Handle handleOne = JDBI_RULE.DBI.open(); final Handle handleTwo = JDBI_RULE.DBI.open(); final Handle handleThree = JDBI_RULE.DBI.open(); assertTrue(attemptLock(handleOne, "4445")); assertFalse(attemptLock(handleTwo, "4445")); assertFalse(attemptLock(handleTwo, "4445")); assertFalse(attemptLock(handleThree, "4445")); assertFalse(attemptLock(handleThree, "4445")); unlock(handleOne, "4445"); assertTrue(attemptLock(handleTwo, "4445")); assertFalse(attemptLock(handleThree, "4445")); assertFalse(attemptLock(handleThree, "4445")); unlock(handleTwo, "4445"); assertTrue(attemptLock(handleThree, "4445")); assertFalse(attemptLock(handleOne, "4445")); assertFalse(attemptLock(handleOne, "4445")); }
@Test public void unlockShouldBeIdempotent() { final Handle handleOne = JDBI_RULE.DBI.open(); final Handle handleTwo = JDBI_RULE.DBI.open(); final Handle handleThree = JDBI_RULE.DBI.open(); assertTrue(attemptLock(handleOne, "4450")); unlock(handleOne, "4450"); unlock(handleOne, "4450"); assertTrue(attemptLock(handleTwo, "4450")); unlock(handleTwo, "4450"); unlock(handleTwo, "4450"); assertTrue(attemptLock(handleThree, "4450")); unlock(handleThree, "4450"); unlock(handleThree, "4450"); }
@Test public void shouldNotUnlockIfAttemptToUnlockWithHandleThatDidntObtainLock() throws InterruptedException { final Handle handleOne = JDBI_RULE.DBI.open(); final Handle handleTwo = JDBI_RULE.DBI.open(); assertTrue(attemptLock(handleOne, "4446")); assertFalse(attemptLock(handleTwo, "4446")); assertFalse(attemptLock(handleTwo, "4446")); unlock(handleTwo, "4446"); assertTrue(attemptLock(handleOne, "4446")); assertFalse(attemptLock(handleTwo, "4446")); assertFalse(attemptLock(handleTwo, "4446")); }