Repository is archived
No commit activity in last 3 years
No release in over 3 years
Adds advisory locks that are automatically released at the end of a transaction.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.10
~> 10.0
~> 3.3

Runtime

 Project Readme

build status

TransactionalLock

⚠️ Deprecation notice

This gem is not being maintained anymore. We migrated to https://github.com/ClosureTree/with_advisory_lock and suggest you do as well.

TransactionalLock is providing access to database advisory locks that will be automatically released upon the end of a transaction (COMMIT or ROLLBACK).

As of now this gem only targets MySQL databases, where such locks are not available, thus they are emulated by ensuring that the outmost ActiveRecord transaction will release all* locks.

* "all" refers to "one", because in MySQL a session can only hold a single advisory lock.

Installation

Add this line to your application's Gemfile:

gem 'transactional_lock'

And then execute:

$ bundle

Or install it yourself as:

$ gem install transactional_lock

Usage

While inside a transaction acquire your lock and you can be sure, that it will be released at the end of the transaction:

ActiveRecord::Base.transaction do
  TransactionalLock::AdvisoryLock.new('your_lock').acquire
  # do your work
end
# the lock has been released at this point

It will also work for nested AR transactions (that do not really map to your SQL COMMIT or ROLLBACK):

ActiveRecord::Base.transaction do
  ActiveRecord::Base.transaction do
    TransactionalLock::AdvisoryLock.new('your_lock').acquire
  end

  # lock is still acquired here...
end # actual SQL COMMIT
# the lock has been released at this point

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/finnlabs/transactional_lock. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.