Deadlock handling

at 2009-06-16 in ExamplesDatabasesRFCs by friebe (0 comments)

DeadlockThe changes implemented in RFC #1059 make handling deadlocks easier. During deadlocks you might want to your program behave differently than during "regular" statement failures. For example, in Sybase, all transactions, regardless of their nesting level, are rolled back during a deadlock!

You may have written sourcecode as follows in the past:

<?php 
try
{
// ...
} catch (SQLStatementFailedException $e) {
if
(1205 == $e->getErrorcode()) {
// Deadlock
}
}
?>
The problems should be obvious to the reader: The hardcoded errorcode (here the relevant one for Sybase), the very unelegant if, the missing portability for other drivers (PostgreSQL for example).



The XP Framework now offers a way to handle deadlocks independent of the RDBMS you are accessing. We've introduced a new exception - rdbms.SQLDeadlockException, which will be thrown by the respective implementations.

Example (new functionality)
<?php 
$conn= DriverManager::getConnection('sybase://...');

try
{
$tran= $conn->begin(new Transaction('update'));
$conn->query('...');
$conn->query('...');
} catch (SQLDeadlockException $e) {
// Handle deadlock
} catch (SQLException $e) {
$tran && $tran->rollback();
// Handle other situations
}
?>

Note: The old code from above will still work - as SQLDeadlockException is a subclass of SQLStatementFailedException !



Subscribe

You can subscribe to the XP framework's news by using RSS syndication.


Categories

News
General
PHP5
Announcements
RFCs
Further reading
Examples
Editorial
EASC
Experiments
Unittests
Databases
5.8-SERIES

Related

Find related articles by a search for «Deadlock».