0

I wonder if f3 has the concept of DB connection pool. When I check the sample code of CMS, it seems the DB connection is created in the controller's construct.

function __construct() {
    $f3=Base::instance();
    // Connect to the database
    $db=new DB\SQL($f3->get('db'));

So each time there is a request to any controller, there is a new DB connection created. If the server is under huge load, this may cause DB to be saturated.

Does f3 (or any plug-in) supports DB connection pool so that we can limit the # of DB connections? Requests are put into a queue when the number of DB connection exceeds the pool size.

NHG
  • 35
  • 4

1 Answers1

0

It is not mandatory to create the DB connection in the controller as this is only a simple example. I would advise against it and would propose to use at least service location or - even better - dependency injection.

F3 has no pool management. If I am not wrong there is no pool management in PHP. It could be possible that there is a PECL module which adds this feature. Requests are usually (PHP-FPM, FCGI, mod_php) answered by clean PHP environments (except some details like apcu, obcache or sessions).

Further information

  • It should be possible to use persistent connections by setting PDO::ATTR_PERSISTENT, but there are some unpleasant drawbacks. Details are available in this SO question: What are the disadvantages of using persistent connection in PDO

  • You could implicitly limit the number of connections by limiting the number of concurrently working PHP workers.

Rayne
  • 2,620
  • 20
  • 28
  • Thanks Rayne. Due to the nature of PHP, it may be better to create the DB connection using singleton and share it throughout the request chain. But we still need one DB connection per request at least. – NHG Sep 07 '18 at 07:06