Hello!
I experienced problems with testing transactions which are supposed to
be
rolled back, but are not, because of transactional fixtures eliminating
the
inner transaction. Can this be worked around somehow without turning off
transactional fixtures? Maybe savepoints can help this?
Does anyone have any experience with savepoints to achieve nested
transaction
functionality? Both Postgres and MySQL support savepoints (didn’t check
others).
I was thinking to make a custom patch for AR to create savepoints for
inner
transactions. Can somebody help me with achieving this (esp. with
connection_adapters and wrapping this all up as a plugin perhaps).
Something in the way of:
Wrap a block in a transaction. Returns result of block.
def transaction(start_db_transaction = true)
transaction_open = false
savepoint_open = false
savepoint_name = nil
begin
if block_given?
if start_db_transaction
begin_db_transaction
transaction_open = true
else
savepoint_name = create_savepoint
savepoint_open = true
end
yield
end
rescue Exception => database_transaction_rollback
if transaction_open
transaction_open = false
rollback_db_transaction
end
if savepoint_open
savepoint_open = false
rollback_to_savepoint(savepoint_name)
end
raise
end
ensure
commit_db_transaction if transaction_open
release_savepoint(savepoint_name) if savepoint_open
end
Best regards,
Laas