transaction_isolation 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +6 -0
- data/Gemfile +24 -0
- data/LICENSE +19 -0
- data/README.md +95 -0
- data/Rakefile +11 -0
- data/d +1 -0
- data/lib/transaction_isolation.rb +30 -0
- data/lib/transaction_isolation/active_record/base.rb +13 -0
- data/lib/transaction_isolation/active_record/connection_adapters/abstract_adapter.rb +32 -0
- data/lib/transaction_isolation/active_record/connection_adapters/mysql2_adapter.rb +77 -0
- data/lib/transaction_isolation/active_record/connection_adapters/postgresql_adapter.rb +77 -0
- data/lib/transaction_isolation/active_record/connection_adapters/sqlite3_adapter.rb +76 -0
- data/lib/transaction_isolation/active_record/errors.rb +10 -0
- data/lib/transaction_isolation/version.rb +3 -0
- data/test/db/all.rb +4 -0
- data/test/db/db.rb +35 -0
- data/test/db/migrations.rb +20 -0
- data/test/db/queued_job.rb +2 -0
- data/test/integration/active_record/base/isolation_level_test.rb +23 -0
- data/test/integration/active_record/connection_adapters/any_adapter/current_isolation_level_test.rb +33 -0
- data/test/integration/active_record/connection_adapters/any_adapter/current_vendor_isolation_level_test.rb +33 -0
- data/test/integration/active_record/connection_adapters/any_adapter/isolation_level_test.rb +69 -0
- data/test/integration/active_record/connection_adapters/any_adapter/supports_isolation_levels_test.rb +23 -0
- data/test/integration/active_record/connection_adapters/any_adapter/translate_exception_test.rb +52 -0
- data/test/library_setup.rb +20 -0
- data/test/log/.gitkeep +0 -0
- data/test/test_console.rb +11 -0
- data/test/test_helper.rb +12 -0
- data/test/test_runner.rb +4 -0
- data/tests +6 -0
- data/transaction-isolation.gemspec +22 -0
- metadata +95 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in kontolib.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group :test do
|
7
|
+
# Use the gem instead of a dated version bundled with Ruby
|
8
|
+
gem 'minitest', '2.8.1'
|
9
|
+
|
10
|
+
gem 'activerecord', "3.1.3"
|
11
|
+
|
12
|
+
gem 'simplecov', :require => false
|
13
|
+
|
14
|
+
gem 'mysql2'
|
15
|
+
gem 'pg'
|
16
|
+
gem 'sqlite3'
|
17
|
+
end
|
18
|
+
|
19
|
+
group :development do
|
20
|
+
gem 'rake'
|
21
|
+
# enhance irb
|
22
|
+
gem 'awesome_print', :require => false
|
23
|
+
gem 'pry', :require => false
|
24
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (C) 2012 Piotr 'Qertoip' Włodarek
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
|
+
so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
# transaction_isolation
|
2
|
+
|
3
|
+
Adds support for setting transaction isolation level in ActiveRecord in a database agnostic way.
|
4
|
+
|
5
|
+
## Example
|
6
|
+
|
7
|
+
ActiveRecord::Base.isolation_level( :serializable ) do
|
8
|
+
# your code
|
9
|
+
end
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this to your Gemfile:
|
14
|
+
|
15
|
+
gem 'transaction_isolation'
|
16
|
+
|
17
|
+
Then run:
|
18
|
+
|
19
|
+
bundle
|
20
|
+
|
21
|
+
## Features
|
22
|
+
|
23
|
+
* Setting transaction isolation level (:read_uncommitted, :read_committed, :repeatable_read, :serializable)
|
24
|
+
* Auto-reverting to the original isolation level after the block
|
25
|
+
* Database agnostic
|
26
|
+
* MySQL2, PostgreSQL and SQLite3 database connection adapters supported
|
27
|
+
* Exception translation. All deadlocks and serialization errors are wrapped in a ActiveRecord::TransactionIsolationConflict exception
|
28
|
+
* Use it in your Rails application or a standalone ActiveRecord-based project
|
29
|
+
|
30
|
+
## Real world example
|
31
|
+
|
32
|
+
When implementing a table-based job queue you should ensure that only one worker process can pop a particular job from the queue.
|
33
|
+
Wrapping your code in a transaction is not enough because by default databases do not isolate transactions to the full extent,
|
34
|
+
which leads to occasional phantom reads. It is therefore necessary to manually raise the transaction isolation level.
|
35
|
+
The highest level of transaction isolation is called "serializable".
|
36
|
+
|
37
|
+
[Read about isolation levels in Wikipedia](http://tinyurl.com/nrqjbb)
|
38
|
+
|
39
|
+
class QueuedJob < ActiveRecord::Base
|
40
|
+
|
41
|
+
# Job status
|
42
|
+
TODO = 1
|
43
|
+
PROCESSING = 2
|
44
|
+
DONE = 3
|
45
|
+
|
46
|
+
# Returns first job from the queue or nil if the queue is empty
|
47
|
+
def pop
|
48
|
+
QueuedJob.isolation_level( :serializable ) do
|
49
|
+
QueuedJob.transaction do
|
50
|
+
queued_job = find_by_status( TODO )
|
51
|
+
if queud_job
|
52
|
+
queued_job.update_attribute( :status, PROCESSING )
|
53
|
+
return queued_job
|
54
|
+
else
|
55
|
+
return nil
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
## Requirements
|
64
|
+
|
65
|
+
* Ruby 1.9
|
66
|
+
* ActiveRecord 3.1+
|
67
|
+
|
68
|
+
## Running tests
|
69
|
+
|
70
|
+
Run tests on the selected database (mysql2 by default):
|
71
|
+
|
72
|
+
db=mysql2 bundle exec rake test
|
73
|
+
db=postgresql bundle exec rake test
|
74
|
+
db=sqlite3 bundle exec rake test
|
75
|
+
|
76
|
+
Run tests on all supported databases:
|
77
|
+
|
78
|
+
./tests
|
79
|
+
|
80
|
+
Database configuration is hardcoded in test/db/db.rb; feel free to improve this and submit a pull request.
|
81
|
+
|
82
|
+
## How intrusive is this gem?
|
83
|
+
|
84
|
+
You should be very suspicious about any gem that monkey patches your stock Ruby on Rails framework.
|
85
|
+
|
86
|
+
This gem is carefully written to not be more intrusive than it needs to be:
|
87
|
+
|
88
|
+
* introduces several new methods to Mysql2Adapter, PostgreSQLAdapter, SQLite3Adapter; names are carefully taken to not collide with future changes
|
89
|
+
* wraps #translate_exception method using alias_method_chain to add new translation
|
90
|
+
* introduces new class ActiveRecord::TransactionIsolationConflict in the ActiveRecord module
|
91
|
+
* introduces new convenience method ActiveRecord::Base.isolation_level akin to ActiveRecord::Base.transaction
|
92
|
+
|
93
|
+
## License
|
94
|
+
|
95
|
+
Released under the MIT license. Copyright (C) 2012 Piotr 'Qertoip' Włodarek.
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative 'transaction_isolation/version'
|
2
|
+
|
3
|
+
module TransactionIsolation
|
4
|
+
|
5
|
+
# Must be called after ActiveRecord established a connection.
|
6
|
+
# Only then we know which connection adapter is actually loaded and can be enhanced.
|
7
|
+
# Please note ActiveRecord does not load unused adapters.
|
8
|
+
def self.apply_activerecord_patch
|
9
|
+
require_relative 'transaction_isolation/active_record/errors'
|
10
|
+
require_relative 'transaction_isolation/active_record/base'
|
11
|
+
require_relative 'transaction_isolation/active_record/connection_adapters/abstract_adapter'
|
12
|
+
require_relative 'transaction_isolation/active_record/connection_adapters/mysql2_adapter'
|
13
|
+
require_relative 'transaction_isolation/active_record/connection_adapters/postgresql_adapter'
|
14
|
+
require_relative 'transaction_isolation/active_record/connection_adapters/sqlite3_adapter'
|
15
|
+
end
|
16
|
+
|
17
|
+
if defined?( ::Rails )
|
18
|
+
# Setup applying the patch after Rails is initialized.
|
19
|
+
class Railtie < ::Rails::Railtie
|
20
|
+
config.after_initialize do
|
21
|
+
TransactionIsolation.apply_activerecord_patch
|
22
|
+
end
|
23
|
+
end
|
24
|
+
else
|
25
|
+
# Without Rails we can apply the patch outright. It is programmer responsibility
|
26
|
+
# to require the transaction_isolation gem *after* connecting to the database.
|
27
|
+
TransactionIsolation.apply_activerecord_patch
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'active_record/base'
|
2
|
+
|
3
|
+
module TransactionIsolation
|
4
|
+
module ActiveRecord
|
5
|
+
module Base
|
6
|
+
def isolation_level( isolation_level, &block )
|
7
|
+
connection.isolation_level( isolation_level, &block )
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveRecord::Base.extend( TransactionIsolation::ActiveRecord::Base )
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'active_record/connection_adapters/abstract_adapter'
|
2
|
+
|
3
|
+
module TransactionIsolation
|
4
|
+
module ActiveRecord
|
5
|
+
module ConnectionAdapters # :nodoc:
|
6
|
+
module AbstractAdapter
|
7
|
+
|
8
|
+
VALID_ISOLATION_LEVELS = [:read_uncommitted, :read_committed, :repeatable_read, :serializable]
|
9
|
+
|
10
|
+
# If true, #isolation_level(level) method is available
|
11
|
+
def supports_isolation_levels?
|
12
|
+
false
|
13
|
+
end
|
14
|
+
|
15
|
+
def isolation_level( level )
|
16
|
+
raise NotImplementedError
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def validate_isolation_level( isolation_level )
|
22
|
+
unless VALID_ISOLATION_LEVELS.include?( isolation_level )
|
23
|
+
raise ArgumentError, "Invalid isolation level '#{isolation_level}'. Supported levels include #{VALID_ISOLATION_LEVELS.join( ', ' )}."
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
ActiveRecord::ConnectionAdapters::AbstractAdapter.send( :include, TransactionIsolation::ActiveRecord::ConnectionAdapters::AbstractAdapter )
|
@@ -0,0 +1,77 @@
|
|
1
|
+
if defined?( ActiveRecord::ConnectionAdapters::Mysql2Adapter )
|
2
|
+
|
3
|
+
module TransactionIsolation
|
4
|
+
module ActiveRecord
|
5
|
+
module ConnectionAdapters # :nodoc:
|
6
|
+
module Mysql2Adapter
|
7
|
+
|
8
|
+
def self.included( base )
|
9
|
+
base.class_eval do
|
10
|
+
alias_method :translate_exception_without_transaction_isolation_conflict, :translate_exception
|
11
|
+
alias_method :translate_exception, :translate_exception_with_transaction_isolation_conflict
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def supports_isolation_levels?
|
16
|
+
true
|
17
|
+
end
|
18
|
+
|
19
|
+
VENDOR_ISOLATION_LEVEL = {
|
20
|
+
:read_uncommitted => 'READ UNCOMMITTED',
|
21
|
+
:read_committed => 'READ COMMITTED',
|
22
|
+
:repeatable_read => 'REPEATABLE READ',
|
23
|
+
:serializable => 'SERIALIZABLE'
|
24
|
+
}
|
25
|
+
|
26
|
+
ANSI_ISOLATION_LEVEL = {
|
27
|
+
'READ UNCOMMITTED' => :read_uncommitted,
|
28
|
+
'READ COMMITTED' => :read_committed,
|
29
|
+
'REPEATABLE READ' => :repeatable_read,
|
30
|
+
'SERIALIZABLE' => :serializable
|
31
|
+
}
|
32
|
+
|
33
|
+
def current_isolation_level
|
34
|
+
ANSI_ISOLATION_LEVEL[current_vendor_isolation_level]
|
35
|
+
end
|
36
|
+
|
37
|
+
def current_vendor_isolation_level
|
38
|
+
select_value( "SELECT @@session.tx_isolation" ).gsub( '-', ' ' )
|
39
|
+
end
|
40
|
+
|
41
|
+
def isolation_level( level )
|
42
|
+
validate_isolation_level( level )
|
43
|
+
|
44
|
+
original_vendor_isolation_level = current_vendor_isolation_level if block_given?
|
45
|
+
|
46
|
+
execute( "SET SESSION TRANSACTION ISOLATION LEVEL #{VENDOR_ISOLATION_LEVEL[level]}" )
|
47
|
+
|
48
|
+
begin
|
49
|
+
yield
|
50
|
+
ensure
|
51
|
+
execute "SET SESSION TRANSACTION ISOLATION LEVEL #{original_vendor_isolation_level}"
|
52
|
+
end if block_given?
|
53
|
+
end
|
54
|
+
|
55
|
+
def translate_exception_with_transaction_isolation_conflict( exception, message )
|
56
|
+
if isolation_conflict?( exception )
|
57
|
+
::ActiveRecord::TransactionIsolationConflict.new( "Transaction isolation conflict detected: #{exception.message}", exception )
|
58
|
+
else
|
59
|
+
translate_exception_without_transaction_isolation_conflict( exception, message )
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def isolation_conflict?( exception )
|
64
|
+
[ "Deadlock found when trying to get lock",
|
65
|
+
"Lock wait timeout exceeded"].any? do |error_message|
|
66
|
+
exception.message =~ /#{Regexp.escape( error_message )}/i
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
ActiveRecord::ConnectionAdapters::Mysql2Adapter.send( :include, TransactionIsolation::ActiveRecord::ConnectionAdapters::Mysql2Adapter )
|
76
|
+
|
77
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
if defined?( ActiveRecord::ConnectionAdapters::PostgreSQLAdapter )
|
2
|
+
|
3
|
+
module TransactionIsolation
|
4
|
+
module ActiveRecord
|
5
|
+
module ConnectionAdapters # :nodoc:
|
6
|
+
module PostgreSQLAdapter
|
7
|
+
|
8
|
+
def self.included( base )
|
9
|
+
base.class_eval do
|
10
|
+
alias_method :translate_exception_without_transaction_isolation_conflict, :translate_exception
|
11
|
+
alias_method :translate_exception, :translate_exception_with_transaction_isolation_conflict
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def supports_isolation_levels?
|
16
|
+
true
|
17
|
+
end
|
18
|
+
|
19
|
+
VENDOR_ISOLATION_LEVEL = {
|
20
|
+
:read_uncommitted => 'READ UNCOMMITTED',
|
21
|
+
:read_committed => 'READ COMMITTED',
|
22
|
+
:repeatable_read => 'REPEATABLE READ',
|
23
|
+
:serializable => 'SERIALIZABLE'
|
24
|
+
}
|
25
|
+
|
26
|
+
ANSI_ISOLATION_LEVEL = {
|
27
|
+
'READ UNCOMMITTED' => :read_uncommitted,
|
28
|
+
'READ COMMITTED' => :read_committed,
|
29
|
+
'REPEATABLE READ' => :repeatable_read,
|
30
|
+
'SERIALIZABLE' => :serializable
|
31
|
+
}
|
32
|
+
|
33
|
+
def current_isolation_level
|
34
|
+
ANSI_ISOLATION_LEVEL[current_vendor_isolation_level]
|
35
|
+
end
|
36
|
+
|
37
|
+
def current_vendor_isolation_level
|
38
|
+
select_value( "SHOW TRANSACTION ISOLATION LEVEL" ).upcase
|
39
|
+
end
|
40
|
+
|
41
|
+
def isolation_level( level )
|
42
|
+
validate_isolation_level( level )
|
43
|
+
|
44
|
+
original_vendor_isolation_level = current_vendor_isolation_level if block_given?
|
45
|
+
|
46
|
+
execute "SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL #{VENDOR_ISOLATION_LEVEL[level]}"
|
47
|
+
|
48
|
+
begin
|
49
|
+
yield
|
50
|
+
ensure
|
51
|
+
execute "SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL #{original_vendor_isolation_level}"
|
52
|
+
end if block_given?
|
53
|
+
end
|
54
|
+
|
55
|
+
def translate_exception_with_transaction_isolation_conflict( exception, message )
|
56
|
+
if isolation_conflict?( exception )
|
57
|
+
::ActiveRecord::TransactionIsolationConflict.new( "Transaction isolation conflict detected: #{exception.message}", exception )
|
58
|
+
else
|
59
|
+
translate_exception_without_transaction_isolation_conflict( exception, message )
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def isolation_conflict?( exception )
|
64
|
+
[ "deadlock detected",
|
65
|
+
"could not serialize access" ].any? do |error_message|
|
66
|
+
exception.message =~ /#{Regexp.escape( error_message )}/i
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send( :include, TransactionIsolation::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter )
|
76
|
+
|
77
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
if defined?( ActiveRecord::ConnectionAdapters::SQLiteAdapter )
|
2
|
+
|
3
|
+
module TransactionIsolation
|
4
|
+
module ActiveRecord
|
5
|
+
module ConnectionAdapters # :nodoc:
|
6
|
+
module SQLite3Adapter
|
7
|
+
|
8
|
+
def self.included( base )
|
9
|
+
base.class_eval do
|
10
|
+
alias_method :translate_exception_without_transaction_isolation_conflict, :translate_exception
|
11
|
+
alias_method :translate_exception, :translate_exception_with_transaction_isolation_conflict
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def supports_isolation_levels?
|
16
|
+
true
|
17
|
+
end
|
18
|
+
|
19
|
+
VENDOR_ISOLATION_LEVEL = {
|
20
|
+
:read_uncommitted => 'read_uncommitted = 1',
|
21
|
+
:read_committed => 'read_uncommitted = 0',
|
22
|
+
:repeatable_read => 'read_uncommitted = 0',
|
23
|
+
:serializable => 'read_uncommitted = 0'
|
24
|
+
}
|
25
|
+
|
26
|
+
ANSI_ISOLATION_LEVEL = {
|
27
|
+
'read_uncommitted = 1' => :read_uncommitted,
|
28
|
+
'read_uncommitted = 0' => :serializable
|
29
|
+
}
|
30
|
+
|
31
|
+
def current_isolation_level
|
32
|
+
ANSI_ISOLATION_LEVEL[current_vendor_isolation_level]
|
33
|
+
end
|
34
|
+
|
35
|
+
def current_vendor_isolation_level
|
36
|
+
"read_uncommitted = #{select_value( "PRAGMA read_uncommitted" )}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def isolation_level( level )
|
40
|
+
validate_isolation_level( level )
|
41
|
+
|
42
|
+
original_vendor_isolation_level = current_vendor_isolation_level if block_given?
|
43
|
+
|
44
|
+
execute "PRAGMA #{VENDOR_ISOLATION_LEVEL[level]}"
|
45
|
+
|
46
|
+
begin
|
47
|
+
yield
|
48
|
+
ensure
|
49
|
+
execute "PRAGMA #{original_vendor_isolation_level}"
|
50
|
+
end if block_given?
|
51
|
+
end
|
52
|
+
|
53
|
+
def translate_exception_with_transaction_isolation_conflict( exception, message )
|
54
|
+
if isolation_conflict?( exception )
|
55
|
+
::ActiveRecord::TransactionIsolationConflict.new( "Transaction isolation conflict detected: #{exception.message}", exception )
|
56
|
+
else
|
57
|
+
translate_exception_without_transaction_isolation_conflict( exception, message )
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# http://www.sqlite.org/c3ref/c_abort.html
|
62
|
+
def isolation_conflict?( exception )
|
63
|
+
[ "The database file is locked",
|
64
|
+
"A table in the database is locked",
|
65
|
+
"Database lock protocol error"].any? do |error_message|
|
66
|
+
exception.message =~ /#{Regexp.escape( error_message )}/i
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
ActiveRecord::ConnectionAdapters::SQLite3Adapter.send( :include, TransactionIsolation::ActiveRecord::ConnectionAdapters::SQLite3Adapter )
|
75
|
+
|
76
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'active_record/errors'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
# This exception represents both deadlocks and serialization conflicts.
|
5
|
+
# Deadlocks happen when db engine is using lock-based concurrency control.
|
6
|
+
# Serialization conflicts happen when db engine is using multi-version concurrency control.
|
7
|
+
# Often db engines combine both approaches and thus generate both types of errors.
|
8
|
+
|
9
|
+
class TransactionIsolationConflict < ::ActiveRecord::WrappedDatabaseException; end
|
10
|
+
end
|
data/test/db/all.rb
ADDED
data/test/db/db.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
module TransactionIsolation
|
4
|
+
module Test
|
5
|
+
module Db
|
6
|
+
|
7
|
+
def self.connect_to_mysql2
|
8
|
+
::ActiveRecord::Base.establish_connection(
|
9
|
+
:adapter => "mysql2",
|
10
|
+
:database => "transaction_isolation_test", #database_filepath
|
11
|
+
:user => 'root',
|
12
|
+
:password => ''
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.connect_to_postgresql
|
17
|
+
::ActiveRecord::Base.establish_connection(
|
18
|
+
:adapter => "postgresql",
|
19
|
+
:database => "transaction_isolation_test", #database_filepath
|
20
|
+
:user => 'qertoip',
|
21
|
+
:password => 'test123'
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.connect_to_sqlite3
|
26
|
+
ActiveRecord::Base.establish_connection(
|
27
|
+
:adapter => "sqlite3",
|
28
|
+
:database => ":memory:",
|
29
|
+
:verbosity => "silent"
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module TransactionIsolation
|
2
|
+
module Test
|
3
|
+
module Migrations
|
4
|
+
|
5
|
+
def self.run!
|
6
|
+
c = ::ActiveRecord::Base.connection
|
7
|
+
|
8
|
+
# Queued Jobs
|
9
|
+
|
10
|
+
c.create_table "queued_jobs", :force => true do |t|
|
11
|
+
t.text "job", :null => false
|
12
|
+
t.integer "status", :default => 0, :null => false
|
13
|
+
t.timestamps
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class ActiveRecordTest < MiniTest::Unit::TestCase
|
6
|
+
|
7
|
+
class BaseTest < MiniTest::Unit::TestCase
|
8
|
+
|
9
|
+
class IsolationLevelTest < MiniTest::Unit::TestCase
|
10
|
+
|
11
|
+
def test_wraps_connection_isolation_level
|
12
|
+
ActiveRecord::Base.isolation_level( :serializable ) do
|
13
|
+
ActiveRecord::Base.transaction do
|
14
|
+
assert_equal( :serializable, ActiveRecord::Base.connection.current_isolation_level )
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
data/test/integration/active_record/connection_adapters/any_adapter/current_isolation_level_test.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class ActiveRecordTest < MiniTest::Unit::TestCase
|
6
|
+
|
7
|
+
class ConnectionAdaptersTest < MiniTest::Unit::TestCase
|
8
|
+
|
9
|
+
class AnyAdapterTest < MiniTest::Unit::TestCase
|
10
|
+
|
11
|
+
class CurrentIsolationLevelTest < MiniTest::Unit::TestCase
|
12
|
+
|
13
|
+
def test_returns_correct_default_isolation_level
|
14
|
+
if defined?( ActiveRecord::ConnectionAdapters::Mysql2Adapter )
|
15
|
+
assert_equal( :repeatable_read, ActiveRecord::Base.connection.current_isolation_level )
|
16
|
+
end
|
17
|
+
|
18
|
+
if defined?( ActiveRecord::ConnectionAdapters::PostgreSQLAdapter )
|
19
|
+
assert_equal( :read_committed, ActiveRecord::Base.connection.current_isolation_level )
|
20
|
+
end
|
21
|
+
|
22
|
+
if defined?( ActiveRecord::ConnectionAdapters::SQLite3Adapter )
|
23
|
+
assert_equal( :serializable, ActiveRecord::Base.connection.current_isolation_level )
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class ActiveRecordTest < MiniTest::Unit::TestCase
|
6
|
+
|
7
|
+
class ConnectionAdaptersTest < MiniTest::Unit::TestCase
|
8
|
+
|
9
|
+
class AnyAdapterTest < MiniTest::Unit::TestCase
|
10
|
+
|
11
|
+
class CurrentVendorIsolationLevelTest < MiniTest::Unit::TestCase
|
12
|
+
|
13
|
+
def test_returns_correct_default_vendor_isolation_level
|
14
|
+
if defined?( ActiveRecord::ConnectionAdapters::Mysql2Adapter )
|
15
|
+
assert_equal( 'REPEATABLE READ', ActiveRecord::Base.connection.current_vendor_isolation_level )
|
16
|
+
end
|
17
|
+
|
18
|
+
if defined?( ActiveRecord::ConnectionAdapters::PostgreSQLAdapter )
|
19
|
+
assert_equal( 'READ COMMITTED', ActiveRecord::Base.connection.current_vendor_isolation_level )
|
20
|
+
end
|
21
|
+
|
22
|
+
if defined?( ActiveRecord::ConnectionAdapters::SQLite3Adapter )
|
23
|
+
assert_equal( 'read_uncommitted = 0', ActiveRecord::Base.connection.current_vendor_isolation_level )
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class ActiveRecordTest < MiniTest::Unit::TestCase
|
6
|
+
|
7
|
+
class ConnectionAdaptersTest < MiniTest::Unit::TestCase
|
8
|
+
|
9
|
+
class AnyAdapterTest < MiniTest::Unit::TestCase
|
10
|
+
|
11
|
+
class IsolationLevelTest < MiniTest::Unit::TestCase
|
12
|
+
|
13
|
+
def test_without_a_block
|
14
|
+
original_isolation_level = ActiveRecord::Base.connection.current_isolation_level
|
15
|
+
|
16
|
+
ActiveRecord::Base.connection.isolation_level( :read_uncommitted )
|
17
|
+
assert_equal( :read_uncommitted, ActiveRecord::Base.connection.current_isolation_level )
|
18
|
+
|
19
|
+
ActiveRecord::Base.connection.isolation_level( original_isolation_level )
|
20
|
+
assert_equal( original_isolation_level, ActiveRecord::Base.connection.current_isolation_level )
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_with_a_block
|
24
|
+
original_isolation_level = ActiveRecord::Base.connection.current_isolation_level
|
25
|
+
refute_equal( :read_uncommitted, original_isolation_level )
|
26
|
+
|
27
|
+
ActiveRecord::Base.connection.isolation_level( :read_uncommitted ) do
|
28
|
+
assert_equal( :read_uncommitted, ActiveRecord::Base.connection.current_isolation_level )
|
29
|
+
ActiveRecord::Base.transaction do
|
30
|
+
assert_equal( :read_uncommitted, ActiveRecord::Base.connection.current_isolation_level )
|
31
|
+
QueuedJob.count
|
32
|
+
QueuedJob.first
|
33
|
+
assert_equal( :read_uncommitted, ActiveRecord::Base.connection.current_isolation_level )
|
34
|
+
end
|
35
|
+
assert_equal( :read_uncommitted, ActiveRecord::Base.connection.current_isolation_level )
|
36
|
+
end
|
37
|
+
|
38
|
+
assert_equal( original_isolation_level, ActiveRecord::Base.connection.current_isolation_level )
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_with_all_possible_ansi_levels
|
42
|
+
[:read_uncommitted, :read_committed, :repeatable_read, :serializable].each do |ansi_level|
|
43
|
+
|
44
|
+
QueuedJob.isolation_level( ansi_level ) do
|
45
|
+
|
46
|
+
# Some typical usage
|
47
|
+
QueuedJob.transaction do
|
48
|
+
QueuedJob.create!( :job => 'is fun' )
|
49
|
+
assert_equal( 1, QueuedJob.count )
|
50
|
+
raise ActiveRecord::Rollback
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_with_invalid_isolation_level
|
58
|
+
assert_raises( ArgumentError ) do
|
59
|
+
QueuedJob.isolation_level( :dupa )
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class ActiveRecordTest < MiniTest::Unit::TestCase
|
6
|
+
|
7
|
+
class ConnectionAdaptersTest < MiniTest::Unit::TestCase
|
8
|
+
|
9
|
+
class AnyAdapterTest < MiniTest::Unit::TestCase
|
10
|
+
|
11
|
+
class SupportsIsolationLevelsTest < MiniTest::Unit::TestCase
|
12
|
+
|
13
|
+
def test_returns_true
|
14
|
+
assert( ActiveRecord::Base.connection.supports_isolation_levels? )
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
data/test/integration/active_record/connection_adapters/any_adapter/translate_exception_test.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class ActiveRecordTest < MiniTest::Unit::TestCase
|
6
|
+
|
7
|
+
class ConnectionAdaptersTest < MiniTest::Unit::TestCase
|
8
|
+
|
9
|
+
class AnyAdapterTest < MiniTest::Unit::TestCase
|
10
|
+
|
11
|
+
class TranslateExceptionTest < MiniTest::Unit::TestCase
|
12
|
+
|
13
|
+
def test_does_not_break_existing_translation
|
14
|
+
assert_raises( ActiveRecord::StatementInvalid ) do
|
15
|
+
ActiveRecord::Base.connection.execute( "WE LIVE IN THE MOST EXCITING TIMES EVER" )
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_translates_low_level_exceptions_to_transaction_isolation_level
|
20
|
+
if defined?( ActiveRecord::ConnectionAdapters::Mysql2Adapter )
|
21
|
+
message = "Deadlock found when trying to get lock"
|
22
|
+
translated_exception = ActiveRecord::Base.connection.send( :translate_exception, Mysql2::Error.new( message ), message )
|
23
|
+
assert_equal( ActiveRecord::TransactionIsolationConflict, translated_exception.class )
|
24
|
+
end
|
25
|
+
|
26
|
+
if defined?( ActiveRecord::ConnectionAdapters::PostgreSQLAdapter )
|
27
|
+
message = "deadlock detected"
|
28
|
+
translated_exception = ActiveRecord::Base.connection.send( :translate_exception, PGError.new( message ), message )
|
29
|
+
assert_equal( ActiveRecord::TransactionIsolationConflict, translated_exception.class )
|
30
|
+
end
|
31
|
+
|
32
|
+
if defined?( ActiveRecord::ConnectionAdapters::SQLite3Adapter )
|
33
|
+
message = "The database file is locked"
|
34
|
+
translated_exception = ActiveRecord::Base.connection.send( :translate_exception, StandardError.new( message ), message )
|
35
|
+
assert_equal( ActiveRecord::TransactionIsolationConflict, translated_exception.class )
|
36
|
+
end
|
37
|
+
|
38
|
+
# assert_equal( :read_committed, ActiveRecord::Base.connection.current_isolation_level )
|
39
|
+
#end
|
40
|
+
#
|
41
|
+
#if defined?( ActiveRecord::ConnectionAdapters::SQLite3Adapter )
|
42
|
+
# assert_equal( :serializable, ActiveRecord::Base.connection.current_isolation_level )
|
43
|
+
#end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# Prepares application to be tested (requires files, connects to db, resets schema and data, applies patches, etc.)
|
2
|
+
|
3
|
+
# Initialize database
|
4
|
+
require 'db/all'
|
5
|
+
|
6
|
+
case ENV['db']
|
7
|
+
when 'mysql2'
|
8
|
+
TransactionIsolation::Test::Db.connect_to_mysql2
|
9
|
+
when 'postgresql'
|
10
|
+
TransactionIsolation::Test::Db.connect_to_postgresql
|
11
|
+
when 'sqlite3'
|
12
|
+
TransactionIsolation::Test::Db.connect_to_sqlite3
|
13
|
+
else
|
14
|
+
TransactionIsolation::Test::Db.connect_to_mysql2
|
15
|
+
end
|
16
|
+
|
17
|
+
TransactionIsolation::Test::Migrations.run!
|
18
|
+
|
19
|
+
# Load the code that will be tested
|
20
|
+
require 'transaction_isolation'
|
data/test/log/.gitkeep
ADDED
File without changes
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# Ensure that LOAD_PATH is the same as when running "rake test"; normally rake takes care of that
|
2
|
+
$LOAD_PATH << File.expand_path( ".", File.dirname( __FILE__ ) )
|
3
|
+
$LOAD_PATH << File.expand_path( "./lib", File.dirname( __FILE__ ) )
|
4
|
+
$LOAD_PATH << File.expand_path( "./test", File.dirname( __FILE__ ) )
|
5
|
+
|
6
|
+
# Boot the app
|
7
|
+
require_relative 'library_setup'
|
8
|
+
|
9
|
+
# Fire the console
|
10
|
+
require 'pry'
|
11
|
+
binding.pry
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# Load test coverage tool (must be loaded before any code)
|
2
|
+
#require 'simplecov'
|
3
|
+
#SimpleCov.start do
|
4
|
+
# add_filter '/test/'
|
5
|
+
# add_filter '/config/'
|
6
|
+
#end
|
7
|
+
|
8
|
+
# Load and initialize the application to be tested
|
9
|
+
require 'library_setup'
|
10
|
+
|
11
|
+
# Load test frameworks
|
12
|
+
require 'minitest/autorun'
|
data/test/test_runner.rb
ADDED
data/tests
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "transaction_isolation/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "transaction_isolation"
|
7
|
+
s.version = TransactionIsolation::VERSION
|
8
|
+
s.authors = ["Piotr 'Qertoip' Włodarek"]
|
9
|
+
s.email = ["qertoip@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Adds support for setting transaction isolation level in ActiveRecord in a database agnostic way.}
|
12
|
+
s.description = %q{Run transactions with a serializable / repeatable read / read committed / read uncommitted isolation level in MySQL2, PostgreSQL and SQLite3}
|
13
|
+
|
14
|
+
s.rubyforge_project = "transaction_isolation"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_runtime_dependency "activerecord"
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: transaction_isolation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Piotr 'Qertoip' Włodarek
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: &16117860 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *16117860
|
25
|
+
description: Run transactions with a serializable / repeatable read / read committed
|
26
|
+
/ read uncommitted isolation level in MySQL2, PostgreSQL and SQLite3
|
27
|
+
email:
|
28
|
+
- qertoip@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- Gemfile
|
35
|
+
- LICENSE
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- d
|
39
|
+
- lib/transaction_isolation.rb
|
40
|
+
- lib/transaction_isolation/active_record/base.rb
|
41
|
+
- lib/transaction_isolation/active_record/connection_adapters/abstract_adapter.rb
|
42
|
+
- lib/transaction_isolation/active_record/connection_adapters/mysql2_adapter.rb
|
43
|
+
- lib/transaction_isolation/active_record/connection_adapters/postgresql_adapter.rb
|
44
|
+
- lib/transaction_isolation/active_record/connection_adapters/sqlite3_adapter.rb
|
45
|
+
- lib/transaction_isolation/active_record/errors.rb
|
46
|
+
- lib/transaction_isolation/version.rb
|
47
|
+
- test/db/all.rb
|
48
|
+
- test/db/db.rb
|
49
|
+
- test/db/migrations.rb
|
50
|
+
- test/db/queued_job.rb
|
51
|
+
- test/integration/active_record/base/isolation_level_test.rb
|
52
|
+
- test/integration/active_record/connection_adapters/any_adapter/current_isolation_level_test.rb
|
53
|
+
- test/integration/active_record/connection_adapters/any_adapter/current_vendor_isolation_level_test.rb
|
54
|
+
- test/integration/active_record/connection_adapters/any_adapter/isolation_level_test.rb
|
55
|
+
- test/integration/active_record/connection_adapters/any_adapter/supports_isolation_levels_test.rb
|
56
|
+
- test/integration/active_record/connection_adapters/any_adapter/translate_exception_test.rb
|
57
|
+
- test/library_setup.rb
|
58
|
+
- test/log/.gitkeep
|
59
|
+
- test/test_console.rb
|
60
|
+
- test/test_helper.rb
|
61
|
+
- test/test_runner.rb
|
62
|
+
- tests
|
63
|
+
- transaction-isolation.gemspec
|
64
|
+
homepage: ''
|
65
|
+
licenses: []
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
hash: 1075603796640971358
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
hash: 1075603796640971358
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project: transaction_isolation
|
90
|
+
rubygems_version: 1.8.15
|
91
|
+
signing_key:
|
92
|
+
specification_version: 3
|
93
|
+
summary: Adds support for setting transaction isolation level in ActiveRecord in a
|
94
|
+
database agnostic way.
|
95
|
+
test_files: []
|