• Stars
    star
    116
  • Rank 303,894 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 13 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Retries database transaction on deadlock and transaction serialization errors. Supports MySQL, PostgreSQL and SQLite.

transaction_retry

Retries database transaction on deadlock and transaction serialization errors. Supports MySQL, PostgreSQL, and SQLite.

Example

The gem works automatically by rescuing ActiveRecord::TransactionIsolationConflict and retrying the transaction.

Installation

Add this to your Gemfile:

gem 'transaction_retry'

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:

TransactionRetry.apply_activerecord_patch     # after connecting to the database

after connecting to the database.

Database deadlock and serialization errors that are retried

MySQL

  • Deadlock found when trying to get lock
  • Lock wait timeout exceeded

PostgreSQL

  • deadlock detected
  • could not serialize access

SQLite

  • The database file is locked
  • A table in the database is locked
  • Database lock protocol error

Configuration

You can optionally configure transaction_retry gem in your config/initializers/transaction_retry.rb (or anywhere else):

TransactionRetry.max_retries = 3
TransactionRetry.wait_times = [0, 1, 2, 4, 8, 16, 32]   # seconds to sleep after retry n

Features

  • Supports MySQL, PostgreSQL, and SQLite (as long as you are using new drivers mysql2, pg, sqlite3).
  • Exponential sleep times between retries (0, 1, 2, 4 seconds).
  • Logs every retry as a warning.
  • Intentionally does not retry nested transactions.
  • Configurable number of retries and sleep time between them.
  • Use it in your Rails application or a standalone ActiveRecord-based project.

Testimonials

This gem was initially developed for and successfully works in production at Kontomierz.pl - the finest Polish personal finance app.

Requirements

  • ruby 1.9.2
  • activerecord 3.0.11+

Running tests

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.

How intrusive is this gem?

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:

  • wraps ActiveRecord::Base#transaction class method using alias_method to add new behaviour
  • introduces two new private class methods in ActiveRecord::Base (with names that should never collide)

License

Released under the MIT license. Copyright (C) 2012 Piotr 'Qertoip' Włodarek.

More Repositories

1

guru_watch

This toy application is an experiment with the idea of use case driven architecture and isolation from frameworks.
Ruby
166
star
2

transaction_isolation

Set transaction isolation level in the ActiveRecord in a database agnostic way.
Ruby
58
star
3

jeditable-datepicker

In place editing with a datepicker. Clean and simple. Based on jQuery UI and Jeditable.
JavaScript
26
star
4

friendly

HTML and XML parser for Elixir aiming at friendly API
Elixir
18
star
5

borg-helper

Scripts to run Borg Backup periodically on desktop Linux. Preconfigured for security, eficiency, and completeness.
Shell
11
star
6

lock-unless-smartphone

Auto lock screen if your smartphone is not connected via USB
Shell
10
star
7

calc

Calculator (mathematical expressions evaluator) library for Ruby
Ruby
9
star
8

istext

A library to tell apart binary and text files using proven heuristics.
Clojure
7
star
9

python-package-size

Learn after-install weight of any Python package including its dependencies.
Python
5
star
10

battery

Ruby API to your laptop's battery.
Ruby
4
star
11

Transaction-Isolation-in-Ruby-on-Rails-applications

My presentation at WRUG meeting on 2012-03-20
Shell
3
star
12

react-form-logic

React library handling form serialization and validation
JavaScript
3
star
13

cleancr

Finds and optionally removes carriage return characters from your project text files (also known as Ctrl-M, ^M, \r).
Clojure
3
star
14

javascript-najlepsze-praktyki

JavaScript - garść najlepszych praktyk - prezentacja
JavaScript
3
star
15

Prezentacja-Software-Craftsmanship

Od studenta do profesjonalisty
Shell
1
star
16

przyszlosc-bankowosci-elektronicznej-pfm

Przyszłość bankowości elektronicznej. Personal Finance Management (PFM). Prezentacja wygłoszona na zjeździe Związku Banków Polskich w Ostródzie 12 maja 2011.
1
star