Replies: 3 comments 2 replies
-
Your example isn't self contained and doesn't have valid syntax, so it's hard to see whether or not is a bug in your code. Your example doesn't even execute the Sequel will rollback the transaction it creates if the transaction block raises an exception. The main reason code in the block you are passing wouldn't be rolled back is if it didn't execute on the same connection used by the transaction. Any chance you could post a minimal, self-contained example showing the issue you are having? Note that self-contained means it connects to the database, creates the related tables, and defines all methods used before calling them. It's something I could copy and paste into a file and run on my own system to reproduce the issue. |
Beta Was this translation helpful? Give feedback.
-
Let's see if it is more clear now @jeremyevans; but if you need a working repo it will take me a little bit more time. I have a repository layer to avoid accessing directly to the DB
Then I have workflows/useCases to execute logic.
so the code before this does The issue is that when something goes wrong inside the like if you raise an exception after
and keep the raise inside the block. the are they executed in different connections? I only initialize the Thanks. |
Beta Was this translation helpful? Give feedback.
-
This is executing the block immediately and passing what the block returns to the method as an argument, not using block. Try this to see the error: def execute_transaction(&block)
raise LocalJumpError unless block_given?
@db.transaction do
block
end
end You want something like: @transaction_manager.execute_transaction { inserts_updates_etc } Also I didn't run the code but I expect it doesn't work, since |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I think this is a bug more than a discussion, but let's confirm I am not doing something wrong.
the scenario is the next, I wanted to abstract the Database layer outside of the logic of my application, so basically, I have some repositories like
table1_repository
and so on.The problem becomes when I have to add transactions into the logic;
I've been looking at the gem and in the documentation, and it seems that there is no other way to do them than do
DB.transaction do ... end
.Note: I know I can use SQL directly to execute that, but it will not take advantage of all the work already done in the gem.
so my idea was to create a
transaction_manager
which will contain a method that receives a block and it gets executed, and the information gets inserted correctly, but if something goes wrong, it does not roll back.Simplest Possible Self-Contained Example Showing the Bug
and block can be any simple piece of code
Am I doing something wrong? or is it a bug?
thanks
Beta Was this translation helpful? Give feedback.
All reactions