Method: SQLite3::Database#transaction
- Defined in:
- lib/sqlite3/database.rb
#transaction(mode = nil) ⇒ Object
Begins a new transaction. Note that nested transactions are not allowed by SQLite, so attempting to nest a transaction will result in a runtime exception.
The mode parameter may be either :deferred, :immediate, or :exclusive. If nil is specified, the default transaction mode, which was passed to #initialize, is used.
If a block is given, the database instance is yielded to it, and the transaction is committed when the block terminates. If the block raises an exception, a rollback will be performed instead. Note that if a block is given, #commit and #rollback should never be called explicitly or you’ll get an error when the block terminates.
If a block is not given, it is the caller’s responsibility to end the transaction explicitly, either by calling #commit, or by calling #rollback.
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 |
# File 'lib/sqlite3/database.rb', line 646 def transaction(mode = nil) mode = @default_transaction_mode if mode.nil? execute "begin #{mode} transaction" if block_given? abort = false begin yield self rescue abort = true raise ensure abort and rollback or commit end else true end end |