0

I'm new to Silex/PHP. I have the following snippet in my index.php.

$app->register(new \Silex\Provider\DoctrineServiceProvider(), array(
  'db.options' => $dbConfig
));

dbConfig - is configurable (pdo_sqlite or pdo_mysql). Silex is run under lighttpd with php-fpm.

I have following questions:

  1. Does Silex create "new" database connection for every request? If so, how does one optimize for performance so old/existing connection is used?
  2. If new connection is not made, then how can one change the existing connection from one to other for the particular "app" instance when the database configuration changes? In this case, please assume that "app" is the one that process the configuration change and gets to know about it.

Thanks in advance.

  • Yes, each PHP execution is created and destroyed with every request (unlike, Java for example). Check [this answer.](https://stackoverflow.com/questions/17560717/why-does-php-create-a-new-instance-of-a-singleton-each-time-the-script-calling-f?rq=1) – mTorres Aug 24 '17 at 10:17
  • @mTorres, Thank you. Setting up database connection for each request would impact performance? How does one typically optimize that? – Sudhagar Chinnaswamy Aug 24 '17 at 17:26
  • I really don't know as my knowledge is limited in this regard. I've always used the default MYSQL/MYSQLi/PDO connection without problems. My web applications does not need to worry about this because they don't need to handle a big load. If that's your case, I would recommend to stick with PDO connections, otherwise, ask another question :-) – mTorres Aug 24 '17 at 17:42
  • I would appreciate if you have some pointers on how to (re-)use PDO connections using Silex framework, so that new database connection is avoided for every request. [PS: I'm using Silex under lighttpd/php-fpm.] – Sudhagar Chinnaswamy Aug 25 '17 at 00:21
  • The easiest way would be to use a a Silex Service Provider for a PDO service. You can find some by [googling](https://www.google.com/search?q=silex+pdo+service+provider), for example this [one](https://github.com/csanquer/PdoServiceProvider). Keep in mind that this would recreate the connection for every request. If you want to keep the same connection between executions ([you probably won't](https://stackoverflow.com/questions/50303/persistent-db-connections-yea-or-nay)) you have to look for persistent connections in the [PHP manual](http://php.net/manual/en/pdo.connections.php). – mTorres Aug 25 '17 at 06:31
  • @mTorres, Thanks. Let me check the pointers. – Sudhagar Chinnaswamy Aug 28 '17 at 22:00

0 Answers0