use Doctrine\DBAL\DriverManager; // create a new database connection $conn = DriverManager::getConnection([ 'dbname' => 'myDatabase', 'user' => 'myUsername', 'password' => 'myPassword', 'host' => 'localhost', 'driver' => 'pdo_mysql', ]); // start the transaction $conn->beginTransaction(); try { // perform database operations // commit changes to the database $conn->commit(); } catch (Exception $e) { // rollback to the beginning of the transaction $conn->rollBack(); // handle the exception }
use Doctrine\DBAL\Connection; // rollback the transaction to a specific save point $conn->beginTransaction(); $conn->savepoint('sp1'); try { // perform some database operations // create a new save point $conn->savepoint('sp2'); // perform more database operations // commit the transaction $conn->commit(); } catch (Exception $e) { // rollback to save point sp1 $conn->rollBack('sp1'); // handle the exception }In this example, we start a transaction and create a save point called `sp1`. We perform some database operations and create another save point called `sp2`. If an exception occurs, we roll back to the `sp1` save point and undo the changes made after that point. The Doctrine DBAL Connection rollback method is part of the Doctrine DBAL package library.