Skip to content

Commit

Permalink
jdbc: allow calling of PooledConnection.close() multiple times #80
Browse files Browse the repository at this point in the history
  • Loading branch information
angryziber committed Dec 13, 2024
1 parent c84f47d commit da102cf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Unreleased
* json: fix TSGenerator on Windows
* server: `Server(InetSocketAddress(0))` can now be used to bind to any available port.
* server: make idle restarts quick #96
* server: initialize `HttpExchange.pathParams` even no route is matched (404), for decorators #82
* jdbc: between operator introduced with open and closed ranges, also in and notIn
* jdbc: @NoTransaction can now be used on jobs
* jdbc: fixed usage of multiple different DataSources when there is an active transaction
* jdbc: allow calling of `PooledConnection.close()` multiple times #80
* json: fix TSGenerator on Windows
* json: TSGenerator will now use more type-safe string template types for java.time classes, e.g. `${number}-${number}-${number}` instead of `string`
* server: `Server(InetSocketAddress(0))` can now be used to bind to any available port.
* server: make idle restarts quick #96
* server: initialize `HttpExchange.pathParams` even no route is matched (404), for decorators #82
* jackson: serialize enums using their toString() method by default, this fixes `openapi` module usage with `jackson` #88
* liquibase: do not close jdbc connection if it was passed by user #81

Expand Down
15 changes: 9 additions & 6 deletions jdbc/src/PooledDataSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,15 @@ class PooledDataSource(

internal fun checkBySetApplicationName() { applicationName = currentThread().name }

override fun close() = try {
used -= this
if (!conn.autoCommit) conn.rollback()
available += this
} catch (e: SQLException) {
log.warn("Failed to return $this: $e")
override fun close() {
try {
if (used.remove(this) != null) {
if (!conn.autoCommit) conn.rollback()
available += this
} else log.warn("Trying to close already returned $this")
} catch (e: SQLException) {
log.warn("Failed to return $this: $e")
}
}

internal fun reallyClose() = try {
Expand Down
17 changes: 17 additions & 0 deletions jdbc/test/PooledDataSourceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,21 @@ class PooledDataSourceTest {
expect(pool.available).toHaveSize(0)
expect(pool.used.keys).toHaveSize(0)
}

@Test fun `close same connection multiple times`() {
val conn = pool.connection
expect(pool.used.keys).toHaveSize(1)
expect(pool.available).toHaveSize(0)

conn.close()
conn.close()
conn.close()

expect(pool.used.keys).toHaveSize(0)
expect(pool.available).toHaveSize(1)

val conn2 = pool.connection
expect(pool.connection).notToEqual(conn2)
expect(pool.connection).notToEqual(conn2)
}
}

0 comments on commit da102cf

Please sign in to comment.