Skip to content

Commit 1aed1b5

Browse files
committed
HHH-3628 - Hilo optimizer problem in case of multiple threads accessing the sequence table
- replace the Semaphore with two CountDownLatches that shouldd deliver a more predictable outcome
1 parent eef8a48 commit 1aed1b5

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

hibernate-core/src/test/java/org/hibernate/test/idgen/enhanced/table/concurrent/HiloOptimizerConcurrencyTest.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.List;
1111
import java.util.concurrent.Callable;
1212
import java.util.concurrent.CopyOnWriteArrayList;
13+
import java.util.concurrent.CountDownLatch;
1314
import java.util.concurrent.ExecutionException;
1415
import java.util.concurrent.Executor;
1516
import java.util.concurrent.ExecutorService;
@@ -52,7 +53,7 @@ public class HiloOptimizerConcurrencyTest extends BaseNonConfigCoreFunctionalTes
5253

5354
private boolean createSchema = true;
5455

55-
private ExecutorService executor = Executors.newFixedThreadPool( 2);
56+
private ExecutorService executor = Executors.newFixedThreadPool( 2 );
5657

5758
@Override
5859
protected Class[] getAnnotatedClasses() {
@@ -95,7 +96,8 @@ public void testTwoSessionsParallelGeneration() {
9596

9697
final List<Throwable> errs = new CopyOnWriteArrayList<>();
9798

98-
final Semaphore semaphore = new Semaphore( 0, true );
99+
CountDownLatch firstLatch = new CountDownLatch( 1 );
100+
CountDownLatch secondLatch = new CountDownLatch( 1 );
99101

100102
Callable<Void> callable1 = () -> {
101103
try {
@@ -109,8 +111,8 @@ public void testTwoSessionsParallelGeneration() {
109111
session1.getTransaction().commit();
110112
}
111113
}
112-
semaphore.release();
113-
semaphore.acquire();
114+
firstLatch.countDown();
115+
secondLatch.await();
114116
try {
115117
session1.beginTransaction();
116118
HibPerson p = new HibPerson();
@@ -129,7 +131,8 @@ public void testTwoSessionsParallelGeneration() {
129131

130132
Callable<Void> callable2 = () -> {
131133
try {
132-
semaphore.acquire();
134+
firstLatch.await();
135+
secondLatch.countDown();
133136
try {
134137
session2.beginTransaction();
135138
HibPerson p = new HibPerson();
@@ -142,23 +145,21 @@ public void testTwoSessionsParallelGeneration() {
142145
catch (Throwable t) {
143146
errs.add( t );
144147
}
145-
finally {
146-
semaphore.release();
147-
}
148148

149149
return null;
150150
};
151151

152152
executor.invokeAll( Arrays.asList(
153153
callable1, callable2
154-
) ).forEach( c -> {
154+
) , 30, TimeUnit.SECONDS).forEach( c -> {
155155
try {
156156
c.get();
157157
}
158158
catch (InterruptedException|ExecutionException e) {
159+
Thread.interrupted();
159160
fail(e.getMessage());
160161
}
161-
} );
162+
});
162163

163164
for(Throwable ex : errs) {
164165
fail(ex.getMessage());

0 commit comments

Comments
 (0)