Class: ActiveRecord::ConnectionAdapters::TransactionManager
- Inherits:
-
Object
- Object
- ActiveRecord::ConnectionAdapters::TransactionManager
- Defined in:
- lib/active_record/connection_adapters/abstract/transaction.rb
Overview
:nodoc:
Instance Method Summary collapse
- #begin_transaction(options = {}) ⇒ Object
- #commit_transaction ⇒ Object
- #current_transaction ⇒ Object
-
#initialize(connection) ⇒ TransactionManager
constructor
A new instance of TransactionManager.
- #open_transactions ⇒ Object
- #rollback_transaction(transaction = nil) ⇒ Object
- #within_new_transaction(options = {}) ⇒ Object
Constructor Details
#initialize(connection) ⇒ TransactionManager
Returns a new instance of TransactionManager.
146 147 148 149 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 146 def initialize(connection) @stack = [] @connection = connection end |
Instance Method Details
#begin_transaction(options = {}) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 151 def begin_transaction( = {}) @connection.lock.synchronize do run_commit_callbacks = !current_transaction.joinable? transaction = if @stack.empty? RealTransaction.new(@connection, , run_commit_callbacks: run_commit_callbacks) else SavepointTransaction.new(@connection, "active_record_#{@stack.size}", , run_commit_callbacks: run_commit_callbacks) end @stack.push(transaction) transaction end end |
#commit_transaction ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 167 def commit_transaction @connection.lock.synchronize do transaction = @stack.last begin transaction.before_commit_records ensure @stack.pop end transaction.commit transaction.commit_records end end |
#current_transaction ⇒ Object
222 223 224 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 222 def current_transaction @stack.last || NULL_TRANSACTION end |
#open_transactions ⇒ Object
218 219 220 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 218 def open_transactions @stack.size end |
#rollback_transaction(transaction = nil) ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 182 def rollback_transaction(transaction = nil) @connection.lock.synchronize do transaction ||= @stack.pop transaction.rollback transaction.rollback_records end end |
#within_new_transaction(options = {}) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 190 def within_new_transaction( = {}) @connection.lock.synchronize do begin transaction = begin_transaction yield rescue Exception => error if transaction rollback_transaction after_failure_actions(transaction, error) end raise ensure unless error if Thread.current.status == "aborting" rollback_transaction if transaction else begin commit_transaction rescue Exception rollback_transaction(transaction) unless transaction.state.completed? raise end end end end end end |