Class: TorqueBox::Transactions::Manager

Inherits:
Object
  • Object
show all
Includes:
ActiveRecordAdapters::Transaction, Transaction
Defined in:
lib/torquebox/transactions.rb,
lib/torquebox/active_record_adapters.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveRecordAdapters::Transaction

#commit, #connections, #error, #prepare, #rollback, #should_commit?, #should_rollback?

Methods included from Transaction

#commit, #error, #prepare, #rollback

Constructor Details

#initializeManager

Returns a new instance of Manager.



65
66
67
68
# File 'lib/torquebox/transactions.rb', line 65

def initialize()
  @tm = TorqueBox.fetch('transaction-manager')
  @transactions = []
end

Class Method Details

.currentObject



61
62
63
# File 'lib/torquebox/transactions.rb', line 61

def self.current()
  Thread.current[:torquebox_transaction] ||= new
end

Instance Method Details

#active?Boolean

Is there an active transaction?

Returns:

  • (Boolean)


81
82
83
# File 'lib/torquebox/transactions.rb', line 81

def active?
  @tm.transaction != nil
end

#enlist(*resources) ⇒ Object

Associate a list of resources with the current transaction. A resource is expected to either implement XAResource or respond_to?(:xa_resource)



73
74
75
76
77
78
# 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

#mandatoryObject

JEE Mandatory



138
139
140
141
142
143
144
# File 'lib/torquebox/transactions.rb', line 138

def mandatory
  if active?
    yield
  else
    raise "No active transaction"
  end
end

#neverObject

JEE Never



147
148
149
150
151
152
153
# File 'lib/torquebox/transactions.rb', line 147

def never
  if active?
    raise "Active transaction detected"
  else
    yield
  end
end

#not_supported(&block) ⇒ Object

JEE NotSupported



124
125
126
127
128
129
130
# File 'lib/torquebox/transactions.rb', line 124

def not_supported &block
  if active?
    suspend &block
  else
    yield
  end
end

#parse_args(*args) ⇒ Object

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!



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# 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(&block) ⇒ Object

JEE Required



104
105
106
107
108
109
110
# File 'lib/torquebox/transactions.rb', line 104

def required &block
  if active?
    yield
  else
    start &block
  end
end

#requires_new(&block) ⇒ Object

JEE RequiresNew



113
114
115
116
117
118
119
120
121
# 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, &block) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
# 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

#startObject

Begin a transaction, yield, commit or rollback on error.



86
87
88
89
90
91
92
93
# File 'lib/torquebox/transactions.rb', line 86

def start
  prepare
  result = yield
  commit
  result
rescue Exception => e
  error(e)
end

#supportsObject

JEE Supports



133
134
135
# File 'lib/torquebox/transactions.rb', line 133

def supports
  yield
end

#suspendObject

Suspend current transaction, yield, and resume



96
97
98
99
100
101
# File 'lib/torquebox/transactions.rb', line 96

def suspend
  @transactions.push( @tm.suspend )
  yield
ensure
  @tm.resume( @transactions.pop )
end