Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Db2 forupdate fix #3446

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions ebean-test/src/test/java/org/tests/basic/TestQueryForUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,56 @@ public void testForUpdate() {
}
}

@Test
public void testConcurrentForUpdate() throws InterruptedException {

ResetBasicData.reset();

Thread t1 = new Thread() {
@Override
public void run() {
try (final Transaction transaction = DB.beginTransaction()) {
System.out.println("Thread: before find");
DB.find(Customer.class)
.forUpdate()
// .orderBy().desc("1") // this would help by the locks in DB2
.findList();

System.out.println("Thread: after find");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("Thread: done");
}
}
};

t1.start();

Thread.sleep(100);

long start = System.currentTimeMillis();
try (final Transaction transaction = DB.beginTransaction()) {
if (isH2()) {
DB.sqlUpdate("SET LOCK_TIMEOUT 5000").execute();
}
System.out.println("Main: before find");
DB.find(Customer.class)
.forUpdate()
//.orderBy().desc("1") // this would help by the locks in DB2
.findList();

System.out.println("Main: after find");
}

start = System.currentTimeMillis() - start;

assertThat(start).isGreaterThan(2800);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we check, if the second thread blocks, while the first thread locks the data for at least 3000 ms.


}

@Test
public void testForUpdate_withLimit() {
ResetBasicData.reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ public PlatformIdGenerator createSequenceIdGenerator(BackgroundExecutor be, Data
@Override
protected String withForUpdate(String sql, Query.LockWait lockWait, Query.LockType lockType) {
// NOWAIT and SKIP LOCKED not supported with Db2
return sql + " for update";
return sql + " with rs use and keep update locks";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}