Module: Transaction::Simple
- Included in:
- ThreadSafe
- Defined in:
- lib/transaction/simple.rb
Defined Under Namespace
Modules: ThreadSafe Classes: Group
Constant Summary collapse
- VERSION =
TRANSACTION_SIMPLE_VERSION = '1.4.0.2'
- SKIP_TRANSACTION_VARS =
%w(@__transaction_checkpoint__ @__transaction_level__)
Class Attribute Summary collapse
-
.debug_with_checkpoint ⇒ Object
Set to
true
if you want the checkpoint printed with debugging messages where it matters.
Class Method Summary collapse
- .__common_start(name, vars, &block) ⇒ Object
-
.debug(format, *args) ⇒ Object
Fast debugging.
-
.debug_io ⇒ Object
Returns the Transaction::Simple debug object.
-
.debug_io=(io) ⇒ Object
Sets the Transaction::Simple debug object.
-
.debugging? ⇒ Boolean
Returns
true
if we are debugging. -
.start(*vars, &block) ⇒ Object
Start a named transaction in a block.
-
.start_named(name, *vars, &block) ⇒ Object
Start a named transaction in a block.
Instance Method Summary collapse
-
#abort_transaction(name = nil) ⇒ Object
Aborts the transaction.
-
#commit_transaction(name = nil) ⇒ Object
If
name
isnil
(default), the current transaction level is closed out and the changes are committed. -
#rewind_transaction(name = nil) ⇒ Object
Rewinds the transaction.
-
#start_transaction(name = nil) ⇒ Object
Starts a transaction.
-
#transaction(action = nil, name = nil) ⇒ Object
Alternative method for calling the transaction methods.
-
#transaction_exclusions ⇒ Object
Allows specific variables to be excluded from transaction support.
-
#transaction_name ⇒ Object
Returns the current name of the transaction.
-
#transaction_open?(name = nil) ⇒ Boolean
If
name
isnil
(default), then returnstrue
if there is currently a transaction open.
Class Attribute Details
.debug_with_checkpoint ⇒ Object
Set to true
if you want the checkpoint printed with debugging messages where it matters.
64 65 66 |
# File 'lib/transaction/simple.rb', line 64 def debug_with_checkpoint @debug_with_checkpoint end |
Class Method Details
.__common_start(name, vars, &block) ⇒ Object
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/transaction/simple.rb', line 384 def __common_start(name, vars, &block) raise Transaction::TransactionError, [:cannot_start_empty_block_transaction] if vars.empty? if block begin vlevel = {} vars.each do |vv| vv.extend(Transaction::Simple) vv.start_transaction(name) vlevel[vv.__id__] = vv.instance_variable_get(:@__transaction_level__) vv.instance_variable_set(:@__transaction_block__, vlevel[vv.__id__]) end yield(*vars) rescue Transaction::TransactionAborted vars.each do |vv| if name.nil? and vv.transaction_open? loop do tlevel = vv.instance_variable_get(:@__transaction_level__) || -1 vv.instance_variable_set(:@__transaction_block__, -1) break if tlevel < vlevel[vv.__id__] vv.abort_transaction if vv.transaction_open? end elsif vv.transaction_open?(name) vv.instance_variable_set(:@__transaction_block__, -1) vv.abort_transaction(name) end end rescue Transaction::TransactionCommitted nil ensure vars.each do |vv| if name.nil? and vv.transaction_open? loop do tlevel = vv.instance_variable_get(:@__transaction_level__) || -1 break if tlevel < vlevel[vv.__id__] vv.instance_variable_set(:@__transaction_block__, -1) vv.commit_transaction if vv.transaction_open? end elsif vv.transaction_open?(name) vv.instance_variable_set(:@__transaction_block__, -1) vv.commit_transaction(name) end end end else vars.each do |vv| vv.extend(Transaction::Simple) vv.start_transaction(name) end end end |
.debug(format, *args) ⇒ Object
Fast debugging.
78 79 80 81 |
# File 'lib/transaction/simple.rb', line 78 def debug(format, *args) return unless debugging? debug_io << (format % args) end |
.debug_io ⇒ Object
Returns the Transaction::Simple debug object. It must respond to #<<.
72 73 74 75 |
# File 'lib/transaction/simple.rb', line 72 def debug_io @tdi ||= "" @tdi end |
.debug_io=(io) ⇒ Object
Sets the Transaction::Simple debug object. It must respond to #<<. Debugging will be performed automatically if there’s a debug object.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/transaction/simple.rb', line 51 def debug_io=(io) if io.nil? @tdi = nil @debugging = false else raise Transaction::TransactionError, Transaction::Messages[:bad_debug_object] unless io.respond_to?(:<<) @tdi = io @debugging = true end end |
.debugging? ⇒ Boolean
Returns true
if we are debugging.
67 68 69 |
# File 'lib/transaction/simple.rb', line 67 def debugging? defined? @debugging and @debugging end |
.start(*vars, &block) ⇒ Object
Start a named transaction in a block. The transaction will auto-commit when the block finishes.
447 448 449 |
# File 'lib/transaction/simple.rb', line 447 def start(*vars, &block) __common_start(nil, vars, &block) end |
.start_named(name, *vars, &block) ⇒ Object
Start a named transaction in a block. The transaction will auto-commit when the block finishes.
441 442 443 |
# File 'lib/transaction/simple.rb', line 441 def start_named(name, *vars, &block) __common_start(name, vars, &block) end |
Instance Method Details
#abort_transaction(name = nil) ⇒ Object
Aborts the transaction. Rewinds the object state to what it was before the transaction was started and closes the transaction. If name
is specified, then the intervening transactions and the named transaction will be aborted. Otherwise, only the current transaction is aborted.
See #rewind_transaction for information about dealing with complex self-referential object graphs.
If the current or named transaction has been started by a block (Transaction::Simple.start), then the execution of the block will be halted with break
self
.
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/transaction/simple.rb', line 269 def abort_transaction(name = nil) raise Transaction::TransactionError, [:cannot_abort_no_transaction] if @__transaction_checkpoint__.nil? # Check to see if we are trying to abort a transaction that is outside # of the current transaction block. Otherwise, raise TransactionAborted # if they are the same. defined? @__transaction_block__ or @__transaction_block__ = nil if @__transaction_block__ and name nix = @__transaction_names__.index(name) + 1 raise Transaction::TransactionError, [:cannot_abort_transaction_before_block] if nix < @__transaction_block__ raise Transaction::TransactionAborted if @__transaction_block__ == nix end raise Transaction::TransactionAborted if @__transaction_block__ == @__transaction_level__ if name.nil? __abort_transaction(name) else raise Transaction::TransactionError, [:cannot_abort_named_transaction] % name.inspect unless @__transaction_names__.include?(name) __abort_transaction(name) while @__transaction_names__.include?(name) end self end |
#commit_transaction(name = nil) ⇒ Object
If name
is nil
(default), the current transaction level is closed out and the changes are committed.
If name
is specified and name
is in the list of named transactions, then all transactions are closed and committed until the named transaction is reached.
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/transaction/simple.rb', line 300 def commit_transaction(name = nil) raise Transaction::TransactionError, [:cannot_commit_no_transaction] if @__transaction_checkpoint__.nil? @__transaction_block__ ||= nil # Check to see if we are trying to commit a transaction that is outside # of the current transaction block. Otherwise, raise # TransactionCommitted if they are the same. if @__transaction_block__ and name nix = @__transaction_names__.index(name) + 1 raise Transaction::TransactionError, [:cannot_commit_transaction_before_block] if nix < @__transaction_block__ raise Transaction::TransactionCommitted if @__transaction_block__ == nix end raise Transaction::TransactionCommitted if @__transaction_block__ == @__transaction_level__ if name.nil? ___tdebug "<", "%s(%s)", [:commit_transaction], name.inspect __commit_transaction else raise Transaction::TransactionError, [:cannot_commit_named_transaction] % name.inspect unless @__transaction_names__.include?(name) while @__transaction_names__.last != name ___tdebug "<", "%s(%s)", [:commit_transaction], name.inspect __commit_transaction ___tdebug_checkpoint end ___tdebug "<", "%s(%s)", [:commit_transaction], name.inspect __commit_transaction end ___tdebug_checkpoint self end |
#rewind_transaction(name = nil) ⇒ Object
Rewinds the transaction. If name
is specified, then the intervening transactions will be aborted and the named transaction will be rewound. Otherwise, only the current transaction is rewound.
After each level of transaction is rewound, if the callback method #_post_transaction_rewind is defined, it will be called. It is intended to allow a complex self-referential graph to fix itself. The simplest way to explain this is with an example.
class Child
attr_accessor :parent
end
class Parent
include Transaction::Simple
attr_reader :children
def initialize
@children = []
end
def << child
child.parent = self
@children << child
end
def valid?
@children.all? { |child| child.parent == self }
end
end
parent = Parent.new
parent << Child.new
parent.start_transaction
parent << Child.new
parent.abort_transaction
puts parent.valid? # => false
This problem can be fixed by modifying the Parent class to include the #_post_transaction_rewind callback.
class Parent
# Reconnect the restored children to me, instead of to the bogus me
# that was restored to them by Marshal::load.
def _post_transaction_rewind
@children.each { |child| child.parent = self }
end
end
parent = Parent.new
parent << Child.new
parent.start_transaction
parent << Child.new
parent.abort_transaction
puts parent.valid? # => true
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/transaction/simple.rb', line 221 def rewind_transaction(name = nil) raise Transaction::TransactionError, [:cannot_rewind_no_transaction] if @__transaction_checkpoint__.nil? # Check to see if we are trying to rewind a transaction that is outside # of the current transaction block. defined? @__transaction_block__ or @__transaction_block__ = nil if @__transaction_block__ and name nix = @__transaction_names__.index(name) + 1 raise Transaction::TransactionError, [:cannot_rewind_transaction_before_block] if nix < @__transaction_block__ end if name.nil? checkpoint = @__transaction_checkpoint__ __rewind_this_transaction @__transaction_checkpoint__ = checkpoint else raise Transaction::TransactionError, [:cannot_rewind_named_transaction] % name.inspect unless @__transaction_names__.include?(name) while @__transaction_names__.last != name ___tdebug_checkpoint @__transaction_checkpoint__ = __rewind_this_transaction ___tdebug '<', [:rewind_transaction], name @__transaction_level__ -= 1 @__transaction_names__.pop end checkpoint = @__transaction_checkpoint__ __rewind_this_transaction @__transaction_checkpoint__ = checkpoint end ___tdebug '|', "%s(%s)", [:rewind_transaction], name.inspect ___tdebug_checkpoint self end |
#start_transaction(name = nil) ⇒ Object
Starts a transaction. Stores the current object state. If a transaction name is specified, the transaction will be named. Transaction names must be unique. Transaction names of nil
will be treated as unnamed transactions.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/transaction/simple.rb', line 147 def start_transaction(name = nil) @__transaction_level__ ||= 0 @__transaction_names__ ||= [] name = name.dup.freeze if name.kind_of?(String) raise Transaction::TransactionError, [:unique_names] if name and @__transaction_names__.include?(name) @__transaction_names__ << name @__transaction_level__ += 1 ___tdebug '>', "%s(%s)", [:start_transaction], name.inspect ___tdebug_checkpoint checkpoint = Marshal.dump(self) @__transaction_checkpoint__ = Marshal.dump(self) end |
#transaction(action = nil, name = nil) ⇒ Object
Alternative method for calling the transaction methods. An optional name can be specified for named transaction support. This method is deprecated and will be removed in Transaction::Simple 2.0.
- #transaction(:start)
-
#start_transaction
- #transaction(:rewind)
-
#rewind_transaction
- #transaction(:abort)
-
#abort_transaction
- #transaction(:commit)
-
#commit_transaction
- #transaction(:name)
-
#transaction_name
- #transaction
-
#transaction_open?
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/transaction/simple.rb', line 347 def transaction(action = nil, name = nil) _method = case action when :start then :start_transaction when :rewind then :rewind_transaction when :abort then :abort_transaction when :commit then :commit_transaction when :name then :transaction_name when nil then :transaction_open? else nil end if _method warn "The #transaction method has been deprecated. Use #{_method} instead." else warn "The #transaction method has been deprecated." end case _method when :transaction_name __send__ _method when nil nil else __send__ _method, name end end |
#transaction_exclusions ⇒ Object
Allows specific variables to be excluded from transaction support. Must be done after extending the object but before starting the first transaction on the object.
vv.transaction_exclusions << "@io"
379 380 381 |
# File 'lib/transaction/simple.rb', line 379 def transaction_exclusions @transaction_exclusions ||= [] end |
#transaction_name ⇒ Object
Returns the current name of the transaction. Transactions not explicitly named are named nil
.
129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/transaction/simple.rb', line 129 def transaction_name raise Transaction::TransactionError, [:no_transaction_open] if @__transaction_checkpoint__.nil? name = @__transaction_names__.last ___tdebug '|', "%s(%s)", [:transaction_name], name.inspect if name.kind_of?(String) name.dup else name end end |
#transaction_open?(name = nil) ⇒ Boolean
If name
is nil
(default), then returns true
if there is currently a transaction open. If name
is specified, then returns true
if there is currently a transaction known as name
open.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/transaction/simple.rb', line 110 def transaction_open?(name = nil) defined? @__transaction_checkpoint__ or @__transaction_checkpoint__ = nil has_t = nil if name.nil? has_t = (not @__transaction_checkpoint__.nil?) else has_t = ((not @__transaction_checkpoint__.nil?) and @__transaction_names__.include?(name)) end ___tdebug '>', "%s [%s]", [:transaction], [has_t ? :opened : :closed] has_t end |