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

Avoid transaction race condition #326

Merged
merged 1 commit into from
Aug 15, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}