Skip to content

Commit

Permalink
fix: fixing transaction rolled back issues with multithreaded bulk im…
Browse files Browse the repository at this point in the history
…port
  • Loading branch information
tamassoltesz committed Sep 27, 2024
1 parent b51ed98 commit 1a60fdb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ protected <T> T startTransactionHelper(TransactionLogic<T> logic, TransactionIso

@Override
public void commitTransaction(TransactionConnection con) throws StorageQueryException {
// We do not want to commit the queries when using the BulkImportProxyStorage to be able to rollback everything
// if any query fails while importing the user

}

@Override
Expand All @@ -86,7 +85,7 @@ public void initStorage(boolean shouldWait, List<TenantIdentifier> tenantIdentif
public void closeConnectionForBulkImportProxyStorage() throws StorageQueryException {
try {
if (this.connection != null) {
this.connection.close();
this.connection.closeForBulkImportProxyStorage();
this.connection = null;
}
ConnectionPool.close(this);
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/io/supertokens/storage/mysql/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.supertokens.pluginInterface.authRecipe.LoginMethod;
import io.supertokens.pluginInterface.authRecipe.sqlStorage.AuthRecipeSQLStorage;
import io.supertokens.pluginInterface.bulkimport.BulkImportUser;
import io.supertokens.pluginInterface.bulkimport.exceptions.BulkImportTransactionRolledBackException;
import io.supertokens.pluginInterface.bulkimport.sqlStorage.BulkImportSQLStorage;
import io.supertokens.pluginInterface.dashboard.DashboardSearchTags;
import io.supertokens.pluginInterface.dashboard.DashboardSessionInfo;
Expand Down Expand Up @@ -293,7 +294,15 @@ public <T> T startTransaction(TransactionLogic<T> logic, TransactionIsolationLev
if ((e instanceof SQLTransactionRollbackException
|| (e.getMessage() != null && e.getMessage().toLowerCase().contains("deadlock")))
&& tries < NUM_TRIES) {

try {
if(this instanceof BulkImportProxyStorage){
throw new StorageTransactionLogicException(new BulkImportTransactionRolledBackException(e));
// if the current instance is of BulkImportProxyStorage, that means we are doing a bulk import
// which uses nested transactions. With MySQL this retry logic doesn't going to work, we have
// to retry the whole "big" transaction, not just the innermost, current one.
// @see BulkImportTransactionRolledBackException for more explanation.
}
Thread.sleep((long) (10 + (250 + Math.min(Math.pow(2, tries), 3000)) * Math.random()));
} catch (InterruptedException ignored) {
}
Expand Down

0 comments on commit 1a60fdb

Please sign in to comment.