We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Shouldn't the DB adapter be a shared service?

In all examples I see the database adapter set as a non-shared service. Like this:

$di->set("db", function() use ($config) {
    return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
         "host" => $config->host,
         "username" => $config->username,
         "password" => $config->password,
         "dbname" => $config->name
    ));
});

Wouldn't it be better as a shared service ?

I knew I saw it in the docs once before. It took me a second to find it:

https://docs.phalcon.io/en/latest/reference/di.html#service-name-conventions

Db and several other services default to shared.



32.2k

Does it mean Phalcon\DI\FactoryDefault will set a service using the 'db' identifier as shared no matter what ?



98.9k
Accepted
answer
edited Jul '16

If you are using isolated transactions you have to set the service as non-shared as it needs it to create an isolated transaction, in any other case setting the service as shared will be a safe decision.

https://docs.phalcon.io/en/latest/reference/model-transactions.html#isolated-transactions



32.2k
edited Jul '16

Could you please verify your answer? I'm not sure where you meant to write "non-shared" vs "shared".

If you are using isolated transactions you have to set the service as shared as it needs it to create an isolated transaction, in any other case setting the service as shared will be a safe decision.

https://docs.phalcon.io/en/latest/reference/model-transactions.html#isolated-transactions



98.9k

I have edited it :)

This link https://docs.phalcon.io/en/latest/reference/models.html#isolated-transactions is no longer valid as whatever isolated-transaction section that was once there is no longer referenced. I assume this is now the correct link: https://docs.phalcon.io/en/latest/reference/model-transactions.html#isolated-transactions