CodeIgniter4 icon indicating copy to clipboard operation
CodeIgniter4 copied to clipboard

Bug: TypeError in pg_ping()

Open warcooft opened this issue 1 year ago • 7 comments

PHP Version

8.3

CodeIgniter4 Version

4.5.3

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

macOS

Which server did you use?

apache

Database

PostgreSQL 16.3

What happened?

pg_ping() expects the first parameter to be a PostgreSQL connection (or null), but receives the boolean value false.

See also https://github.com/codeigniter4/queue/issues/45#issue-2401691847

This is a blocker for other packages.

Screenshot 2024-07-11 at 05 47 49

Steps to Reproduce

$db = db_connect();
d($db->connect());
d($db->close());
d($db->reconnect());
d($db->getDatabase());
d($db->getVersion());
dd();

Expected Output

The output should show the db name and db driver version.

Anything else?

No response

warcooft avatar Jul 10 '24 22:07 warcooft

it looks like the close() function also doesn't work when using the PostgresSQL driver:

Screenshot 2024-07-11 at 07 24 15

  this when using MySQL:

Screenshot 2024-07-11 at 07 25 22

Steps to Reproduce

$db = db_connect();
d($db);
// d($db->connect());
d($db->close());
// d($db->reconnect());
d($db->getPlatform());
d($db->getVersion());
dd();

warcooft avatar Jul 11 '24 00:07 warcooft

and both connections cannot be closed if before closing we run connect() as follows:

$db = db_connect();
d($db);
d($db->connect());
d($db->close());
// d($db->reconnect());
d($db->getPlatform());
d($db->getVersion());
dd();

warcooft avatar Jul 11 '24 00:07 warcooft

I can reproduce the TypeError in the original issue.

paulbalandan avatar Jul 23 '24 16:07 paulbalandan

Dumb question: if you call reconnect() on a closed DB connection, should you initialize again? In the Postgres driver, you only call pg_ping() which just pings the connection if still active and tries to reconnect if broken. For MySQL driver, it explicitly closes then reinitializes the connection. Clearly a difference in behaviors, I suppose.

paulbalandan avatar Jul 23 '24 16:07 paulbalandan

For MySQL driver, it explicitly closes then reinitializes the connection. Clearly a difference in behaviors.

I'm not sure if the way used to produce the error above is correct. I think both have the same behavior. thankyou for enlighten me.

warcooft avatar Jul 24 '24 15:07 warcooft

The behavior of reconnect() is to keep the connection alive or re-establish it.

If the database server’s idle timeout is exceeded while you’re doing some heavy PHP lifting (processing an image, for instance), you should consider pinging the server by using the reconnect() method before sending further queries, which can gracefully keep the connection alive or re-establish it. https://codeigniter4.github.io/CodeIgniter4/database/connecting.html#reconnecting-keeping-the-connection-alive

So if there is a ping method in the DB module, we should use it.

But in MySQL, ping() is no longer working. See #462

kenjis avatar Jul 25 '24 01:07 kenjis

That helps! thanks for explaining.

warcooft avatar Jul 25 '24 18:07 warcooft