kuvasz-uptime / kuvasz

Kuvasz (pronounce as [ˈkuvɒs]) is an open-source uptime and SSL monitoring service, built in Kotlin on top of the awesome Micronaut framework
Apache License 2.0
47 stars 10 forks source link

Jooq transaction rollback caveat #50

Closed gyulavoros closed 3 years ago

gyulavoros commented 3 years ago

Describe the bug

Transactions in Jooq have a particularity, namely the Configuration that you use to execute SQL statements matters. Each (nested) DSLContext.transaction { configuration: Configuration -> } that you open helps Jooq to determine when to call SQL BEGIN, SAVEPOINT, ROLLBACK, and COMMIT statements.

A quick example

@MicronautTest
class RollbackSpec : FunSpec() {

    @Inject
    lateinit var context: DSLContext

    @Inject
    lateinit var userRepository: UserRepository

    init {
        val user1 = mockUser.setUserId(0).setEmail("user1@test.com")
        val user2 = mockUser.setUserId(1).setEmail("user2@test.com")

        test("Jooq transaction rolls back") {
            context.transaction { configuration ->
                userRepository.insert(user1) // this one stays in the database
                UserRepository(configuration).insert(user2) // this one gets removed
                throw RuntimeException("Rollback")
            }
        }
    }
}

Inside a transaction block, using a configuration from an "outer" context (even implicitly by using Repositories) will end up with records in the database that should have been rolled back.

adamkobor commented 3 years ago

Thanks for the heads-up, I wasn't aware of that 🙌

adamkobor commented 3 years ago

Will be fixed in 1.0.2