Skip to content

Commit

Permalink
perf: EXPOSED-204 Performance problem with getConnection()
Browse files Browse the repository at this point in the history
Switch unitialized Database.dataSourceIsolationLevel from nullable to -1.
  • Loading branch information
bog-walk committed Dec 14, 2023
1 parent 9a0a3e4 commit 1057508
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ class Database private constructor(
/**
* The transaction isolation level defined by a [DataSource] connection.
*
* This should only hold a non-null value if [connectsViaDataSource] has been set to `true`.
* This should only hold a value other than -1 if [connectsViaDataSource] has been set to `true`.
*/
internal var dataSourceIsolationLevel: Int? = null
internal var dataSourceIsolationLevel: Int = -1

companion object {
internal val dialects = ConcurrentHashMap<String, () -> DatabaseDialect>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class ThreadLocalTransactionManager(
field = Database.getDefaultIsolationLevel(db)
}
db.connectsViaDataSource && loadDataSourceIsolationLevel -> {
db.dataSourceIsolationLevel?.let {
if (db.dataSourceIsolationLevel != -1) {
loadDataSourceIsolationLevel = false
field = it
field = db.dataSourceIsolationLevel
}
}
}
Expand Down Expand Up @@ -123,12 +123,12 @@ class ThreadLocalTransactionManager(
// Transaction isolation should go first as readOnly or autoCommit can start transaction with wrong isolation level
// Some drivers start a transaction right after `setAutoCommit(false)`,
// which makes `setReadOnly` throw an exception if it is called after `setAutoCommit`
if (db.connectsViaDataSource && loadDataSourceIsolationLevel && db.dataSourceIsolationLevel == null) {
if (db.connectsViaDataSource && loadDataSourceIsolationLevel && db.dataSourceIsolationLevel == -1) {
// retrieves the setting of the datasource connection & caches it
db.dataSourceIsolationLevel = transactionIsolation
} else if (
!db.connectsViaDataSource ||
db.dataSourceIsolationLevel?.equals(this@ThreadLocalTransaction.transactionIsolation) != true
db.dataSourceIsolationLevel != this@ThreadLocalTransaction.transactionIsolation
) {
// only set the level if there is no cached datasource value or if the value differs
transactionIsolation = this@ThreadLocalTransaction.transactionIsolation
Expand Down

0 comments on commit 1057508

Please sign in to comment.