Skip to content

Commit

Permalink
bugfix: deallocate mysqli prepared statement
Browse files Browse the repository at this point in the history
Long running processes might hit the `max_prepared_stmt_count` due not
deallocating the statement correctly.
  • Loading branch information
petermein committed Jan 3, 2025
1 parent 6428f1a commit d92e954
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/Driver/Mysqli/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ final class Result implements ResultInterface
*
* @throws Exception
*/
public function __construct(private readonly mysqli_stmt $statement)
public function __construct(
private readonly mysqli_stmt $statement,
private readonly ?Statement $statementReference = null,
)
{
$meta = $statement->result_metadata();
$this->hasColumns = $meta !== false;
Expand Down
7 changes: 6 additions & 1 deletion src/Driver/Mysqli/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public function __construct(private readonly mysqli_stmt $stmt)
$this->boundValues = array_fill(1, $paramCount, null);
}

public function __destruct()
{
$this->stmt->close();
}

public function bindValue(int|string $param, mixed $value, ParameterType $type): void
{
assert(is_int($param));
Expand All @@ -72,7 +77,7 @@ public function execute(): Result
throw StatementError::upcast($e);
}

return new Result($this->stmt);
return new Result($this->stmt, $this);
}

/**
Expand Down
52 changes: 52 additions & 0 deletions tests/Functional/Driver/Mysqli/StatementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Functional\Driver\Mysqli;

use Doctrine\DBAL\Driver\Mysqli\Statement;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Statement as WrapperStatement;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\TestUtil;
use Error;
use mysqli_sql_exception;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use ReflectionProperty;

#[RequiresPhpExtension('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);

$mysqliSProperty = new ReflectionProperty(Statement::class, 'stmt');
$mysqliSProperty->setAccessible(true);

$mysqliStatement = $mysqliSProperty->getValue($driverStatement);

unset($statement, $driverStatement);

$this->expectException(Error::class);
$this->expectExceptionMessage('mysqli_stmt object is already closed');

$mysqliStatement->execute();
}
}

0 comments on commit d92e954

Please sign in to comment.