Skip to content

Fix setting persistent option for PDO connection#2092

Merged
MasterOdin merged 9 commits intocakephp:masterfrom
paulermo:feature/pdo-persistent-connection
Jul 4, 2022
Merged

Fix setting persistent option for PDO connection#2092
MasterOdin merged 9 commits intocakephp:masterfrom
paulermo:feature/pdo-persistent-connection

Conversation

@paulermo
Copy link
Copy Markdown
Contributor

@paulermo paulermo commented May 10, 2022

This fixes the ability to use persistent connections by PDO.
One can set generic PDO attributes (#1904) but the attribute - \PDO::ATTR_PERSISTENT - is useless while setting it by setAttribute after PDO object instantiation:

Note:
If you wish to use persistent connections, you must set PDO::ATTR_PERSISTENT in the array of driver options passed to the PDO constructor. If setting this attribute with PDO::setAttribute() after instantiation of the object, the driver will not use persistent connections.
https://www.php.net/manual/en/pdo.connections.php

From the point of view of usage, nothing changes: like other PDO arguments we specify attr_persistent => true config option. Before the PDO instatiation this attribute (if specified) is passed to driver options instead of setting in setAttribute after it.

It's done in Mysql, Postgres and SQLite adapters. SQL Server PDO driver does not support this specific attribute.

Comment thread tests/Phinx/Db/Adapter/MysqlAdapterTest.php Outdated
Comment thread tests/Phinx/Db/Adapter/PostgresAdapterTest.php Outdated
Comment thread tests/Phinx/Db/Adapter/SQLiteAdapterTest.php Outdated
Comment thread tests/Phinx/Db/Adapter/MysqlAdapterTest.php Outdated
Comment thread tests/Phinx/Db/Adapter/PostgresAdapterTest.php Outdated
Comment thread tests/Phinx/Db/Adapter/SQLiteAdapterTest.php Outdated
paulermo and others added 2 commits May 11, 2022 12:40
@paulermo paulermo requested a review from MasterOdin May 11, 2022 10:50
Copy link
Copy Markdown
Member

@MasterOdin MasterOdin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does attr_persistent work for SQL Server? If not, would there be any harm in passing it as an option anyway?

If either of the above is "yes", I think it would make sense to lift the code setting this option out of the specific adapters into the PdoAdapter::createPdoConnection method, having it be:

        $adapterOptions = $this->getOptions() + [
            'attr_errmode' => PDO::ERRMODE_EXCEPTION,
        ];
        if (isset($adapterOptions['attr_persistent'])) {
          $driverOptions[PDO::ATTR_PERSISTENT] = $adapterOptions['attr_persistent'];
        }

This way we are following DRY principles.

@paulermo
Copy link
Copy Markdown
Contributor Author

Does attr_persistent work for SQL Server? If not, would there be any harm in passing it as an option anyway?

No, SQL Server does not support persistent connections. And that's why I have moved the setting of driver attribute out from PDOAdapter to specific MySQL, Postgres and SQLite. For SQL Server nothing changes in comparison to current behavior.

This way we are following DRY principles.

Yes, that was my first solution (7a49fa2), but I have found that it could break SQL server functionality and moved it out in 2b86062.

@paulermo paulermo requested a review from MasterOdin June 6, 2022 11:51
@MasterOdin
Copy link
Copy Markdown
Member

Alright, I found microsoft/msphpsql#65 which confirms that setting that will in fact cause the driver to throw an exception, so we cannot set it for all drivers. I'm fine with just ignoring it for mssql as opposed to throwing an exception like cakephp/database does.

@MasterOdin MasterOdin changed the title support PDO persistent connection Fix setting persistent option for PDO connection Jul 4, 2022
@MasterOdin MasterOdin merged commit 11ec6ee into cakephp:master Jul 4, 2022
@dereuromark
Copy link
Copy Markdown
Member

I think we need to merge this master branch into 0.x and then stop using master (as default).
@markstory Do you have access to change default branch?

@othercorey
Copy link
Copy Markdown
Contributor

We have a small discussions on this in the phinx-dev channel. Leaving it up to @markstory still, but @MasterOdin had some thoughts after I explained the naming alignment.

@MasterOdin
Copy link
Copy Markdown
Member

I've opened #2097 to discuss the branch switch and can carry this conversation there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants