Nested Transaction using savepoint
這次測試的資料庫是 Oracle 12c
但MySQL Postgre SQL應該都有支援savepoint
這次測試的code有用到doctrine ORM
雖然有測試成功
但其實還是不建議使用Nested Transaction
因為transaction的用意就是達到ACID的效果
結果中間穿插一個 transaction
不就讓外面那層transaction分割開來了嗎?
也就違反了 ACID 的定義
實務上也較難掌握
因為有的被commit有的被rollback
但看起來像包在同個transaction
以下是測試的code
<?php
require_once "bootstrap.php";
$conn = $entityManager->getConnection();
$conn->beginTransaction();
$product = new Product();
$product->setName("product1");
$entityManager->persist($product);
$entityManager->flush();
// begin nested transaction
$conn->createSavepoint("save1");
$product2 = new Product();
$product2->setName("def2");
$entityManager->persist($product2);
$entityManager->flush();
// roll back product2
$conn->rollbackSavepoint("save1");
$conn->commit();
最後的結果就是資料庫裡有product1
但沒有product2
留言