Jooq transaction rollback caveat
gyulavoros opened this issue · 2 comments
gyulavoros commented
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
Thanks for the heads-up, I wasn't aware of that 🙌
adamkobor commented
Will be fixed in 1.0.2