Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
transaction_isolation
Advanced tools
Set transaction isolation level in the ActiveRecord in a database agnostic way. Works with MySQL, PostgreSQL and SQLite as long as you are using new adapters mysql2, pg or sqlite3. Supports all ANSI SQL isolation levels: :serializable, :repeatable_read, :read_committed, :read_uncommitted.
See also transaction_retry gem for auto-retrying transactions on deadlocks and serialization errors.
ActiveRecord::Base.isolation_level( :serializable ) do
# your code
end
Add this to your Gemfile:
gem 'transaction_isolation'
Then run:
bundle
It works out of the box with Ruby on Rails.
If you have a standalone ActiveRecord-based project you'll need to call:
TransactionIsolation.apply_activerecord_patch # after connecting to the database
after connecting to the database. This is because ActiveRecord loads adapters lazilly and only then they can be patched.
This gem was initially developed for and successfully works in production at Kontomierz.pl - the finest Polish personal finance app.
When implementing a table-based job queue you should ensure that only one worker process can pop a particular job from the queue. Wrapping your code in a transaction is not enough because by default databases do not isolate transactions to the full extent, which leads to occasional phantom reads. It is therefore necessary to manually raise the transaction isolation level. The highest level of transaction isolation is called "serializable" and that's what we need here:
class QueuedJob < ActiveRecord::Base
# Job status
TODO = 1
PROCESSING = 2
DONE = 3
# Returns first job from the queue or nil if the queue is empty
def pop
QueuedJob.isolation_level( :serializable ) do
QueuedJob.transaction do
queued_job = find_by_status( TODO )
if queud_job
queued_job.update_attribute( :status, PROCESSING )
return queued_job
else
return nil
end
end
end
rescue ActiveRecord::TransactionIsolationConflict => e
logger.warn( e.message )
retry
end
end
Read more about isolation levels in Wikipedia
Run tests on the selected database (mysql2 by default):
db=mysql2 bundle exec rake test
db=postgresql bundle exec rake test
db=sqlite3 bundle exec rake test
Run tests on all supported databases:
./tests
Database configuration is hardcoded in test/db/db.rb; feel free to improve this and submit a pull request.
You should be very suspicious about any gem that monkey patches your stock Ruby on Rails framework.
This gem is carefully written to not be more intrusive than it needs to be:
Released under the MIT license. Copyright (C) 2012 Piotr 'Qertoip' Włodarek.
FAQs
Unknown package
We found that transaction_isolation demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.