Doctrine exception – there is no active transacion

Notice: This thread is very old.
Čamo
Member | 798
+
0
-

Hi,
can you tell me please what should I do to use Doctrine transactions or where is the mistake in code below, which throws me an exception – “There is no active transaction”.

try {
			$this->em->getConnection()->beginTransaction();

			$basket = $this->basketFacade->getBasket();
			$user = $this->user->id ? $this->user->identity : NULL;
			$order = $this->orderFacade->createFromBasket($basket, $user);
			$this->basketFacade->clearBasket();

			$this->getSessionSection()->orderId = $order->id;

			$this->em->getConnection()->commit();

			$this->redirect('done');
		} catch (ItemsIsntOnStockException $ex) {
			$this->redirect('default');
		} catch ( \Exception $ex ) {
			$this->em->getConnection()->rollBack();
			$this->em->close();
			$this->flashMessage( $ex->getMessage(), 'warning' );
			$this->redirect('default');
		}

Both commit and rollback throws me an exception.

Last edited by Čamo (2015-11-20 21:36)

abc
Member | 92
+
0
-

Hi, what are your methods in basketFacade and orderFacade doing?
If there is a flush there also is a commit and after there really is no transaction…

Filip Procházka
Moderator | 4668
+
0
-

You should rather be using ORM\EntityManager::transactional() method. It calls flush at the end, so if you don't want that, use Dbal\Connection::transactional()

try {
    // or $this->connection->transactional(function () {
	$this->em->transactional(function () {
		$basket = $this->basketFacade->getBasket();
		$user = $this->user->id ? $this->user->identity : NULL;
		$order = $this->orderFacade->createFromBasket($basket, $user);
		$this->basketFacade->clearBasket();
		$this->getSessionSection()->orderId = $order->id;
	});
	$this->redirect('done');

} catch (ItemsIsntOnStockException $ex) {
	$this->redirect('default');
} catch ( \Exception $ex ) {
	$this->flashMessage( $ex->getMessage(), 'warning' );
	$this->redirect('default');
}
Čamo
Member | 798
+
0
-

The problem was not in Doctrine transaction, but in

$this->redirect('done');

cause it throws an abort exception what causes a problem in try block. When i removed it from try block it started to work.

PS: Does someone know why netteAbortException in UI\Presenter::terminate doesn't have any message? It would save me a little time.

Thanks for help.

Last edited by Čamo (2015-11-25 22:56)