Class: Archipelago::Tranny::TransactionProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/archipelago/tranny.rb

Overview

A proxy to a transaction managed by this manager.

Used because standard DRbObjects only use object_id to identify objects, while we want the more unique transaction_id.

Will also remember the state of the transaction when it detects methods that will terminate it (:commit | :abort).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transaction) ⇒ TransactionProxy

Returns a new instance of TransactionProxy.



185
186
187
188
189
190
# File 'lib/archipelago/tranny.rb', line 185

def initialize(transaction)
  @manager = transaction.manager
  @manager_id = transaction.manager.service_id
  @transaction_id = transaction.transaction_id
  @state = :unknown
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

Forwards everything to our Transaction and remembers returnvalue if necessary.



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/archipelago/tranny.rb', line 230

def method_missing(meth, *args) #:nodoc:
  rval = nil
  begin
    rval = @manager.call_instance_method(@transaction_id, meth, *args)
  rescue DRb::DRbConnError => e
    if defined?(Archipelago::Disco::MC)
      possible_replacements = Archipelago::Disco::MC.lookup(Archipelago::Disco::Query.new({:service_id => @manager_id}))
      raise e if possible_replacements.empty?
      
      @manager = possible_replacements[@manager_id][:service]
      
      retry
    else
      raise e
    end
  end
  case meth
  when :abort!
    @state = :aborted
  when :commit!
    @state = rval
  end
  return rval
end

Instance Attribute Details

#transaction_idObject

Returns the value of attribute transaction_id.



184
185
186
# File 'lib/archipelago/tranny.rb', line 184

def transaction_id
  @transaction_id
end

Instance Method Details

#==(o) ⇒ Object

Implemented to allow comparison between proxies.



223
224
225
# File 'lib/archipelago/tranny.rb', line 223

def ==(o)
  eql?(o)
end

#eql?(o) ⇒ Boolean

Implemented to ensure that all TransactionProxies with the same transaction_id are hashwise equal.

Returns:

  • (Boolean)


213
214
215
216
217
218
219
# File 'lib/archipelago/tranny.rb', line 213

def eql?(o)
  if TransactionProxy === o
    o.transaction_id == @transaction_id
  else
    false
  end
end

#hashObject

Implemented to ensure that all TransactionProxies with the same transaction_id are hashwise equal.



206
207
208
# File 'lib/archipelago/tranny.rb', line 206

def hash
  @transaction_id.hash
end

#stateObject

Return the cached state of the wrapped Archipelago::Tranny::Transaction if it is known, otherwise fetch it.



195
196
197
198
199
200
201
# File 'lib/archipelago/tranny.rb', line 195

def state
  if @state == :unknown
    method_missing(:state)
  else
    @state
  end
end