Method: PG::Connection#transaction

Defined in:
lib/pg/connection.rb

#transactionObject

call-seq:

conn.transaction { |conn| ... } -> result of the block

Executes a BEGIN at the start of the block, and a COMMIT at the end of the block, or ROLLBACK if any exception occurs.



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/pg/connection.rb', line 308

def transaction
	rollback = false
	exec "BEGIN"
	yield(self)
rescue PG::RollbackTransaction
	rollback = true
	cancel if transaction_status == PG::PQTRANS_ACTIVE
	block
	exec "ROLLBACK"
rescue Exception
	rollback = true
	cancel if transaction_status == PG::PQTRANS_ACTIVE
	block
	exec "ROLLBACK"
	raise
ensure
	exec "COMMIT" unless rollback
end