Method: WIKK::SQL#transaction

Defined in:
lib/wikk_mysql2.rb,
lib/wikk_ruby_mysql.rb

#transaction {|[]| ... } ⇒ Object

Perform a transaction in the passed block. RollBACK on error, otherwise COMMIT

Yield Parameters:

  • []

    yields to block, where the queries are performed.

Raises:

  • (Mysql)

    passes on Mysql errors, freeing the result.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/wikk_mysql2.rb', line 128

def transaction
  raise Mysql2::Error, 2002 if @my.nil?

  if block_given?
    begin
      @my.query('START TRANSACTION WITH CONSISTENT SNAPSHOT')
      yield # Start executing the query black.
      @my.query('COMMIT')
    rescue Mysql2::Error => e
      @my.query('ROLLBACK')
      raise e
    end
  end
end