Closed Draghmar closed 3 years ago
try to set max_execution_time = 0 in php.ini
max_execution_time
is set to 0 for CLI, so there's no need for that. Even if it would be set to something else, it would stop the server altogether, not the DB connection alone. MariaDB is closing connection because there was no transfer from Swoole server - connections in pool aren't used on my test server so often. I could bump equivalent in MariaDB but that's not the solution I'm looking for. ;)
Try to reconnect to the database.
for ($i = 0; $i < 2; $i++) {
$result = $db->query($sql);
if ($result === false) {
if ($db->errno == 2013 or $db->errno == 2006 or ($db->errno == 0 and !$db->ping())) {
$db = $dbpool->get();
if ($r === true) {
continue;
}
} else {
return false;
}
}
break;
}
The best way to deal with this is having PDOProxy
from swoole take care of the exceptions and reconnect itself.
Just add following to the PDOConfig
:
->withOptions([
PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING
])
This will still throw exceptions, but after the proxy classes have done its work ππ»
@matyhtf This should probably be part of the documentation or set by default, otherwise if the exception is thrown here the remaining code is never reached.
What did you do? If possible, provide a simple script for reproducing the error. I'm trying to use Swoole as a API server. At the moment it works great but has one issue that I can't find workaround for. Basically I have DB connection through PDO to the MariaDB and some stuff that works with data. This is simplified code that I'm playing with:
What did you expect to see? Properly working code, with working DB connection. ;) I think
ConnectionPool
should have some way to auto reconnect if connection is dead. I tried to do something about that but I couldn't get it to work as I intended:Unfortunetly PDO doesn't have built-in way to detect connection status. No ping or anything like that. At least nothing I'm aware of. :( I'm still testing the code above to find a way to reconnect on failed connection.
What did you see instead? If I leave Swoole running along with MariaDB and there won't be enough connections during that time, MariaDB will timeout connections from connection pool with no mean to reconnect, with one of the errors:
and Swoole outputs:
What version of Swoole are you using (show your
php --ri swoole
)?Directive => Local Value => Master Value swoole.enable_coroutine => On => On swoole.enable_library => On => On swoole.enable_preemptive_scheduler => Off => Off swoole.display_errors => On => On swoole.use_shortname => On => On swoole.unixsock_buffer_size => 8388608 => 8388608
Linux 5.10.12-arch1-1 #1 SMP PREEMPT Sun, 31 Jan 2021 00:41:06 +0000 x86_64 GNU/Linux
PHP 8.0.3 (cli) (built: Mar 4 2021 05:33:14) ( NTS ) Copyright (c) The PHP Group
Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --with-isl --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-install-libiberty --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-libunwind-exceptions --disable-werror gdc_include_dir=/usr/include/dlang/gdc Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 10.2.0 (GCC)