Ми}{@лbI4

Helloworlder's blog

How to change isolation level by using Doctrine 2

2021-06-15 doctrine, orm, php

Unfortunately, we have not another solution except one: change session isolation level. Mostly we don't need to change isolation level. Though sometimes should do that.

/** @var \Doctrine\ORM\EntityManager $entityManager */

$oldIsolationLevel = $entityManager->getConnection()->getTransactionIsolation();
$entityManager->getConnection()->setTransactionIsolation(\Doctrine\DBAL\TransactionIsolationLevel::SERIALIZABLE);
$entityManager->beginTransaction();
try {
    // do something...
    
    $entityManager->flush();
    $entityManager->commit();
} catch (\Exception $e) {
    $entityManager->rollback();
    throw $e;
} finally {
    $entityManager->getConnection()->setTransactionIsolation($oldIsolationLevel);
}