diff --git a/CHANGELOG-5.0.md b/CHANGELOG-5.0.md index cf596e7279c..3168559bc11 100644 --- a/CHANGELOG-5.0.md +++ b/CHANGELOG-5.0.md @@ -36,6 +36,8 @@ - Fixed `Phalcon\Support\Helper\Json\Encode` to prefix the `InvalidArgumentException` message with `"json_encode error: "` for consistency [#16889](https://github.com/phalcon/cphalcon/issues/16889) - Fixed `Phalcon\Storage\Adapter\Libmemcached`, `Phalcon\Storage\Adapter\Redis` and `Phalcon\Storage\Adapter\Weak` to call `initSerializer()` during construction [#16889](https://github.com/phalcon/cphalcon/issues/16889) - Fixed `Phalcon\Storage\Adapter\Redis` to initialize `lifetime` from options during construction [#16889](https://github.com/phalcon/cphalcon/issues/16889) +- Fixed `Phalcon\Mvc\Model\Transaction\Manager::commit()` to remove each transaction from the pool after committing so that subsequent `get()` calls return a fresh transaction [#16522](https://github.com/phalcon/cphalcon/issues/16522) +- Fixed `Phalcon\Mvc\Model\Transaction\Manager::collectTransaction()` to keep the correct transactions when rebuilding the list after removal [#16522](https://github.com/phalcon/cphalcon/issues/16522) ### Removed diff --git a/phalcon/Mvc/Model/Transaction/Manager.zep b/phalcon/Mvc/Model/Transaction/Manager.zep index 4f2ce4c03c3..94d1abb571f 100644 --- a/phalcon/Mvc/Model/Transaction/Manager.zep +++ b/phalcon/Mvc/Model/Transaction/Manager.zep @@ -146,6 +146,8 @@ class Manager implements ManagerInterface, InjectionAwareInterface if connection->isUnderTransaction() { connection->commit(); } + + this->collectTransaction(transaction); } } @@ -350,7 +352,7 @@ class Manager implements ManagerInterface, InjectionAwareInterface for managedTransaction in this->transactions { if managedTransaction != transaction { - let newTransactions[] = transaction; + let newTransactions[] = managedTransaction; } else { let this->number--; } diff --git a/tests/database/Mvc/Model/Transaction/ManagerTest.php b/tests/database/Mvc/Model/Transaction/ManagerTest.php index 96c6053ef44..301fbf3ba4b 100644 --- a/tests/database/Mvc/Model/Transaction/ManagerTest.php +++ b/tests/database/Mvc/Model/Transaction/ManagerTest.php @@ -136,6 +136,76 @@ public function testMvcModelTransactionManagerTransactionRemovedOnCommit(): void ); } + /** + * @author Phalcon Team + * @since 2026-04-21 + * + * @group mysql + * @group pgsql + */ + public function testMvcModelTransactionManagerCommitViaManagerClearsTransaction(): void + { + $tm = $this->container->getShared('transactionManager'); + + $transaction = $tm->get(); + + $select = new Select(); + $select->setTransaction($transaction); + $select->assign(['sel_name' => 'Test One']); + $select->create(); + + $this->assertSame(1, $this->getProtectedProperty($tm, 'number')); + $this->assertCount(1, $this->getProtectedProperty($tm, 'transactions')); + + $tm->commit(); + + $this->assertSame(0, $this->getProtectedProperty($tm, 'number')); + $this->assertCount(0, $this->getProtectedProperty($tm, 'transactions')); + + $newTransaction = $tm->get(); + + $this->assertTrue($newTransaction->isValid()); + $this->assertNotSame($transaction, $newTransaction); + } + + /** + * @author Phalcon Team + * @since 2026-04-21 + * + * @group mysql + * @group pgsql + */ + public function testMvcModelTransactionManagerCommitRollbackCycle(): void + { + $tm = $this->container->getShared('transactionManager'); + $countBefore = Select::count(); + + $transaction = $tm->get(); + + $select = new Select(); + $select->setTransaction($transaction); + $select->assign(['sel_name' => 'Cycle One']); + $select->create(); + + $tm->commit(); + + $countAfterCommit = Select::count(); + $this->assertSame($countBefore + 1, $countAfterCommit); + + $transaction = $tm->get(); + + $select = new Select(); + $select->setTransaction($transaction); + $select->assign(['sel_name' => 'Cycle Two']); + $select->create(); + + $tm->rollback(); + + $stmt = self::getConnection()->query('SELECT COUNT(*) FROM ph_select'); + $countAfterRollback = (int) $stmt->fetchColumn(); + $this->assertSame($countAfterCommit, $countAfterRollback); + } + /** * @author Phalcon Team * @since 2012-08-07