Search code examples
phppdozend-db

set isolation level via PDO or Zend_Db_Adapter_Abstract?


Does there is way to set isolation level independently to concrete database driver in php? Looks like there is no.


Solution

  • Unfortunately, I'm not sure there is a way to do such a thing with Zend_Db. The manual says that setTransactionIsolationLevel() can be used only with the Microsoft SQL Server adapter.

    You can use setTransactionIsolationLevel() to set isolation level for current connection. The value can be SQLSRV_TXN_READ_UNCOMMITTED, SQLSRV_TXN_READ_COMMITTED, SQLSRV_TXN_REPEATABLE_READ, SQLSRV_TXN_SNAPSHOT or SQLSRV_TXN_SERIALIZABLE.

    It doesn't seem that the Zend_Db API implements a way to manage transaction isolation levels, the only way would be to write your own SQL statements and execute them according the the PDO driver you are using.

    I would recommend you instead to use a better database abstraction layer such as Doctrine (really powerful). Then you would be able to do it this way:

    The Doctrine\DBAL\Connection also has methods to control the transaction isolation level as supported by the underlying database. Connection#setTransactionIsolation($level) and Connection#getTransactionIsolation() can be used for that purpose. The possible isolation levels are represented by the following constants:

    <?php
    Connection::TRANSACTION_READ_UNCOMMITTED
    Connection::TRANSACTION_READ_COMMITTED
    Connection::TRANSACTION_REPEATABLE_READ
    Connection::TRANSACTION_SERIALIZABLE
    

    See this question to know how to integrate Doctrine with ZF and this page for more information about transaction management using Doctrine.