Comments (4)
What you can do is to patch the subscriber and make it ignore queries from non-primary connection.
Do you think we still want to catch violations inside non-primary db transaction?
We should track connections separately using their ids.
It sounds like this statement means we track isolator_transactions
per connection? This makes sense to me. Although I think the remaining challenge is to identify and ignore sql of connection establishment. May be we can use it as the trigger for initializing a new isolator_transactions
.
Do you think using the db_config's host, adapter, and database would be a good connection identifier?
As for repro example
# This one is using the main db connection
class User < ActiveRecord::Base; end
# this is a different db that connects lazily
class Foo < ActiveRecord::Base
self.establish_connection db_config
end
# simply reference the class would increment the count
# Thread.current[:isolator_transactions] == 0
User.transaction do
# Thread.current[:isolator_transactions] == 1
Foo
# Thread.current[:isolator_transactions] == 2
end
# Thread.current[:isolator_transactions] == 1
I haven't tried but base on your comment may be the following is enough to throw off the count
User.transaction do
# Would `Thread.current[:isolator_transactions]` still increment even if the adapter and database are the same as the main connection?
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
end
from isolator.
Hey @mquan!
Finally, I found some time to invest into this feature. I took your PR and added some changes: #32
It would be great if you try this branch in your project (we're currently testing it internally, too).
from isolator.
@palkan awesome, I tested it out in our repo, and it fixes the problem. I also added a few offenses in both main and secondary dbs and it correctly identifies them. Great job
from isolator.
Do you have a recommended way for dealing with multiple connections?
Unfortunately, no. Isolator was designed for single connection per thread usage, and it's not that simple to make it work with multiple connections.
What you can do is to patch the subscriber and make it ignore queries from non-primary connection. Smth like this:
def self.subscribe!(event)
::ActiveSupport::Notifications.subscribe(event) do |_name, _start, _finish, _id, query|
# not sure about this one
next unless query[:connection] == ActiveRecord::Base.connection
Isolator.incr_transactions! if START_PATTERN.match?(query[:sql])
Isolator.decr_transactions! if FINISH_PATTERN.match?(query[:sql])
end
end
We should track connections separately using their ids. That would require some work.
Could you please provide a reproduction example or, much better, open a PR to Isolator with a test scenario for multiple connections? That would help us to fix this quicker.
from isolator.
Related Issues (20)
- KeyError: key not found: isolator_threshold HOT 3
- NoMethodError when building an exception for Faraday request HOT 3
- Idea: Add an RSpec matcher HOT 2
- Active Storage - Analyze Job - Always fails because of after_create_commit callback HOT 3
- Ignore http requests in models that save files to AWS using carrier_waive HOT 2
- Mailer adapter does not behave as expected HOT 10
- Being able to allow/ignore inside a specific transaction HOT 3
- Isolator behaves differently between rails 6.1.3.2 and 6.1.4 HOT 3
- Failure when saving attachment with action_text/active storage and AWS HOT 1
- Any reason to not run in production? HOT 1
- Crashes when used with `disable_ddl_transaction!` & `commit_db_transaction` HOT 2
- Add .isolator_ignore support HOT 3
- Error raised when query has invalid encoding HOT 3
- Stack level too deep HOT 1
- Subtransactions tracking/preventions HOT 2
- How to handle rescue of StandardError? HOT 3
- Sidekiq isolator doesn't work if "sidekiq/testing" is required only AFTER "isolator" HOT 2
- Sidekiq isolator doesn't work if "sidekiq/testing" is required only AFTER "isolator" HOT 2
- Mailer is not present in Isolator.adapters after initialize HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from isolator.