Method: Extralite::Database#transaction

Defined in:
lib/extralite.rb

#transaction(mode = :immediate) ⇒ Any

Starts a transaction and runs the given block. If an exception is raised in the block, the transaction is rolled back. Otherwise, the transaction is commited after running the block.

db.transaction do
  db.execute('insert into foo values (1, 2, 3)')
  raise if db.query_single_value('select x from bar') > 42
end

For more information on transactions see: https://sqlite.org/lang_transaction.html



237
238
239
240
241
242
243
244
245
246
# File 'lib/extralite.rb', line 237

def transaction(mode = :immediate)
  abort = false
  execute "begin #{mode} transaction"    
  yield self
rescue => e
  abort = true
  e.is_a?(Rollback) ? nil : raise
ensure
  execute(abort ? 'rollback' : 'commit')
end