-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
pdo_mysql with persistent connection #2315
Comments
Passing in a PDO instance is the correct way to handle edge cases in the configuration DSL of the |
Yes, but do you need a pdo instance configured to work with |
@jeff1985 doctrine wraps around a number of drivers, and many of them are not PDO-based, so we built internal abstractions to get rid of that impedance.
|
@Ocramius You write, that So the actual question is will using the plain |
You have to test that against doctrine's test suite. Doctrine sets the statement in I didn't know this:
Is there a documentation about it somewhere? Sounds like a weird issue... |
This is not true. We are speaking of a workaround here. PDOConnection is created by PDOMySql\Driver::connect(). The connect() method won't get called, if you've provided a ready pdo instance.
It is documented in the php manual:
|
👍 Can't use persistent connection in Doctrine :( |
@mikemix |
I introduced a better solution in my app and used the tick() method in my Ratchet server which pings the connection from time to time. |
@mikemix |
I am using a Doctrine connection in a really long running socket app. |
@mikemix |
After some time the server was useless because the connection to the database was lost? |
@mikemix |
Prove me wrong I thought this topic was about making a persistent connection to the database with Doctrine. |
@mikemix |
the problem seems to be this line: https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Driver/PDOConnection.php#L44 but I don't know if this is required to work, else something like this could fix it: if (!isset($options[PDO::ATTR_PERSISTENT]) || !$options[PDO::ATTR_PERSISTENT]) {
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('Doctrine\DBAL\Driver\PDOStatement', array()));
} |
Doctrine DBAL currently does not work with persistent connections. doctrine/dbal#2315
You can specify a |
@odiaseo, yes, but for Doctrine\DBAL\Cache\ResultCacheStatement need Doctrine\DBAL\Driver\Statement |
Still no solution how to use persistent connection? I also get the ResultCacheStatement error. |
As I mentioned in the first response:
|
Can you give an example please? Where to pass the PDO instance? $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
PDO::ATTR_PERSISTENT => true
PDO::ATTR_STATEMENT_CLASS => array('Custom', array($dbh))
));
$conn = array('pdo' => $dbh);`
$this->entityManager = EntityManager::create($conn, $config); But this gives the |
@timmetj the config above seems correct: that's indeed how it's supposed to be done. Now that makes the limitation clear. I think this cannot be solved on the doctrine side, but needs to be reported as a bug in PHP/PDO core (if there isn't already an issue for this) |
The downside of explicitly passing \PDO instance is that your connection stops being lazy. |
Looks like when using the workaround, passing an instance of When Exceptions thrown by Am I correct in assuming this? Is there a work around? |
It would be interesting to find a way to define persistent connection when using Doctrine to avoid errors on benchmarks with high traffic. See comments in TechEmpower/FrameworkBenchmarks#4293 |
Persistent connections are generally not endorsed, and are not the way real world applications written in PHP should be configured. Do not write benchmarks that do not represent a real world scenario just for the sake of winning at benchmarks. |
So one solution in "real world" is to use proxy for resolving this issue. |
It's your opinion (and it was mine because I generally use transactions and want to avoid specific issues that can appears with persistent connection) but it seems to not be true for other peoples as you can read here: Do you have technical information that prove it? Nothing seems to indicate it should not be used. Even here it's just say that it can be useful to improve performances (depending on the business you have). You can have sites with high traffic where transactions are not used in real world. http://php.net/manual/en/features.persistent-connections.php |
@raziel057, yes 45krps. Impossible work without proxy(open connection, TIME_WAIT, CLOSE_WAIT, etc). ProxySQL fixes that problems, but has other issues. |
It isn't about PHP or any other language. In a real application we possibly have the database in another server. About using transactions and others issues are things of the past. |
I tried to disable the user-supplied statement class in case we turn ON the persistent connection. In PDOConnection: if (!isset($options[PDO::ATTR_PERSISTENT]) || !$options[PDO::ATTR_PERSISTENT]) {
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('Doctrine\DBAL\Driver\PDOStatement', array()));
} I have run my test suites (phpUnit / behat) on 2 projects that use Doctrine and tried manually the web application and at this stage I can't detect any issues. |
FWIW, as part of #2958, we're not using a custom statement class at all. Could you check if your case works on |
I currently use the following doctrine packages:
In my composer json "require" : {
"php" : ">=7.1",
"symfony/symfony" : "3.4.*",
"doctrine/orm" : "^2.5",
"doctrine/doctrine-bundle" : "^1.6",
...
}, I tried to add "doctrine/dbal" : "dev-develop" and "doctrine/common" : "^2.9" but I still have dependency conflicts. I will check later. What is your opinion about the small modification in PDOConnection to be able to enable the persistent connection for high traffic sites (and benchmarks). Currently it's not fair play because all PHP projects using Doctrine have bad results / errors in last runs because of not using a persistent connection (all others Frameworks use it): It would be great to avoid to give this bad image for such a good solution. |
As proposed in #2315 (comment), it's a breaking change. The statement objects instantiated by Probably, certain non-breaking parts of #2958 (the changes in |
Actually, the fact that /cc @Ocramius |
Resolved by #3515. |
That's a very good news! Thanks |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
While trying to use doctrine with persistent pdo_mysql connections, it seemed that this is not supported.
To be clear, when you pass
PDO::ATTR_PERSISTENT => true
as config option for connection, an exception will be raised stating thatGeneral error: PDO::ATTR_STATEMENT_CLASS cannot be used with persistent PDO instances
.After some research, i found this answer on stackoverflow suggesting to pass a pre-created PDO instance as configuration parameter like this:
It does work and my application is able use doctrine as usual. BUT it feels strange. My understanding is that this way we fool doctrine to use pdo without the statement class (which is by the way
Doctrine\DBAL\Driver\PDOStatement
). Can someone please point out, if there are any risks of using doctrine like that?I also suggest to add the corresponding info regarding persistent connection to the docs.
The text was updated successfully, but these errors were encountered: