-
Notifications
You must be signed in to change notification settings - Fork 87
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
How to execute a transaction with the model? #379
Comments
any updates here? |
There does not seem to be any support for transactions via Granite models. Nor does there seem to be a way to have the model use the transaction connection for its changes. It defaults to using the models adapter connection, which is why the transaction cannot be rolled back. |
Thank you @kalinon I think that supporting transactions is important, so I've labeled this as part of bringing Granite up to feature parity with ActiveRecord and added the label for this being an |
Just some further comments on how this example is working: # TX comes from the DB shard not granite
User.adapter.database.transaction do |tx|
user = User.find 17
break unless user
# here the lang is "en"
user.update lang: "fr" # This update uses a different connection which is not having a `transaction` happening
tx.rollback # This performs a rollback but on the `tx` connection
puts "rollback"
# here the lang is "fr" (updated)
pp User.find 17
end when you get to So if we wanted this behavior we would need to build a whole transaction feature, where any DB action within the block, would use that connection. Allowing a rollback to occur. he was able to roll back in his first example because hes calling the tx directly:
This feature would probably require some sort of mechanism that checks what thread you are in, and gives you the same connection. However, it would need to probably be implemented on a Granite call (i.e |
Hello,
I am trying to execute a transaction with the model.
It works:
It does not work:
Where am i wrong? Maybe I need to pass the
tx.connection
to the User model?The text was updated successfully, but these errors were encountered: