-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: deallocate mysqli prepared statement (#6681)
Long running processes might hit the `max_prepared_stmt_count` due not deallocating the statement correctly. <!-- Fill in the relevant information below to help triage your pull request. --> | Q | A |------------- | ----------- | Type | bug | Fixed issues | - #### Summary For postgres there was an implementation to deallocate prepared statements to prevent hitting a any limits. This was not the case for the `mysqli` drivers. Which can suffer from the same problem. In this PR a `mysqli_stmt_close` is added to properly cleanup any open prepared statement. A similar approach was implemented for PostgreSQL in this PR #5893 --------- Co-authored-by: Alexander M. Turek <[email protected]>
- Loading branch information
Showing
3 changed files
with
89 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Functional\Driver\Mysqli; | ||
|
||
use Doctrine\DBAL\Driver\Mysqli\Statement; | ||
use Doctrine\DBAL\Statement as WrapperStatement; | ||
use Doctrine\DBAL\Tests\FunctionalTestCase; | ||
use Doctrine\DBAL\Tests\TestUtil; | ||
use Error; | ||
use ErrorException; | ||
use ReflectionProperty; | ||
|
||
use function restore_error_handler; | ||
use function set_error_handler; | ||
|
||
use const PHP_VERSION_ID; | ||
|
||
/** @requires extension mysqli */ | ||
class StatementTest extends FunctionalTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
if (TestUtil::isDriverOneOf('mysqli')) { | ||
return; | ||
} | ||
|
||
self::markTestSkipped('This test requires the mysqli driver.'); | ||
} | ||
|
||
public function testStatementsAreDeallocatedProperly(): void | ||
{ | ||
$statement = $this->connection->prepare('SELECT 1'); | ||
|
||
$property = new ReflectionProperty(WrapperStatement::class, 'stmt'); | ||
$property->setAccessible(true); | ||
|
||
$driverStatement = $property->getValue($statement); | ||
|
||
$mysqliProperty = new ReflectionProperty(Statement::class, 'stmt'); | ||
$mysqliProperty->setAccessible(true); | ||
|
||
$mysqliStatement = $mysqliProperty->getValue($driverStatement); | ||
|
||
unset($statement, $driverStatement); | ||
|
||
if (PHP_VERSION_ID < 80000) { | ||
$this->expectException(ErrorException::class); | ||
$this->expectExceptionMessage('mysqli_stmt::execute(): Couldn\'t fetch mysqli_stmt'); | ||
} else { | ||
$this->expectException(Error::class); | ||
$this->expectExceptionMessage('mysqli_stmt object is already closed'); | ||
} | ||
|
||
set_error_handler(static function (int $errorNumber, string $error, string $file, int $line): void { | ||
throw new ErrorException($error, 0, $errorNumber, $file, $line); | ||
}); | ||
|
||
try { | ||
$mysqliStatement->execute(); | ||
} finally { | ||
restore_error_handler(); | ||
} | ||
} | ||
} |