Skip to content

Commit

Permalink
Bug when calling TransactionManager.closeAndUnregister from a differe…
Browse files Browse the repository at this point in the history
…nt Thread / Fix #2 #1387
  • Loading branch information
Tapac committed Nov 28, 2021
1 parent b1453c9 commit 621dc5b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ interface TransactionManager {
if (defaultDatabase == null) {
currentThreadManager.remove()
}
if (!registeredDatabases.containsKey(database)) {
databases.push(database)
}

registeredDatabases[database] = manager
databases.push(database)
}

@Synchronized fun closeAndUnregister(database: Database) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.transactions.transactionManager
import org.junit.Test
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

class H2Tests : DatabaseTestsBase() {

Expand Down Expand Up @@ -69,6 +73,29 @@ class H2Tests : DatabaseTestsBase() {
}
}

@Test
fun closeAndUnregister() {
withDb(TestDB.H2) { testDB ->
val orignalManager = TransactionManager.manager
val db = requireNotNull(testDB.db) { "testDB.db cannot be null" }
try {
TransactionManager.registerManager(
db,
WrappedTransactionManager(db.transactionManager)
)
Executors.newSingleThreadExecutor().apply {
submit { TransactionManager.closeAndUnregister(db) }
.get(1, TimeUnit.SECONDS)
}.shutdown()
} finally {
TransactionManager.registerManager(db, orignalManager)
}
}
}

class WrappedTransactionManager(val transactionManager: TransactionManager) :
TransactionManager by transactionManager

object Testing : Table("H2_TESTING") {
val id = integer("id").autoIncrement() // Column<Int>
val string = varchar("string", 128)
Expand Down

0 comments on commit 621dc5b

Please sign in to comment.