Comments (5)
(original author here, but realize it's been 6 years since I wrote this, and I'm no longer the maintainer).
If you're not in a transaction, (depending on the rdbms, but certainly true with MySQL), consistency guarantees are simply not present. It's never a bad idea to be in a transaction, as autocommit (at least used to be) much slower than explicit transaction boundaries.
from with_advisory_lock.
Thanks for jumping on this one (SO op here). Can you elaborate a bit on “consistency guarantees are not present”? Does that mean that locks work only some of the time, or would it mean that in the context of Rails, acquiring db lock is not always guaranteed?
@allanohorn Thanks for the tip on ‘create_or_find_by’ - I ended up implementing something very similar that relies on catching unique constraint exceptions before I knew about ’create_or_find_by’
from with_advisory_lock.
With MySQL, if you're not in a txn, read results may not reflect the committed state of the database, depending on what engine you're using and how you've configured it.
from with_advisory_lock.
I have same problem with rspec tests. When I disable with_advisory_lock, it works as expected
from with_advisory_lock.
I had some problems but I've been able to resolve them by setting rspecs use_transactional_fixtures to false (as I should have done in the first place given I use database_cleaner).
I've written two tests to be able to see that w/o locks two threads run async and with locks they get synchronized. They work as expected, both letting database_cleaner use transactions or not.
RSpec.describe :with_advisory_lock, type: :model do
def thread_1(lock, result)
Thread.new do
ActiveRecord::Base.with_advisory_lock(lock) do
result << "thread_1"
sleep 0.01
result << "/thread_1"
end
end
end
def thread_2(lock, result)
Thread.new do
ActiveRecord::Base.with_advisory_lock(lock) do
result << "thread_2"
end
end
end
it "can run two different synchronized tasks in parallel" do
global_result = []
10.times do
result = []
[
thread_1("lock_1", result),
thread_2("lock_2", result)
].each(&:join)
global_result << result
end
expect(global_result).to include(%w[thread_1 thread_2 /thread_1])
end
it "can synchronize tasks with the same key" do
10.times do
result = []
[
thread_1("same_lock", result),
thread_2("same_lock", result)
].each(&:join)
# check thread_1 and /thread_1 are siblings
expect(result.index("thread_1")).to eq(result.index("/thread_1") - 1)
end
end
end
from with_advisory_lock.
Related Issues (20)
- Drop dependency on thread_safe HOT 3
- MySQL supports nested locks in >= 5.7.5
- Website in repo description goes to 404 HOT 1
- Build is failing in master branch HOT 1
- undefined method `with_advisory_lock' in Rails test environment HOT 6
- [Not a bug] Proper usage of with_advisory_lock
- Next planned release? HOT 4
- Rails 6 compatibility? HOT 2
- MiniTest warning about usage in tests HOT 1
- advisory locks with connection pooling (pgbouncer) HOT 3
- Connection pool causing locks to be taken when they shouldn't HOT 1
- Lots of duplicate/unnecessary calls to `select get_version()` HOT 1
- PG Transactional locks not working on Rails 5.2 HOT 9
- Is the unique column name still necessary or does AR do the right thing now? HOT 5
- [Question] RDSProxy Usage HOT 2
- RFC: Turn off ActiveRecord caching within lock blocks HOT 7
- Why not raise exception when the lock couldn't be acquired in the given timeout? HOT 4
- any plans on supporting rails 7 ? HOT 2
- Using `with_advisory_lock!` HOT 7
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 with_advisory_lock.