class TorqueBox::Transactions::Manager
The thread-local Manager
encapsulates the interaction with the real JBoss TransactionManager
Public Class Methods
# File lib/torquebox/transactions.rb, line 61 def self.current() Thread.current[:torquebox_transaction] ||= new end
# File lib/torquebox/transactions.rb, line 65 def initialize() @tm = TorqueBox.fetch('transaction-manager') @transactions = [] end
Public Instance Methods
Is there an active transaction?
# File lib/torquebox/transactions.rb, line 81 def active? @tm.transaction != nil end
Associate a list of resources with the current transaction. A resource is expected to either implement XAResource or respond_to?(:xa_resource)
# File lib/torquebox/transactions.rb, line 73 def enlist(*resources) resources.each do |resource| xa = resource.is_a?( javax.transaction.xa.XAResource ) ? resource : resource.xa_resource @tm.transaction.enlist_resource(xa) end end
JEE Mandatory
# File lib/torquebox/transactions.rb, line 138 def mandatory if active? yield else raise "No active transaction" end end
JEE Never
# File lib/torquebox/transactions.rb, line 147 def never if active? raise "Active transaction detected" else yield end end
JEE NotSupported
# File lib/torquebox/transactions.rb, line 124 def not_supported &block if active? suspend &block else yield end end
Returns a 2-element tuple [resources, method] where method is a symbol corresponding to one of the JEE tx attribute methods above. All but the last argument should be XA resources to be enlisted in the transaction. The last argument passed is expected to be either a symbol referring to one of the JEE methods or a Hash in which the method symbol is associated with the :scope key. If omitted, defaults to :required.
For backwards compatibility the hash may also contain a :requires_new key which, if true, will result in the :requires_new method symbol being returned.
:none may be used as an alias for :not_supported
Whew!
# File lib/torquebox/transactions.rb, line 170 def parse_args(*args) resources, method = case args.last when Symbol last = args.pop [args, last] when Hash hash = args.pop last = hash[:scope] || (hash[:requires_new] ? :requires_new : :required) [args, last] else [args, :required] end method = :not_supported if method == :none [resources, method] end
JEE Required
# File lib/torquebox/transactions.rb, line 104 def required &block if active? yield else start &block end end
JEE RequiresNew
# File lib/torquebox/transactions.rb, line 113 def requires_new &block if active? suspend do start &block end else start &block end end
# File lib/torquebox/transactions.rb, line 186 def run(*args, &block) return yield unless @tm resources, method = parse_args(*args) fn = lambda do enlist(*resources) block.call end send(method, &fn) ensure Thread.current[:torquebox_transaction] = nil if @transactions.empty? end
Begin a transaction, yield, commit or rollback on error.
# File lib/torquebox/transactions.rb, line 86 def start prepare result = yield commit result rescue Exception => e error(e) end
JEE Supports
# File lib/torquebox/transactions.rb, line 133 def supports yield end
Suspend current transaction, yield, and resume
# File lib/torquebox/transactions.rb, line 96 def suspend @transactions.push( @tm.suspend ) yield ensure @tm.resume( @transactions.pop ) end