Class: TorqueBox::Transactions::Manager

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

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

- (Manager) initialize

Returns a new instance of Manager



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

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

Class Method Details

+ (Object) current



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

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

Instance Method Details

- (Boolean) active?

Returns:

  • (Boolean)


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

def active?
  @tm.transaction != nil
end

- (Object) enlist(*resources)



73
74
75
76
77
78
# File 'transactions/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

- (Object) mandatory



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

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

- (Object) never



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

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

- (Object) not_supported(&block)



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

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

- (Object) parse_args(*args)



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'transactions/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

- (Object) required(&block)



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

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

- (Object) requires_new(&block)



113
114
115
116
117
118
119
120
121
# File 'transactions/lib/torquebox/transactions.rb', line 113

def requires_new &block
  if active?
    suspend do
      start &block
    end
  else
    start &block
  end
end

- (Object) run(*args, &block)



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

- (Object) start



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

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

- (Object) supports



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

def supports
  yield
end

- (Object) suspend



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

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