ActiveRecord disables nested transactions by using a mutex lock. But sometimes nested transactions are needed. For example when testing code that requires a transaction to be rolled back on some error (e.g. not enough money) and transactional fixtures are turned on.
For such cases I wrote a plugin for ActiveRecord to make usage of savepoints for enabling nested transactions functionality. Currently PostgreSQL and MySQL are implemented, though all databases that have some kind of savepoint mechanism could be supported.
This plugin can be reached at:
http://rubyforge.org/projects/arnesttransacts
Is this something to be considered as a patch to core ActiveRecord? I could try to make this patch, but won't bother if you think this should remain a plugin.
Summary of patch:
- rewrite ActiveRecord::ConnectionAdapters::DatabaseStatements.transaction to support possible savepoints
- add create_savepoint, rollback_to_savepoint and release_savepoint methods to ActiveRecord::ConnectionAdapters::DatabaseStatements
- implement those methods in actual connection adapters
- if create_savepoint returns nil, then savepoints are considered not supported and nested transactions are not used.
Best regards,
Laas Toom