Exposed
Exposed copied to clipboard
Can't set "repetitionAttempts" for suspended transactions
I think the title already explains everything, there isn't a way to set the repetition attempts for newSuspendedTransaction or suspendedTransactionAsync
Also, I may be wrong, but it seems that by default suspended transactions do not retry like how transaction behaves, it always fails when a concurrent update/delete/etc exception happens, while with non suspended transactions Exposed tries again.
My own workaround for this issue, mimicking Exposed's transaction behavior.:
suspend fun <T> newSuspendedTransaction(repetitions: Int = 5, statement: org.jetbrains.exposed.sql.Transaction.() -> T): T {
var lastException: Exception? = null
for (i in 1..repetitions) {
try {
return org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction(Dispatchers.IO, Databases.loritta) {
statement.invoke(this)
}
} catch (e: ExposedSQLException) {
logger.warn(e) { "Exception while trying to execute query. Tries: $i" }
lastException = e
}
}
throw lastException ?: RuntimeException("This should never happen")
}
@MrPowerGamerBR Why do you just catch an ExposedSQLException rather than all types of exceptions?
@MrPowerGamerBR Why do you just catch an ExposedSQLException rather than all types of exceptions?
Because if any other exception happened during the transaction, probably it is an issue in your application, and trying to retry the transaction wouldn't fix the issue.
Also because Exposed's blocking transaction also catches SQLException instead of catching all types. https://github.com/JetBrains/Exposed/blob/master/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/transactions/ThreadLocalTransactionManager.kt#L217
Thanks for your quick reply @MrPowerGamerBR. Your comment was really helpful.
@MrPowerGamerBR are you still using this workaround? I encountered a similar issue and i guess will go a the same route then you since i did not find any other option to provide a retry value. Btw. thanks for providing the sample code 👍
@MrPowerGamerBR are you still using this workaround? I encountered a similar issue and i guess will go a the same route then you since i did not find any other option to provide a retry value. Btw. thanks for providing the sample code 👍
Yup! And it works fine :)