Skip to content

Commit df35a2f

Browse files
committed
fix: revert transaction isolation changes
1 parent 91c9331 commit df35a2f

File tree

6 files changed

+35
-44
lines changed

6 files changed

+35
-44
lines changed

src/main/java/io/supertokens/storage/postgresql/BulkImportProxyConnection.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,7 @@ public String getCatalog() throws SQLException {
130130

131131
@Override
132132
public void setTransactionIsolation(int level) throws SQLException {
133-
if (level != Connection.TRANSACTION_READ_COMMITTED) {
134-
this.con.setTransactionIsolation(level);
135-
}
133+
this.con.setTransactionIsolation(level);
136134
}
137135

138136
@Override

src/main/java/io/supertokens/storage/postgresql/BulkImportProxyStorage.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616

1717
package io.supertokens.storage.postgresql;
1818

19+
import java.sql.Connection;
20+
import java.sql.SQLException;
21+
import java.util.List;
22+
1923
import io.supertokens.pluginInterface.exceptions.DbInitException;
2024
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
2125
import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException;
2226
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
2327
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
2428
import io.supertokens.pluginInterface.sqlStorage.TransactionConnection;
2529

26-
import java.sql.Connection;
27-
import java.sql.SQLException;
28-
import java.util.List;
29-
3030

3131
/**
3232
* BulkImportProxyStorage is a class extending Start, serving as a Storage instance in the bulk import user cronjob.

src/main/java/io/supertokens/storage/postgresql/ConnectionPool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private synchronized void initialiseHikariDataSource() throws SQLException, Stor
5454
HikariConfig config = new HikariConfig();
5555
PostgreSQLConfig userConfig = Config.getConfig(start);
5656
config.setDriverClassName("org.postgresql.Driver");
57-
config.setTransactionIsolation("TRANSACTION_READ_COMMITTED");
57+
config.setTransactionIsolation("TRANSACTION_SERIALIZABLE");
5858

5959
String scheme = userConfig.getConnectionScheme();
6060

src/main/java/io/supertokens/storage/postgresql/Start.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ public void initStorage(boolean shouldWait, List<TenantIdentifier> tenantIdentif
342342
@Override
343343
public <T> T startTransaction(TransactionLogic<T> logic)
344344
throws StorageTransactionLogicException, StorageQueryException {
345-
return startTransaction(logic, TransactionIsolationLevel.READ_COMMITTED);
345+
return startTransaction(logic, TransactionIsolationLevel.SERIALIZABLE);
346346
}
347347

348348
@Override
@@ -431,7 +431,7 @@ protected <T> T startTransactionHelper(TransactionLogic<T> logic, TransactionIso
431431
try {
432432
con = ConnectionPool.getConnection(this);
433433
defaultTransactionIsolation = con.getTransactionIsolation();
434-
int libIsolationLevel = Connection.TRANSACTION_READ_COMMITTED;
434+
int libIsolationLevel = Connection.TRANSACTION_SERIALIZABLE;
435435
switch (isolationLevel) {
436436
case SERIALIZABLE:
437437
libIsolationLevel = Connection.TRANSACTION_SERIALIZABLE;
@@ -449,9 +449,7 @@ protected <T> T startTransactionHelper(TransactionLogic<T> logic, TransactionIso
449449
libIsolationLevel = Connection.TRANSACTION_NONE;
450450
break;
451451
}
452-
if (libIsolationLevel != Connection.TRANSACTION_READ_COMMITTED) {
453-
con.setTransactionIsolation(libIsolationLevel);
454-
}
452+
con.setTransactionIsolation(libIsolationLevel);
455453
con.setAutoCommit(false);
456454
return logic.mainLogicAndCommit(new TransactionConnection(con));
457455
} catch (Exception e) {

src/main/java/io/supertokens/storage/postgresql/queries/MultitenancyQueries.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
2323
import io.supertokens.pluginInterface.multitenancy.ThirdPartyConfig;
2424
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
25-
import io.supertokens.pluginInterface.sqlStorage.SQLStorage;
2625
import io.supertokens.storage.postgresql.Start;
2726
import io.supertokens.storage.postgresql.config.Config;
2827
import io.supertokens.storage.postgresql.queries.multitenancy.MfaSqlHelper;
@@ -270,7 +269,7 @@ public static void overwriteTenantConfig(Start start, TenantConfig tenantConfig)
270269
}
271270

272271
return null;
273-
}, SQLStorage.TransactionIsolationLevel.SERIALIZABLE);
272+
});
274273
}
275274

276275
public static TenantConfig[] getAllTenants(Start start) throws StorageQueryException {

src/test/java/io/supertokens/storage/postgresql/test/DeadlockTest.java

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@
1717

1818
package io.supertokens.storage.postgresql.test;
1919

20+
import java.sql.Connection;
21+
import java.sql.SQLException;
22+
import java.util.concurrent.ExecutorService;
23+
import java.util.concurrent.Executors;
24+
import java.util.concurrent.TimeUnit;
25+
import java.util.concurrent.atomic.AtomicBoolean;
26+
import java.util.concurrent.atomic.AtomicLong;
27+
import java.util.concurrent.atomic.AtomicLongArray;
28+
import java.util.concurrent.atomic.AtomicReference;
29+
30+
import org.junit.AfterClass;
31+
import static org.junit.Assert.assertNotNull;
32+
import static org.junit.Assert.assertNull;
33+
import static org.junit.Assert.assertTrue;
34+
import org.junit.Before;
35+
import org.junit.Rule;
36+
import org.junit.Test;
37+
import org.junit.rules.TestRule;
38+
2039
import io.supertokens.ProcessState;
2140
import io.supertokens.authRecipe.AuthRecipe;
2241
import io.supertokens.emailpassword.EmailPassword;
@@ -32,31 +51,13 @@
3251
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
3352
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
3453
import io.supertokens.pluginInterface.sqlStorage.SQLStorage;
35-
import io.supertokens.pluginInterface.sqlStorage.SQLStorage.TransactionIsolationLevel;
3654
import io.supertokens.pluginInterface.totp.TOTPDevice;
3755
import io.supertokens.pluginInterface.totp.TOTPUsedCode;
3856
import io.supertokens.pluginInterface.totp.exception.UnknownTotpUserIdException;
3957
import io.supertokens.pluginInterface.totp.exception.UsedCodeAlreadyExistsException;
4058
import io.supertokens.pluginInterface.totp.sqlStorage.TOTPSQLStorage;
41-
import io.supertokens.storageLayer.StorageLayer;
42-
import org.junit.AfterClass;
43-
import org.junit.Before;
44-
import org.junit.Rule;
45-
import org.junit.Test;
46-
import org.junit.rules.TestRule;
47-
48-
import java.sql.Connection;
49-
import java.sql.SQLException;
50-
import java.util.concurrent.ExecutorService;
51-
import java.util.concurrent.Executors;
52-
import java.util.concurrent.TimeUnit;
53-
import java.util.concurrent.atomic.AtomicBoolean;
54-
import java.util.concurrent.atomic.AtomicLong;
55-
import java.util.concurrent.atomic.AtomicLongArray;
56-
import java.util.concurrent.atomic.AtomicReference;
57-
5859
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.update;
59-
import static org.junit.Assert.*;
60+
import io.supertokens.storageLayer.StorageLayer;
6061

6162
public class DeadlockTest {
6263
@Rule
@@ -266,8 +267,7 @@ public void testCodeCreationRapidlyWithDifferentEmails() throws Exception {
266267
.checkOrWaitForEventInPlugin(
267268
io.supertokens.storage.postgresql.ProcessState.PROCESS_STATE.DEADLOCK_NOT_RESOLVED));
268269

269-
// Deadlock doesn't occur with READ_COMMITTED
270-
assertNull(process
270+
assertNotNull(process
271271
.checkOrWaitForEventInPlugin(
272272
io.supertokens.storage.postgresql.ProcessState.PROCESS_STATE.DEADLOCK_FOUND));
273273

@@ -433,8 +433,7 @@ public void testConcurrentDeleteAndUpdate() throws Exception {
433433
assertTrue(!t1Failed.get() && !t2Failed.get());
434434
assert (t1State.get().equals("commit") && t2State.get().equals("commit"));
435435

436-
// Deadlock doesn't occur with READ_COMMITTED
437-
assertNull(process.checkOrWaitForEventInPlugin(
436+
assertNotNull(process.checkOrWaitForEventInPlugin(
438437
io.supertokens.storage.postgresql.ProcessState.PROCESS_STATE.DEADLOCK_FOUND));
439438

440439
process.kill();
@@ -520,7 +519,7 @@ public void testConcurrentDeleteAndInsert() throws Exception {
520519
t1Failed.set(false);
521520

522521
return null;
523-
}, TransactionIsolationLevel.READ_COMMITTED);
522+
}/*, TransactionIsolationLevel.READ_COMMITTED */);
524523
} catch (StorageQueryException | StorageTransactionLogicException e) {
525524
// This is expected because of "could not serialize access"
526525
t1Failed.set(true);
@@ -602,8 +601,7 @@ public void testConcurrentDeleteAndInsert() throws Exception {
602601
assertTrue(!t1Failed.get() && t2Failed.get());
603602
assert (t1State.get().equals("commit") && t2State.get().equals("query"));
604603

605-
// Deadlock doesn't occur anymore with READ_COMMITTED
606-
assertNull(process
604+
assertNotNull(process
607605
.checkOrWaitForEventInPlugin(
608606
io.supertokens.storage.postgresql.ProcessState.PROCESS_STATE.DEADLOCK_FOUND,
609607
1000));
@@ -654,7 +652,6 @@ public void testLinkAccountsInParallel() throws Exception {
654652
.checkOrWaitForEventInPlugin(
655653
io.supertokens.storage.postgresql.ProcessState.PROCESS_STATE.DEADLOCK_NOT_RESOLVED));
656654

657-
// Deadlock doesn't occur with READ_COMMITTED
658655
assertNull(process
659656
.checkOrWaitForEventInPlugin(
660657
io.supertokens.storage.postgresql.ProcessState.PROCESS_STATE.DEADLOCK_FOUND));
@@ -700,8 +697,7 @@ public void testCreatePrimaryInParallel() throws Exception {
700697
assertNull(process
701698
.checkOrWaitForEventInPlugin(
702699
io.supertokens.storage.postgresql.ProcessState.PROCESS_STATE.DEADLOCK_NOT_RESOLVED));
703-
// No deadlock happens now with READ_COMMITTED
704-
assertNull(process
700+
assertNotNull(process
705701
.checkOrWaitForEventInPlugin(
706702
io.supertokens.storage.postgresql.ProcessState.PROCESS_STATE.DEADLOCK_FOUND));
707703

0 commit comments

Comments
 (0)