Exposed icon indicating copy to clipboard operation
Exposed copied to clipboard

Can't set "repetitionAttempts" for suspended transactions

Open MrPowerGamerBR opened this issue 5 years ago • 4 comments

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.

MrPowerGamerBR avatar Jul 28 '20 16:07 MrPowerGamerBR

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 avatar Oct 11 '20 13:10 MrPowerGamerBR

@MrPowerGamerBR Why do you just catch an ExposedSQLException rather than all types of exceptions?

hennihaus avatar Sep 01 '22 14:09 hennihaus

@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

MrPowerGamerBR avatar Sep 01 '22 17:09 MrPowerGamerBR

Thanks for your quick reply @MrPowerGamerBR. Your comment was really helpful.

hennihaus avatar Sep 03 '22 12:09 hennihaus

@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 👍

AlbRoehm avatar Dec 09 '22 15:12 AlbRoehm

@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 :)

MrPowerGamerBR avatar Dec 09 '22 15:12 MrPowerGamerBR