From 5ec2b2257a71236a652c89f62a39688bd3dbacfe Mon Sep 17 00:00:00 2001 From: Kyle Aure Date: Mon, 14 Aug 2023 15:25:03 -0500 Subject: [PATCH] Avoid transaction race condition --- .../tx/TransactionServlet.java | 13 ++++++++----- .../tx/TransactionServlet.java | 13 ++++++++----- .../ManagedThreadFactory/tx/TransactionServlet.java | 11 +++++++---- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedExecutorService/tx/TransactionServlet.java b/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedExecutorService/tx/TransactionServlet.java index dd664046..3a37d421 100644 --- a/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedExecutorService/tx/TransactionServlet.java +++ b/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedExecutorService/tx/TransactionServlet.java @@ -43,7 +43,7 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -@SuppressWarnings({ "serial", "unused" }) +@SuppressWarnings({ "serial" }) @WebServlet(Constants.CONTEXT_PATH) @DataSourceDefinition( name = "java:comp/env/jdbc/ManagedExecutorServiceDB", @@ -95,17 +95,20 @@ public void cancelTest() { CancelledTransactedTask cancelledTask = new CancelledTransactedTask(Constants.SQL_TEMPLATE_INSERT); Future future = scheduledExecutor.submit(cancelledTask); - // then cancel it after transaction begin and + // wait for transaction to begin Wait.waitForTransactionBegan(cancelledTask); - // before it commit. + // set flag to rollback transaction cancelledTask.getCancelTransaction().set(true); - // continue to run if possible. + // continue query cancelledTask.getRunQuery().set(true); + + // wait for transaction to finish + Wait.waitForTaskComplete(future); + // verify transaction rolled back int afterTransacted = Counter.getCount(); - assertEquals(originTableCount, afterTransacted, "task was not properly cancelled"); } } diff --git a/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedScheduledExecutorService/tx/TransactionServlet.java b/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedScheduledExecutorService/tx/TransactionServlet.java index cf342a11..b11ec457 100644 --- a/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedScheduledExecutorService/tx/TransactionServlet.java +++ b/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedScheduledExecutorService/tx/TransactionServlet.java @@ -43,7 +43,7 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -@SuppressWarnings({ "serial", "unused" }) +@SuppressWarnings({ "serial" }) @WebServlet(Constants.CONTEXT_PATH) @DataSourceDefinition( name = "java:comp/env/jdbc/ManagedScheduledExecutorServiceDB", @@ -94,17 +94,20 @@ public void cancelTest() { CancelledTransactedTask cancelledTask = new CancelledTransactedTask(Constants.SQL_TEMPLATE_INSERT); Future future = managedScheduledExecutorService.schedule(cancelledTask, new OnceTrigger()); - // then cancel it after transaction begin and + // wait for transaction to begin Wait.waitForTransactionBegan(cancelledTask); - // before it commit. + // set flag to rollback transaction cancelledTask.getCancelTransaction().set(true); - // continue to run if possible. + // continue query cancelledTask.getRunQuery().set(true); + + // wait for transaction to finish + Wait.waitForTaskComplete(future); + // verify transaction rolled back int afterTransacted = Counter.getCount(); - assertEquals(originTableCount, afterTransacted, "task was not properly cancelled"); } } diff --git a/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedThreadFactory/tx/TransactionServlet.java b/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedThreadFactory/tx/TransactionServlet.java index c762b85a..481446a2 100644 --- a/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedThreadFactory/tx/TransactionServlet.java +++ b/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedThreadFactory/tx/TransactionServlet.java @@ -94,17 +94,20 @@ public void cancelTest() { Thread thread = threadFactory.newThread(cancelledTask); thread.start(); - // then cancel it after transaction begin and + // wait for transaction to begin Wait.waitForTransactionBegan(cancelledTask); - // before it commit. + // set flag to rollback transaction cancelledTask.getCancelTransaction().set(true); - // continue to run if possible. + // continue query cancelledTask.getRunQuery().set(true); + + // wait for transaction to finish + Wait.waitTillThreadFinish(thread); + // verify transaction rolled back int afterTransacted = Counter.getCount(); - assertEquals(originTableCount, afterTransacted, "task was not properly cancelled"); } }