Class: DataMapper::Transaction

  • Object
    • DataMapper::Transaction

Attributes

Instance Attributes

adapters [RW] public

Returns the value of attribute adapters.

state [RW] public

Returns the value of attribute state.

transaction_primitives [RW] public

Returns the value of attribute transaction_primitives.

Constructor Summary

public initialize(*things, &block)

Create a new DataMapper::Transaction

Meta Tags

See Also:

DataMapper::Transaction#link In fact, it just calls #link with the given arguments at the end of the constructor.
[View source]


14
15
16
17
18
19
20
# File 'dm-core/lib/dm-core/transaction.rb', line 14

def initialize(*things, &block)
  @transaction_primitives = {}
  @state = :none
  @adapters = {}
  link(*things)
  commit(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

public method_missing(meth, *args, &block)
[View source]


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'dm-core/lib/dm-core/transaction.rb', line 144

def method_missing(meth, *args, &block)
  if args.size == 1 && args.first.is_a?(DataMapper::Adapters::AbstractAdapter)



    if (match = meth.to_s.match(/^(.*)_if_(none|begin|prepare|rollback|commit)$/))



      if self.respond_to?(match[1], true)



        self.send(match[1], args.first) if state_for(args.first).to_s == match[2]
      else
        super
      end
    elsif (match = meth.to_s.match(/^(.*)_unless_(none|begin|prepare|rollback|commit)$/))
      if self.respond_to?(match[1], true)



        self.send(match[1], args.first) unless state_for(args.first).to_s == match[2]
      else
        super
      end
    else
      super
    end
  else
    super
  end
end

Public Visibility

Public Instance Method Summary

#begin

Begin the transaction.

#commit(&block)

Commit the transaction.

#link(*things, &block)

Associate this Transaction with some things.

#primitive_for(adapter)
#rollback

Rollback the transaction.

#within(&block)

Execute a block within this Transaction.

Public Instance Methods Inherited from Object

validatable?

Public Instance Method Details

begin

public begin

Begin the transaction

Before #begin is called, the transaction is not valid and can not be used.

[View source]


66
67
68
69
70
# File 'dm-core/lib/dm-core/transaction.rb', line 66

def begin;      raise "Illegal state for begin: #{@state}" unless @state == :none
  each_adapter(:connect_adapter, [:log_fatal_transaction_breakage])
  each_adapter(:begin_adapter, [:rollback_and_close_adapter_if_begin, :close_adapter_if_none])
  @state = :begin
end

commit

public commit(&block)

Commit the transaction

Meta Tags

Parameters:

block<Block>

a block (taking the one argument, the Transaction) to execute within this transaction. The transaction will begin and commit around the block, and roll back if an exception is raised.

[View source]


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'dm-core/lib/dm-core/transaction.rb', line 84

def commit(&block)
  if block_given?



    raise "Illegal state for commit with block: #{@state}" unless @state == :none
    begin
      self.begin
      rval = within(&block)
      self.commit if @state == :begin
      return rval
    rescue Exception => e
      self.rollback if @state == :begin
      raise e
    end
  else
    raise "Illegal state for commit without block: #{@state}" unless @state == :begin
    each_adapter(:prepare_adapter, [:rollback_and_close_adapter_if_begin, :rollback_prepared_and_close_adapter_if_prepare])
    each_adapter(:commit_adapter, [:log_fatal_transaction_breakage])
    each_adapter(:close_adapter, [:log_fatal_transaction_breakage])
    @state = :commit
  end
end

link

primitive_for

public primitive_for(adapter)
[View source]


166
167
168
169
170
# File 'dm-core/lib/dm-core/transaction.rb', line 166

def primitive_for(adapter)
  raise "Unknown adapter #{adapter}" unless @adapters.include?(adapter)
  raise "No primitive for #{adapter}" unless @transaction_primitives.include?(adapter)
  @transaction_primitives[adapter]
end

rollback

public rollback

Rollback the transaction

Will undo all changes made during the transaction.

[View source]


110
111
112
113
114
115
116
117
118
# File 'dm-core/lib/dm-core/transaction.rb', line 110

def rollback


  raise "Illegal state for rollback: #{@state}" unless @state == :begin
  each_adapter(:rollback_adapter_if_begin, [:rollback_and_close_adapter_if_begin, :close_adapter_if_none])
  each_adapter(:rollback_prepared_adapter_if_prepare, [:rollback_prepared_and_close_adapter_if_begin, :close_adapter_if_none])
  each_adapter(:close_adapter_if_open, [:log_fatal_transaction_breakage])
  @state = :rollback
end

within

public within(&block)

Execute a block within this Transaction.

Meta Tags

Parameters:

block<Block>

the block of code to execute.

[View source]


129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'dm-core/lib/dm-core/transaction.rb', line 129

def within(&block)
  raise "No block provided" unless block_given?
  raise "Illegal state for within: #{@state}" unless @state == :begin
  @adapters.each do |adapter, state|
    adapter.push_transaction(self)
  end
  begin
    return yield(self)
  ensure
    @adapters.each do |adapter, state|
      adapter.pop_transaction
    end
  end
end