Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented support for persistent connections in PDO and mysqli drivers #3515

Merged
merged 1 commit into from
Apr 23, 2019

Conversation

morozov
Copy link
Member

@morozov morozov commented Apr 14, 2019

Q A
Type improvement
BC Break no
Fixed issues #2315

As of #3080, DBAL doesn't use a custom PDO statement class which enables the use of persistent connections with PDO drivers. Additionally, the support for persistent connections has been implemented for the mysqli driver.

@morozov
Copy link
Member Author

morozov commented Apr 14, 2019

At the moment, I don't see a reliable way to test the persistence of a connection across supported platforms/drivers.

In pseudo-code, a test may look like following:

$conn = connect($persistent = false);
$id1 = $conn->id;

$conn = connect($persistent = true);
$id2 = $conn->id;

assertNotEquals($id1, $id2);

$conn = connect($persistent = true);
$id3 = $conn->id;

assertEquals($id2, $id3);

The results are:

  1. The approach above works for MySQL (SELECT CONNECTION_ID()) and PostgreSQL (SELECT pg_backend_pid()).
  2. SQL Server and Oracle return the same ID even for non-persistent connections (SELECT @@SPID and SELECT SYS_CONTEXT('USERENV', 'SID') FROM DUAL respectively).
  3. I didn't find a way to obtain the connection ID for IBM DB2 and SQL Anywhere.

@Majkl578
Copy link
Contributor

Can we at least test that persistent connection flag is active when persistent parameter is enabled?
For PDO, it can be done using PDO::getAttribute(). For mysqli, it can be done using mysqli::get_connection_stats().

@morozov morozov force-pushed the persistent-connections branch 2 times, most recently from dcf863c to 773bd02 Compare April 14, 2019 21:41
@morozov
Copy link
Member Author

morozov commented Apr 14, 2019

Can we at least test that persistent connection flag is active when persistent parameter is enabled?
For PDO, it can be done using PDO::getAttribute().

Good idea. Done.

For mysqli, it can be done using mysqli::get_connection_stats().

If we use the active_persistent_connections key from there, the test may pass if there is a persistent connection in the server, not necessarily the current one. Let's keep the test skipped for non-PDO drivers.

tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php Outdated Show resolved Hide resolved
@@ -56,6 +56,11 @@ public function __construct(array $params, $username, $password, array $driverOp

$socket = $params['unix_socket'] ?? ini_get('mysqli.default_socket');
$dbname = $params['dbname'] ?? '';
$host = $params['host'];

if (! empty($params['persistent'])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer array_key_exists() over more dangerous ! empty().

Copy link
Member Author

Choose a reason for hiding this comment

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

In this case it's irrelevant. array_key_exists() over a mistyped key will have the same effect.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm thinking of introducing a ConnectionParameters object which will be just a typed structure which will also contain username and password if they are specified. It could avoid optional $username and $pasword in driver/connection methods. Any suggestions/ideas?

Copy link
Member

@jwage jwage left a comment

Choose a reason for hiding this comment

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

Nice! quick and easy win 👍

@morozov morozov merged commit 55bd22c into doctrine:develop Apr 23, 2019
@morozov morozov deleted the persistent-connections branch April 23, 2019 18:08
morozov added a commit that referenced this pull request May 6, 2019
Implemented support for persistent connections in PDO and mysqli drivers
morozov added a commit that referenced this pull request May 23, 2019
Implemented support for persistent connections in PDO and mysqli drivers
@morozov morozov self-assigned this May 23, 2019
morozov added a commit to morozov/dbal that referenced this pull request May 31, 2019
Implemented support for persistent connections in PDO and mysqli drivers
morozov added a commit to morozov/dbal that referenced this pull request May 31, 2019
Implemented support for persistent connections in PDO and mysqli drivers
morozov added a commit that referenced this pull request Jun 13, 2019
Implemented support for persistent connections in PDO and mysqli drivers
morozov added a commit that referenced this pull request Jun 27, 2019
Implemented support for persistent connections in PDO and mysqli drivers
morozov added a commit that referenced this pull request Jun 27, 2019
Implemented support for persistent connections in PDO and mysqli drivers
morozov added a commit that referenced this pull request Jun 27, 2019
Implemented support for persistent connections in PDO and mysqli drivers
@dasmfm
Copy link

dasmfm commented Aug 8, 2019

Hi, @morozov, can you please backport this fix to upcoming 2.9.3 or 2.10.0?
It looks like small, but very useful feature.

@morozov
Copy link
Member Author

morozov commented Aug 9, 2019

No, it depends on #3080 which is a breaking change.

morozov added a commit to morozov/dbal that referenced this pull request Aug 26, 2019
Implemented support for persistent connections in PDO and mysqli drivers
morozov added a commit to morozov/dbal that referenced this pull request Aug 27, 2019
Implemented support for persistent connections in PDO and mysqli drivers
morozov added a commit that referenced this pull request Nov 2, 2019
Implemented support for persistent connections in PDO and mysqli drivers
@morozov morozov modified the milestones: 4.0.0, 3.0.0 Nov 17, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants