db_transaction (public)
db_transaction [ -dbn dbn ] transaction_code [ args... ]
Defined in packages/acs-tcl/tcl/01-database-procs.tcl
Usage: db_transaction transaction_code [ on_error { error_code_block } ] Executes transaction_code with transactional semantics. This means that either all of the database commands within transaction_code are committed to the database or none of them are. Multiple
db_transaction
s may be nested (end transaction is transparently ns_db dml'ed when the outermost transaction completes).To handle errors, use
db_transaction {transaction_code} on_error {error_code_block}
. Any error generated intransaction_code
will be caught automatically and process control will transfer toerror_code_block
with a variableerrmsg
set. The error_code block can then clean up after the error, such as presenting a usable error message to the user. Following the execution oferror_code_block
the transaction will be aborted. If you want to explicitly abort the transaction, calldb_abort_transaction
from within the transaction_code block or the error_code block.Example 1:
In this example, db_dml triggers an error, so control passes to the on_error block which prints a readable error.db_transaction { db_dml test "nonsense" } on_error { ad_return_error "Error in blah/foo/bar" "The error was: $errmsg" }Example 2:
In this example, the second command, "nonsense" triggers an error. There is no on_error block, so the transaction is immediately halted and aborted.db_transaction { db_dml test {insert into footest values(1)} nonsense db_dml test {insert into footest values(2)} }
- Switches:
- -dbn (optional)
- The database name to use. If empty_string, uses the default database.
- Parameters:
- transaction_code (required)
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- db__transaction, db__transaction_bug_3440