class TorqueBox::Transactions::Manager

The thread-local Manager encapsulates the interaction with the real JBoss TransactionManager

Public Class Methods

current() click to toggle source
# File lib/torquebox/transactions.rb, line 61
def self.current()
  Thread.current[:torquebox_transaction] ||= new
end
new() click to toggle source
# File lib/torquebox/transactions.rb, line 65
def initialize()
  @tm = TorqueBox.fetch('transaction-manager')
  @transactions = []
end

Public Instance Methods

active?() click to toggle source

Is there an active transaction?

# File lib/torquebox/transactions.rb, line 81
def active?
  @tm.transaction != nil
end
enlist(*resources) click to toggle source

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
mandatory() { || ... } click to toggle source

JEE Mandatory

# File lib/torquebox/transactions.rb, line 138
def mandatory
  if active?
    yield
  else
    raise "No active transaction"
  end
end
never() { || ... } click to toggle source

JEE Never

# File lib/torquebox/transactions.rb, line 147
def never
  if active?
    raise "Active transaction detected"
  else
    yield
  end
end
not_supported() { || ... } click to toggle source

JEE NotSupported

# File lib/torquebox/transactions.rb, line 124
def not_supported &block
  if active?
    suspend &block
  else
    yield
  end
end
parse_args(*args) click to toggle source

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
required() { || ... } click to toggle source

JEE Required

# File lib/torquebox/transactions.rb, line 104
def required &block
  if active?
    yield
  else
    start &block
  end
end
requires_new(&block) click to toggle source

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
run(*args) { || ... } click to toggle source
# 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
start() { || ... } click to toggle source

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
supports() { || ... } click to toggle source

JEE Supports

# File lib/torquebox/transactions.rb, line 133
def supports
  yield
end
suspend() { || ... } click to toggle source

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