Class: DataObjects::NestedTransaction
- Inherits:
-
Transaction
- Object
- Transaction
- DataObjects::NestedTransaction
- Defined in:
- lib/dm-nested-transactions.rb
Constant Summary collapse
- HOST =
The host name. Note, this relies on the host name being configured and resolvable using DNS
"#{Socket::gethostbyname(Socket::gethostname)[0]}" rescue "localhost"
- @@counter =
0
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
The connection object for this transaction - must have already had a transaction begun on it.
-
#id ⇒ Object
readonly
A unique ID for this transaction.
Class Method Summary collapse
Instance Method Summary collapse
- #begin ⇒ Object
- #begin_statement ⇒ Object
- #close ⇒ Object
- #commit ⇒ Object
- #commit_statement ⇒ Object
-
#initialize(uri, connection, adapter) ⇒ NestedTransaction
constructor
Creates a NestedTransaction bound to an existing connection.
- #rollback ⇒ Object
- #rollback_statement ⇒ Object
Constructor Details
#initialize(uri, connection, adapter) ⇒ NestedTransaction
Creates a NestedTransaction bound to an existing connection
64 65 66 67 68 |
# File 'lib/dm-nested-transactions.rb', line 64 def initialize(uri, connection, adapter) @adapter = adapter @connection = connection @id = Digest::SHA256.hexdigest("#{HOST}:#{$$}:#{Time.now.to_f}:nested:#{@@counter += 1}")[0..10] end |
Instance Attribute Details
#connection ⇒ Object (readonly)
The connection object for this transaction - must have already had a transaction begun on it
52 53 54 |
# File 'lib/dm-nested-transactions.rb', line 52 def connection @connection end |
#id ⇒ Object (readonly)
A unique ID for this transaction
54 55 56 |
# File 'lib/dm-nested-transactions.rb', line 54 def id @id end |
Class Method Details
.create_for_uri(uri, connection, adapter) ⇒ Object
56 57 58 59 |
# File 'lib/dm-nested-transactions.rb', line 56 def self.create_for_uri(uri, connection, adapter) uri = uri.is_a?(String) ? URI::parse(uri) : uri DataObjects::NestedTransaction.new(uri, connection, adapter) end |
Instance Method Details
#begin ⇒ Object
90 91 92 |
# File 'lib/dm-nested-transactions.rb', line 90 def begin connection.create_command(begin_statement.call(@id)).execute_non_query end |
#begin_statement ⇒ Object
70 71 72 |
# File 'lib/dm-nested-transactions.rb', line 70 def begin_statement lambda{ |id| "SAVEPOINT \"#{id}\"" } end |
#close ⇒ Object
87 88 |
# File 'lib/dm-nested-transactions.rb', line 87 def close end |
#commit ⇒ Object
94 95 96 97 98 99 |
# File 'lib/dm-nested-transactions.rb', line 94 def commit statement = commit_statement if statement connection.create_command(commit_statement.call(@id)).execute_non_query end end |
#commit_statement ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/dm-nested-transactions.rb', line 74 def commit_statement case @adapter.class.name when /oracle/i nil else lambda{ |id| "RELEASE SAVEPOINT \"#{id}\"" } end end |
#rollback ⇒ Object
101 102 103 |
# File 'lib/dm-nested-transactions.rb', line 101 def rollback connection.create_command(rollback_statement.call(@id)).execute_non_query end |
#rollback_statement ⇒ Object
83 84 85 |
# File 'lib/dm-nested-transactions.rb', line 83 def rollback_statement lambda{ |id| "ROLLBACK TO SAVEPOINT \"#{id}\"" } end |