Method: Pgtk::Pool#transaction

Defined in:
lib/pgtk/pool.rb

#transactionObject

Run a transaction. The block has to be provided. It will receive a temporary object, which implements method exec, which works exactly like the method exec of class Pool, for example:

pgsql.transaction do |t|
  t.exec('DELETE FROM user WHERE id = $1', [id])
  t.exec('INSERT INTO user (name) VALUES ($1)', [name])
end


208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/pgtk/pool.rb', line 208

def transaction
  connect do |c|
    t = Txn.new(c, @log)
    t.exec('START TRANSACTION')
    begin
      r = yield t
      t.exec('COMMIT')
      r
    rescue StandardError => e
      t.exec('ROLLBACK')
      raise e
    end
  end
end