database_cleaner-active_record icon indicating copy to clipboard operation
database_cleaner-active_record copied to clipboard

DatabaseCleaner improperly starts and ends transactions for ActiveRecord

Open ryanong opened this issue 10 years ago • 2 comments

If there is more than one connection in the connection pool DatabaseCleaner will only begin the transaction on one of the connections. https://github.com/rails/rails/blob/master/activerecord/lib/active_record/fixtures.rb#L979

Any ActiveRecord call afterwards will use the next connection in the pool which does not have a transaction.

When DatabaseCleaner closes the transaction, it will only do so on the next active connection. Thus leaving a connection with an active transaction in the connection pool which can deadlock when doing a deletion cleanup or any table modifications.

DatabaseCleaner should start and end transactions the same way ActiveRecord does by collecting all available connections and starting transactions on all of them.

start: https://github.com/rails/rails/blob/06a9e18e646845ded0f0a46a14c1eb6b3a14e55a/activerecord/lib/active_record/fixtures.rb#L949 end: https://github.com/rails/rails/blob/06a9e18e646845ded0f0a46a14c1eb6b3a14e55a/activerecord/lib/active_record/fixtures.rb#L967

ryanong avatar Sep 19 '14 13:09 ryanong

@ryanong the code line numbers are off because master changed. Can you reference line numbers by some commit please?

lulalala avatar Sep 30 '14 09:09 lulalala

@lulalala fixed

ryanong avatar Sep 30 '14 19:09 ryanong