-
Notifications
You must be signed in to change notification settings - Fork 697
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spring transaction connection leak. #1167
- Loading branch information
Showing
6 changed files
with
156 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...ed-spring-boot-starter/src/test/kotlin/org/jetbrains/exposed/jdbc-template/AuthorTable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.jetbrains.exposed.`jdbc-template` | ||
|
||
import org.jetbrains.exposed.dao.UUIDEntity | ||
import org.jetbrains.exposed.dao.UUIDEntityClass | ||
import org.jetbrains.exposed.dao.id.EntityID | ||
import org.jetbrains.exposed.dao.id.UUIDTable | ||
import java.util.UUID | ||
|
||
object AuthorTable : UUIDTable("authors") { | ||
val description = text("description") | ||
} | ||
|
||
object BookTable : UUIDTable("books") { | ||
val description = text("description") | ||
} | ||
|
||
class Book(id: EntityID<UUID>) : UUIDEntity(id) { | ||
companion object : UUIDEntityClass<Book>(AuthorTable) | ||
var description by AuthorTable.description | ||
} |
54 changes: 54 additions & 0 deletions
54
...ed-spring-boot-starter/src/test/kotlin/org/jetbrains/exposed/jdbc-template/BookService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.jetbrains.exposed.`jdbc-template` | ||
|
||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.jdbc.core.JdbcTemplate | ||
import org.springframework.stereotype.Component | ||
import org.springframework.transaction.support.TransactionOperations | ||
import java.util.UUID | ||
|
||
@Component | ||
open class BookService( | ||
@Qualifier("operations1") | ||
private val operations1: TransactionOperations, | ||
@Qualifier("operations2") | ||
private val operations2: TransactionOperations, | ||
private val jdbcTemplate: JdbcTemplate | ||
) { | ||
|
||
fun testWithSpringAndExposedTransactions() { | ||
transaction { | ||
Book.new { description = "123" } | ||
} | ||
operations1.execute { | ||
val id = UUID.randomUUID().toString() | ||
val query = "insert into authors(id, description) values ('$id', '234234')" | ||
jdbcTemplate.execute(query) | ||
} | ||
} | ||
|
||
fun testWithSpringTransaction() { | ||
operations1.execute { | ||
val id = UUID.randomUUID().toString() | ||
val query = "insert into authors(id, description) values ('$id', '234234')" | ||
jdbcTemplate.execute(query) | ||
} | ||
} | ||
|
||
fun testWithExposedTransaction() { | ||
transaction { | ||
Book.new { description = "1234" } | ||
} | ||
} | ||
|
||
fun testWithoutSpringTransaction() { | ||
transaction { | ||
Book.new { description = "1234" } | ||
} | ||
operations2.execute { | ||
val id = UUID.randomUUID().toString() | ||
val query = "insert into authors(id, description) values ('$id', '234234')" | ||
jdbcTemplate.execute(query) | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...ing-boot-starter/src/test/kotlin/org/jetbrains/exposed/jdbc-template/JdbcConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.jetbrains.exposed.`jdbc-template` | ||
|
||
import org.jetbrains.exposed.spring.SpringTransactionManager | ||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.transaction.support.TransactionOperations | ||
import org.springframework.transaction.support.TransactionTemplate | ||
|
||
@Configuration | ||
open class JdbcConfiguration { | ||
@Bean | ||
@Qualifier("operations1") | ||
open fun operations1(transactionManager: SpringTransactionManager): TransactionOperations { | ||
return TransactionTemplate(transactionManager) | ||
} | ||
|
||
@Bean | ||
@Qualifier("operations2") | ||
open fun operations2() = TransactionOperations.withoutTransaction() | ||
} |
54 changes: 54 additions & 0 deletions
54
...ing-boot-starter/src/test/kotlin/org/jetbrains/exposed/jdbc-template/JdbcTemplateTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.jetbrains.exposed.`jdbc-template` | ||
|
||
import org.jetbrains.exposed.sql.SchemaUtils | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import org.junit.jupiter.api.* | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.autoconfigure.SpringBootApplication | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.test.context.event.annotation.BeforeTestClass | ||
|
||
@SpringBootApplication | ||
open class JdbcTemplateApplication | ||
|
||
@SpringBootTest( | ||
classes = [JdbcTemplateApplication::class], | ||
properties = ["spring.datasource.url=jdbc:h2:mem:test", "spring.datasource.driver-class-name=org.h2.Driver", "spring.exposed.generate-ddl=true"] | ||
) | ||
@TestMethodOrder(MethodOrderer.OrderAnnotation::class) | ||
class JdbcTemplateTests { | ||
|
||
@BeforeTestClass | ||
fun beforeTests() { | ||
transaction { | ||
SchemaUtils.create(AuthorTable, BookTable) | ||
} | ||
} | ||
|
||
@Autowired | ||
lateinit var bookService: BookService | ||
|
||
@Order(1) | ||
@RepeatedTest(15, name = "Without spring transaction: {currentRepetition}/{totalRepetitions}") | ||
fun testWithoutSpringTransaction() { | ||
bookService.testWithoutSpringTransaction() | ||
} | ||
|
||
@Order(2) | ||
@RepeatedTest(15, name = "With spring transaction: {currentRepetition}/{totalRepetitions}") | ||
fun testWithSpringTransaction() { | ||
bookService.testWithSpringTransaction() | ||
} | ||
|
||
@Order(3) | ||
@RepeatedTest(15, name = "With exposed transaction: {currentRepetition}/{totalRepetitions}") | ||
fun testWithExposedTransaction() { | ||
bookService.testWithExposedTransaction() | ||
} | ||
|
||
@Order(4) | ||
@RepeatedTest(15, name = "With spring and exposed transactions: {currentRepetition}/{totalRepetitions}") | ||
fun testWithSpringAndExposedTransactions() { | ||
bookService.testWithSpringAndExposedTransactions() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters