| ADOdb Library for PHP Manual | ||
|---|---|---|
| Prev | Smart Transactions |
Next |
The old way of doing transactions required you to use
$conn->BeginTrans();
$ok =
$conn->Execute($sql);
if ($ok)
$ok = $conn->Execute($sql2);
if (!$ok)
$conn->RollbackTrans();
else $conn->CommitTrans();
This is very complicated for large projects because you have to track the error
status. Smart Transactions is much simpler. You start a smart transaction by
calling StartTrans():
$conn->StartTrans();
$conn->Execute($sql);
$conn->Execute($Sql2);
$conn->CompleteTrans();
CompleteTrans() detects when an SQL error occurs, and will Rollback/Commit as appropriate. To specificly force a rollback even if no error occured, use FailTrans(). Note that the rollback is done in CompleteTrans(), and not in FailTrans().
You can also check if a transaction has failed, using HasFailedTrans(), which
returns true if FailTrans() was called, or there was an error in the SQL execution.
Make sure you call HasFailedTrans() before you call CompleteTrans(), as it is
only works between StartTrans/CompleteTrans.
$conn->StartTrans();
$conn->Execute($sql);
if (!CheckRecords())
$conn->FailTrans();
$conn->Execute($Sql2);
$conn->CompleteTrans();
Lastly, StartTrans/CompleteTrans is nestable, and only the outermost block
is executed. In contrast, BeginTrans/CommitTrans/RollbackTrans is NOT nestable.
$conn->StartTrans();
$conn->Execute($sql);
$conn->StartTrans();
# ignored
if (!CheckRecords())
$conn->FailTrans();
$conn->CompleteTrans();
# ignored
$conn->Execute($Sql2);
$conn->CompleteTrans();
Note: Savepoints are currently not supported.
| Prev | Home | Next |
| Recordset Filters | Up | Using Custom Error Handlers and PEAR_Error |