DbClusterLock implements Clearable { private Integer get(final String name) { return JooqUtil.contextResult(clusterLockDbConnProvider, context -> context .select(CLUSTER_LOCK.ID) .from(CLUSTER_LOCK) .where(CLUSTER_LOCK.NAME.eq(name)) .fetchOptional() .map(r -> r.get(CLUSTER_LOCK.ID)) .orElse(null)); } @Inject  DbClusterLock(final ClusterLockDbConnProvider clusterLockDbConnProvider); void lock(final String lockName, final Runnable runnable); @Override void clear();  }
@Test void test() throws InterruptedException { final ClusterLockDbConnProvider clusterLockDbConnProvider = DbTestUtil.getTestDbDatasource( new ClusterLockDbModule(), new ClusterLockConfig()); final DbClusterLock dbClusterLock = new DbClusterLock(clusterLockDbConnProvider); final DbClusterLockThreads dbClusterLockThreads = new DbClusterLockThreads(dbClusterLock); final CountDownLatch countDownLatch = new CountDownLatch(2); final List<Integer> sequence = new ArrayList<>(3); final Thread thread1 = new Thread(() -> { dbClusterLockThreads.thread1("TEST", sequence); countDownLatch.countDown(); }); final Thread thread2 = new Thread(() -> { dbClusterLockThreads.thread2("TEST", sequence); countDownLatch.countDown(); }); thread1.start(); thread2.start(); countDownLatch.await(); assertThat(sequence.size()).isEqualTo(3); assertThat(sequence.get(0).intValue()).isEqualTo(1); assertThat(sequence.get(1).intValue()).isEqualTo(2); assertThat(sequence.get(2).intValue()).isEqualTo(3); }