-
Notifications
You must be signed in to change notification settings - Fork 52
Add regression for executor transaction isolation #1913
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
base: 7.0.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| package io.micronaut.configuration.jdbi.example | ||
|
|
||
| import io.micronaut.configuration.jdbi.example.jdbitransaction.ExecutorTransactionIsolationService | ||
| import io.micronaut.configuration.jdbi.example.jdbitransaction.ConcurrentTransactionsBug | ||
| import io.micronaut.context.ApplicationContext | ||
| import io.micronaut.context.DefaultApplicationContext | ||
|
|
@@ -117,4 +118,27 @@ class ApplicationSpec extends Specification { | |
| dbSetup.drop() | ||
| applicationContext.close() | ||
| } | ||
|
|
||
| def "test executor work after commit uses a separate transaction"() { | ||
| given: | ||
| ApplicationContext applicationContext = new DefaultApplicationContext("test") | ||
| applicationContext.environment.addPropertySource(MapPropertySource.of( | ||
| 'test', | ||
| ['datasources.default': [:]] | ||
| )) | ||
| applicationContext.start() | ||
|
|
||
| when: | ||
| def dbSetup = applicationContext.getBean(DatabaseSetup) | ||
| def service = applicationContext.getBean(ExecutorTransactionIsolationService) | ||
| dbSetup.initialize() | ||
| def count = service.executeAsyncTransactionAfterCommit() | ||
|
|
||
| then: | ||
| count == 2 | ||
|
Comment on lines
+135
to
+138
|
||
|
|
||
| cleanup: | ||
| dbSetup.drop() | ||
| applicationContext.close() | ||
|
Comment on lines
+140
to
+142
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,73 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package io.micronaut.configuration.jdbi.example.jdbitransaction; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import io.micronaut.scheduling.TaskExecutors; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import io.micronaut.transaction.TransactionOperations; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import io.micronaut.transaction.support.TransactionSynchronization; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import jakarta.inject.Named; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import jakarta.inject.Singleton; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.jdbi.v3.core.Jdbi; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.sql.Connection; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.concurrent.CountDownLatch; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.concurrent.ExecutorService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.concurrent.TimeUnit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.concurrent.atomic.AtomicReference; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Singleton | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class ExecutorTransactionIsolationService { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final Jdbi jdbi; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final TransactionOperations<Connection> transactionOperations; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final ExecutorService executorService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public ExecutorTransactionIsolationService( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Jdbi jdbi, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Named("default") TransactionOperations<Connection> transactionOperations, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Named(TaskExecutors.SCHEDULED) ExecutorService executorService | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.jdbi = jdbi; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.transactionOperations = transactionOperations; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.executorService = executorService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public int executeAsyncTransactionAfterCommit() throws InterruptedException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CountDownLatch completed = new CountDownLatch(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| AtomicReference<Throwable> failure = new AtomicReference<>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| transactionOperations.executeWrite(status -> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| jdbi.useHandle(handle -> handle.execute("INSERT INTO books(id, name) VALUES(10, 'outer')")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| status.registerSynchronization(new TransactionSynchronization() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void afterCommit() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| executorService.submit(() -> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| transactionOperations.executeWrite(inner -> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| jdbi.useHandle(handle -> handle.execute("INSERT INTO books(id, name) VALUES(11, 'inner')")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+33
to
+45
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public int executeAsyncTransactionAfterCommit() throws InterruptedException { | |
| CountDownLatch completed = new CountDownLatch(1); | |
| AtomicReference<Throwable> failure = new AtomicReference<>(); | |
| transactionOperations.executeWrite(status -> { | |
| jdbi.useHandle(handle -> handle.execute("INSERT INTO books(id, name) VALUES(10, 'outer')")); | |
| status.registerSynchronization(new TransactionSynchronization() { | |
| @Override | |
| public void afterCommit() { | |
| executorService.submit(() -> { | |
| try { | |
| transactionOperations.executeWrite(inner -> { | |
| jdbi.useHandle(handle -> handle.execute("INSERT INTO books(id, name) VALUES(11, 'inner')")); | |
| private int nextBookId() { | |
| return jdbi.withHandle(handle -> | |
| handle.createQuery("SELECT COALESCE(MAX(id), 0) + 1 FROM books") | |
| .mapTo(Integer.class) | |
| .one() | |
| ); | |
| } | |
| public int executeAsyncTransactionAfterCommit() throws InterruptedException { | |
| CountDownLatch completed = new CountDownLatch(1); | |
| AtomicReference<Throwable> failure = new AtomicReference<>(); | |
| transactionOperations.executeWrite(status -> { | |
| int outerBookId = nextBookId(); | |
| jdbi.useHandle(handle -> handle.createUpdate("INSERT INTO books(id, name) VALUES(:id, :name)") | |
| .bind("id", outerBookId) | |
| .bind("name", "outer") | |
| .execute()); | |
| status.registerSynchronization(new TransactionSynchronization() { | |
| @Override | |
| public void afterCommit() { | |
| executorService.submit(() -> { | |
| try { | |
| transactionOperations.executeWrite(inner -> { | |
| int innerBookId = nextBookId(); | |
| jdbi.useHandle(handle -> handle.createUpdate("INSERT INTO books(id, name) VALUES(:id, :name)") | |
| .bind("id", innerBookId) | |
| .bind("name", "inner") | |
| .execute()); |
Copilot
AI
Apr 15, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
executorService.submit(...) can throw (e.g., RejectedExecutionException if the executor is shut down). If that happens, the latch is never counted down and the method will always time out, masking the real failure. Wrap the submit call itself in a try/catch that sets failure and counts down completed when submission fails.
| executorService.submit(() -> { | |
| try { | |
| transactionOperations.executeWrite(inner -> { | |
| jdbi.useHandle(handle -> handle.execute("INSERT INTO books(id, name) VALUES(11, 'inner')")); | |
| return null; | |
| }); | |
| } catch (Throwable e) { | |
| failure.set(e); | |
| } finally { | |
| completed.countDown(); | |
| } | |
| }); | |
| try { | |
| executorService.submit(() -> { | |
| try { | |
| transactionOperations.executeWrite(inner -> { | |
| jdbi.useHandle(handle -> handle.execute("INSERT INTO books(id, name) VALUES(11, 'inner')")); | |
| return null; | |
| }); | |
| } catch (Throwable e) { | |
| failure.set(e); | |
| } finally { | |
| completed.countDown(); | |
| } | |
| }); | |
| } catch (Throwable e) { | |
| failure.set(e); | |
| completed.countDown(); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cleanup:runs even if thewhen:block fails partway through. If an exception occurs beforedbSetupis initialized successfully,dbSetup.drop()can throw aNullPointerExceptionthat obscures the original failure. Consider guarding the cleanup (if (dbSetup != null)) or using a pattern that ensuresdrop()is only called when initialization completed.