Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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:

http://docs.phalconphp.com/en/latest/reference/di.html#service-name-conventions

Db and several other services default to shared.



21.4k

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



85.3k
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.phalconphp.com/en/latest/reference/model-transactions.html#isolated-transactions



21.4k
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.phalconphp.com/en/latest/reference/model-transactions.html#isolated-transactions



85.3k

I have edited it :)

This link http://docs.phalconphp.com/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.phalconphp.com/en/latest/reference/model-transactions.html#isolated-transactions



48.2k

I updated all of the links.