Skip to content

Commit

Permalink
Update to Kotlin 1.9.0, fix suspendable nested transactions not worki…
Browse files Browse the repository at this point in the history
…ng in Exposed 0.42.0
  • Loading branch information
MrPowerGamerBR committed Aug 9, 2023
1 parent 885ecd2 commit 2687586
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 15 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.7.10"
kotlin("jvm") version "1.9.0"
`maven-publish`
}

val exposedPowerUtils = "1.2.1"
val exposedPowerUtils = "1.3.0"

allprojects {
group = "net.perfectdreams.exposedpowerutils"
Expand Down
2 changes: 1 addition & 1 deletion exposed-power-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

dependencies {
implementation("org.jetbrains.exposed:exposed-core:0.41.1")
implementation("org.jetbrains.exposed:exposed-core:0.42.0")
implementation("io.github.microutils:kotlin-logging:2.1.23")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ suspend fun <T> transaction(
database,
transactionIsolation
) {
// We are handling the repetition attempts ourselves, see https://github.com/JetBrains/Exposed/commit/88315512d392a7f723ae387f87f9656ccf9b8210
// If we keep Exposed handling the repetition attempts, it will throw exceptions saying that the connection is closed
repetitionAttempts = 0
withContext(coroutineContext + CoroutineTransaction(this)) {
statement.invoke(this@newSuspendedTransaction)
}
Expand Down
4 changes: 2 additions & 2 deletions postgres-java-time/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ plugins {
}

dependencies {
implementation("org.jetbrains.exposed:exposed-core:0.41.1")
implementation("org.postgresql:postgresql:42.3.3")
implementation("org.jetbrains.exposed:exposed-core:0.42.0")
implementation("org.postgresql:postgresql:42.3.8")
}

tasks.withType<KotlinCompile> {
Expand Down
4 changes: 2 additions & 2 deletions postgres-power-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ plugins {
}

dependencies {
implementation("org.jetbrains.exposed:exposed-core:0.41.1")
implementation("org.postgresql:postgresql:42.3.3")
implementation("org.jetbrains.exposed:exposed-core:0.42.0")
implementation("org.postgresql:postgresql:42.3.8")
}

tasks.withType<KotlinCompile> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class InsertOrUpdate<Key : Any>(
isIgnore: Boolean = false,
private vararg val keys: Column<*>
) : InsertStatement<Key>(table, isIgnore) {
override fun prepareSQL(transaction: Transaction): String {
override fun prepareSQL(transaction: Transaction, prepared: Boolean): String {
val tm = TransactionManager.current()
val updateSetter = (table.columns - keys).joinToString { "${tm.identity(it)} = EXCLUDED.${tm.identity(it)}" }
val onConflict = "ON CONFLICT (${keys.joinToString { tm.identity(it) }}) DO UPDATE SET $updateSetter"
return "${super.prepareSQL(transaction)} $onConflict"
return "${super.prepareSQL(transaction, prepared)} $onConflict"
}
}

Expand Down Expand Up @@ -78,10 +78,10 @@ class BatchInsertOrUpdate(
private vararg val keys: Column<*>,
shouldReturnGeneratedValues: Boolean
) : BatchInsertStatement(table, isIgnore, shouldReturnGeneratedValues) {
override fun prepareSQL(transaction: Transaction): String {
override fun prepareSQL(transaction: Transaction, prepared: Boolean): String {
val tm = TransactionManager.current()
val updateSetter = (table.columns - keys).joinToString { "${tm.identity(it)} = EXCLUDED.${tm.identity(it)}" }
val onConflict = "ON CONFLICT (${keys.joinToString { tm.identity(it) }}) DO UPDATE SET $updateSetter"
return "${super.prepareSQL(transaction)} $onConflict"
return "${super.prepareSQL(transaction, prepared)} $onConflict"
}
}
8 changes: 4 additions & 4 deletions tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ dependencies {
testImplementation(project(":exposed-power-utils"))
testImplementation(project(":postgres-java-time"))
testImplementation(project(":postgres-power-utils"))
testImplementation("org.jetbrains.exposed:exposed-core:0.41.1")
testImplementation("org.jetbrains.exposed:exposed-jdbc:0.41.1")
testImplementation("org.jetbrains.exposed:exposed-dao:0.41.1")
testImplementation("org.jetbrains.exposed:exposed-core:0.42.0")
testImplementation("org.jetbrains.exposed:exposed-jdbc:0.42.0")
testImplementation("org.jetbrains.exposed:exposed-dao:0.42.0")
testImplementation("com.zaxxer:HikariCP:5.0.1")
testImplementation("org.postgresql:postgresql:42.3.3")
testImplementation("org.postgresql:postgresql:42.3.8")
testImplementation("ch.qos.logback:logback-classic:1.3.0-alpha14")
testImplementation("org.junit.jupiter:junit-jupiter:5.8.2")
testImplementation("org.testcontainers:testcontainers:1.16.3")
Expand Down
55 changes: 55 additions & 0 deletions tests/src/test/kotlin/SuspendableNestedTransactionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import com.zaxxer.hikari.metrics.prometheus.PrometheusMetricsTrackerFactory
import kotlinx.coroutines.*
import net.perfectdreams.exposedpowerutils.sql.jsonb
import net.perfectdreams.exposedpowerutils.sql.postgresEnumeration
import net.perfectdreams.exposedpowerutils.sql.transaction
import org.jetbrains.exposed.dao.id.LongIdTable
import org.jetbrains.exposed.exceptions.ExposedSQLException
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.Transaction
import org.jetbrains.exposed.sql.select
import org.junit.Rule
import org.junit.Test
import org.testcontainers.containers.PostgreSQLContainer
Expand Down Expand Up @@ -64,7 +69,57 @@ class SuspendableNestedTransactionTest {
}
}

@Test
fun `test suspendable nested transactions repeat workaround`() {
val hikariConfig = HikariConfig()

hikariConfig.driverClassName = "org.postgresql.Driver"
hikariConfig.jdbcUrl = postgres.jdbcUrl
hikariConfig.username = postgres.username
hikariConfig.password = postgres.password

val hikariDataSource = HikariDataSource(hikariConfig)

val test = Database.connect(hikariDataSource)

val totalRepeats = 5
var repeats = 0

println("starting REALLL")
runBlocking {
coroutineScope {
transaction(Dispatchers.IO, test) {
println("creating the MISSING TABLES AND COLUMNS")
SchemaUtils.createMissingTablesAndColumns(Names)
}

println("DOING THE REPEAT THINGY WOW")
repeat(10) {
try {
transaction(Dispatchers.IO, test, repetitions = totalRepeats) {
println("EXECUTING IT!!!")
repeats++

// We want to intentionally throw an error here
Names.select { Names.name.regexp("\\Q") }
.toList()
}
} catch (e: ExposedSQLException) {
val message = e.message ?: error("Exception does not have an message!")
if (message.contains("Connection is closed")) {
error("Connection is closed when it shouldn't be closed")
}
}
}
}
}
}

private suspend fun differentFunctionCall(t1: Transaction, db: Database) = transaction(Dispatchers.IO, db) {
assert(t1 == this) { "Different Function Call transaction is not equal to Transaction1!" }
}

object Names : LongIdTable() {
val name = text("name")
}
}

0 comments on commit 2687586

Please sign in to comment.