Skip to content

Commit 13c9cb5

Browse files
committed
fix: improve tx manager reconnect strategy
Change-Id: I85a1001bf6940c0fa8306705c36caaa848a6d119
1 parent ae24738 commit 13c9cb5

File tree

1 file changed

+68
-8
lines changed

1 file changed

+68
-8
lines changed

app/Services/Utils/DoctrineTransactionService.php

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
**/
14-
14+
use Doctrine\DBAL\Exception\ConnectionException;
1515
use Doctrine\DBAL\Exception\ConnectionLost;
1616
use Doctrine\DBAL\TransactionIsolationLevel;
1717
use Illuminate\Support\Facades\Log;
1818
use Closure;
1919
use LaravelDoctrine\ORM\Facades\Registry;
2020
use Doctrine\DBAL\Exception\RetryableException;
2121
use Exception;
22+
use ErrorException;
2223
use Utils\Db\ITransactionService;
23-
2424
/**
2525
* Class DoctrineTransactionService
2626
* @package App\Services\Utils
@@ -32,7 +32,7 @@ final class DoctrineTransactionService implements ITransactionService
3232
*/
3333
private $manager_name;
3434

35-
const MaxRetries = 3;
35+
const MaxRetries = 10;
3636

3737
/**
3838
* DoctrineTransactionService constructor.
@@ -49,18 +49,78 @@ public function __construct($manager_name)
4949
*/
5050
public function shouldReconnect(\Exception $e):bool
5151
{
52-
if($e instanceof RetryableException) return true;
53-
if($e instanceof ConnectionLost) return true;
52+
Log::debug
53+
(
54+
sprintf
55+
(
56+
"DoctrineTransactionService::shouldReconnect %s code %s message %s",
57+
get_class($e),
58+
$e->getCode(),
59+
$e->getMessage()
60+
)
61+
);
62+
if($e instanceof ErrorException && str_contains($e->getMessage(), "Packets out of order")){
63+
Log::debug
64+
(
65+
sprintf
66+
(
67+
"DoctrineTransactionService::shouldReconnect %s Packets out of order true",
68+
get_class($e),
69+
)
70+
);
71+
return true;
72+
}
73+
if($e instanceof RetryableException) {
74+
Log::debug
75+
(
76+
sprintf
77+
(
78+
"DoctrineTransactionService::shouldReconnect %s true",
79+
get_class($e),
80+
)
81+
);
82+
return true;
83+
}
84+
if($e instanceof ConnectionLost) {
85+
Log::debug
86+
(
87+
sprintf
88+
(
89+
"DoctrineTransactionService::shouldReconnect %s true",
90+
get_class($e),
91+
)
92+
);
93+
return true;
94+
}
95+
if($e instanceof ConnectionException) {
96+
Log::debug
97+
(
98+
sprintf
99+
(
100+
"DoctrineTransactionService::shouldReconnect %s true",
101+
get_class($e),
102+
)
103+
);
104+
return true;
105+
}
54106
if($e instanceof \PDOException){
55107
switch(intval($e->getCode())){
56108
case 2006:
57-
Log::warning("DoctrineTransactionService::shouldReconnect: MySQL server has gone away!");
109+
Log::warning("DoctrineTransactionService::shouldReconnect: MySQL server has gone away true");
58110
return true;
59111
case 2002:
60-
Log::warning("DoctrineTransactionService::shouldReconnect: php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known!");
112+
Log::warning("DoctrineTransactionService::shouldReconnect: php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known true");
61113
return true;
62114
}
63115
}
116+
Log::debug
117+
(
118+
sprintf
119+
(
120+
"DoctrineTransactionService::shouldReconnect %s false",
121+
get_class($e),
122+
)
123+
);
64124
return false;
65125
}
66126

@@ -117,7 +177,7 @@ public function transaction(Closure $callback, int $isolationLevel = Transactio
117177
continue;
118178
}
119179
Log::warning("DoctrineTransactionService::transaction rolling back TX");
120-
Log::error($ex);
180+
Log::warning($ex);
121181
throw $ex;
122182
}
123183
}

0 commit comments

Comments
 (0)