Skip to content

Commit 33e46ad

Browse files
committed
fix: tx management
Change-Id: Ib303237acec136dcd4d54453f814cb975c897e30
1 parent b91c835 commit 33e46ad

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

app/Services/UserActionService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function addUserAction($user_id, $ip, $user_action, $realm = null, $user_
5656
{
5757
return $this->tx_service->transaction(function () use ($user_id, $ip, $user_action, $realm, $user_email) {
5858

59+
Log::debug(sprintf("UserActionService::addUserAction user %s action %s ip %s", $user_id, $user_action, $ip));
5960
$action = new UserAction();
6061
$action->setFromIp($ip);
6162
$action->setUserAction($user_action);

app/Services/Utils/DoctrineTransactionService.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,38 +56,36 @@ public function transaction(Closure $callback)
5656
while (!$done and $retry < self::MaxRetries) {
5757
try {
5858
$em = Registry::getManager($this->manager_name);
59-
$con = $em->getConnection();
60-
6159
if (!$em->isOpen()) {
6260
Log::warning("DoctrineTransactionService::transaction: entity manager is closed!, trying to re open...");
6361
$em = Registry::resetManager($this->manager_name);
64-
// new entity manager
65-
$con = $em->getConnection();
6662
}
6763

68-
$con->beginTransaction(); // suspend auto-commit
64+
$em->getConnection()->beginTransaction(); // suspend auto-commit
6965
$result = $callback($this);
7066
$em->flush();
71-
$con->commit();
67+
$em->getConnection()->commit();
7268
$done = true;
73-
} catch (RetryableException $ex) {
74-
Log::warning("retrying ...");
69+
}
70+
catch (RetryableException $ex) {
71+
Log::warning($ex);
72+
Log::warning("DoctrineTransactionService::transaction Retrying ...");
73+
$em->close();
74+
$em->getConnection()->rollBack();
7575
Registry::resetManager($this->manager_name);
76-
$con->rollBack();
7776
Log::warning($ex);
7877
$retry++;
7978
if ($retry === self::MaxRetries) {
80-
$em->close();
81-
$con->rollBack();
82-
Registry::resetManager($this->manager_name);
79+
Log::info(sprintf("DoctrineTransactionService::transaction Max Retry Reached %s", $retry));
8380
throw $ex;
8481
}
85-
} catch (Exception $ex) {
86-
Log::warning("rolling back transaction");
82+
}
83+
catch (Exception $ex) {
84+
Log::warning($ex);
8785
$em->close();
88-
$con->rollBack();
86+
Log::warning("DoctrineTransactionService::transaction rolling back TX");
87+
$em->getConnection()->rollBack();
8988
Registry::resetManager($this->manager_name);
90-
Log::warning($ex);
9189
throw $ex;
9290
}
9391
}

0 commit comments

Comments
 (0)