-
Notifications
You must be signed in to change notification settings - Fork 694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using Suspended Transactions with Exposed #1551
Comments
You are right, coroutines doesn't work well with blocking code (like JDBC) where any call to DB can stuck for unpredictable amount of time. And then you'll get thread blocked on The way to go is to use R2DBC but its support is not yet implemented but you can follow #456 to be notified when it will be ready |
@Tapac
and
Now I have a another function at some other point in the code that does this:
The idea here is that both the upserts go through or none at all. |
Hi Folks,
I am trying to use Exposed with Transactions (Kotlin) and was thinking of using suspended transactions as listed here : https://github.com/JetBrains/Exposed/wiki/Transactions#working-with-coroutines.
I have an use-case, wherein I read a value from a database tables using "SELECT..where key = "K"... FOR UPDATE" in-order to ensure row level locking at database level, process the read value and upsert it back to the table.
Here is what my current code looks like :
databaseWrapperInterfaceImpl.getDataById(it)
is responsible for executing"SELECT..where key = "K"... FOR UPDATE"
. Currently,databaseWrapperInterfaceImpl.getDataById(it)
is not a suspend function.Since this
databaseWrapperInterfaceImpl.getDataById(it)
can block if some other instance of the application is already holding the lock, I was thinking my current thread in my current instance that is executingExe(..)
will also block instead of yielding/ suspending as it would for normal suspending functions in Kotlin (correct me if my understanding is wrong here), which is why I was thinking of using the suspended transactions.However there is a line here (https://github.com/JetBrains/Exposed/wiki/Transactions#working-with-coroutines.) :
**Please note what such code remains blocking (as it still uses JDBC) and you should not try to share a transaction between multiple threads as it will lead to undefined behaviour.**
which is confusing, since if a transaction is suspended, it can be run by different threads at different points in time and there is no guarantee that it is not shared.Let me know if I am misunderstanding anything, here.
The text was updated successfully, but these errors were encountered: