You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The self.find_or_create_all_with_like_by_name seems to implement something that resembles concurrency safe creation of records: if many records are created concurrently and one of them ends-up being unique transaction is being reversed.
This implementation is sketchy as it:
Does not adhere to materialised nested transactions that might use savepoints breaking transaction boundaries
Does not adhere to the connection used by the model
1. Materialised nested transactions
The code can run in many level of transactions which will make ROLLBACK to revert the top-most which is incorrect beaviour.
transaction(joinable: false)do# top-leveltransaction(joinable: false)do# savepointActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name('AWESOME')# the ROLLBACK will revert to top-level and break `savepoint`endend
2. Does not adhere to the connection used by the model
The Rails 6 allows to support many databases, and allows to instruct indivdual models to use a different database. The usage of ActiveRecord::Base. is then deemed no longer safe as it might be pointing to wrong database, and rollback might be executed on a wrong database.
The
self.find_or_create_all_with_like_by_name
seems to implement something that resembles concurrency safe creation of records: if many records are created concurrently and one of them ends-up being unique transaction is being reversed.This implementation is sketchy as it:
1. Materialised nested transactions
The code can run in many level of transactions which will make
ROLLBACK
to revert the top-most which is incorrect beaviour.2. Does not adhere to the connection used by the model
The Rails 6 allows to support many databases, and allows to instruct indivdual models to use a different database. The usage of
ActiveRecord::Base.
is then deemed no longer safe as it might be pointing to wrong database, and rollback might be executed on a wrong database.The
ActsAsTaggableOn::Tag.connection
will be different thanActiveRecord::Base.connection
Ref.: https://guides.rubyonrails.org/active_record_multiple_databases.html
The text was updated successfully, but these errors were encountered: