10.04.2018 Views

Doctrine_manual-1-2-en

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 21: Transactions 307<br />

try {<br />

$conn->beginTransaction();<br />

// do some operations here<br />

// creates a new savepoint called mysavepoint<br />

$conn->beginTransaction('mysavepoint');<br />

// do some operations here<br />

$conn->commit(); // deletes all savepoints<br />

} catch(Exception $e) {<br />

$conn->rollback(); // deletes all savepoints<br />

}<br />

Isolation Levels<br />

A transaction isolation level sets the default transactional behavior. As the name 'isolation<br />

level' suggests, the setting determines how isolated each transation is, or what kind of locks<br />

are associated with queries inside a transaction. The four available levels are (in asc<strong>en</strong>ding<br />

order of strictness):<br />

READ UNCOMMITTED<br />

Barely transactional, this setting allows for so-called 'dirty reads', where queries inside<br />

one transaction are affected by uncommitted changes in another transaction.<br />

READ COMMITTED<br />

Committed updates are visible within another transaction. This means id<strong>en</strong>tical queries<br />

within a transaction can return differing results. This is the default in some DBMS's.<br />

REPEATABLE READ<br />

Within a transaction, all reads are consist<strong>en</strong>t. This is the default of Mysql INNODB<br />

<strong>en</strong>gine.<br />

SERIALIZABLE<br />

Updates are not permitted in other transactions if a transaction has run an ordinary<br />

SELECT query.<br />

To get the transaction module use the following code:<br />

$tx = $conn->transaction;<br />

Listing<br />

21-8<br />

Set the isolation level to READ COMMITTED:<br />

$tx->setIsolation('READ COMMITTED');<br />

Listing<br />

21-9<br />

Set the isolation level to SERIALIZABLE:<br />

$tx->setIsolation('SERIALIZABLE');<br />

Listing<br />

21-10<br />

Some drivers (like Mysql) support the fetching of curr<strong>en</strong>t transaction isolation level. It can<br />

be done as follows:<br />

$level = $tx->getIsolation();<br />

Listing<br />

21-11<br />

----------------- Brought to you by

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!