Skip to content

Commit

Permalink
fix: EXPOSED-350 keepLoadedReferencesOutOfTransaction causes duplicat…
Browse files Browse the repository at this point in the history
…e query when true (#2064)

Bug fix in version 0.45.0 introduced a side effect. The query for loading child
references would be executed twice if `keepLoadedReferencesOutOfTransaction = true`.

This happens because the select query was being sent to the database without
checking the cache first for loaded references.

Switched to a search method that goes through the cache first and made sure the
bug fix test also checked that each query was only run once.
  • Loading branch information
bog-walk committed Apr 29, 2024
1 parent 55dc680 commit d2f02dd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,7 @@ abstract class EntityClass<ID : Comparable<ID>, out T : Entity<ID>>(
distinctRefIds.forEach { id ->
cache.getOrPutReferrers(id, refColumn) { result[id]?.let { SizedCollection(it) } ?: emptySized() }.also {
if (keepLoadedReferenceOutOfTransaction) {
val childEntity = find { refColumn eq id }.firstOrNull()
childEntity?.storeReferenceInCache(refColumn, it)
cache.find(this, id)?.storeReferenceInCache(refColumn, it)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1679,7 +1679,18 @@ class EntityTests : DatabaseTestsBase() {
country = lebanon
}

debug = true

Country.all().with(Country::dishes)

statementStats
.filterKeys { it.startsWith("SELECT ") }
.forEach { (_, stats) ->
val (count, _) = stats
assertEquals(1, count)
}

debug = false
} finally {
SchemaUtils.drop(Dishes, Countries)
}
Expand Down

0 comments on commit d2f02dd

Please sign in to comment.