Skip to content

Commit

Permalink
[11.x] MySQL transaction isolation level fix (#50689)
Browse files Browse the repository at this point in the history
* Fix transaction isolation level in MySqlConnector.php

* Update DatabaseConnectorTest.php

* Style fix

* Code style fix

---------

Co-authored-by: Graham Campbell <GrahamCampbell@users.noreply.github.com>
  • Loading branch information
mwikberg-virta and GrahamCampbell authored Mar 21, 2024
1 parent bd096bc commit 202e521
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Illuminate/Database/Connectors/MySqlConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ protected function getHostDsn(array $config)
*/
protected function configureConnection(PDO $connection, array $config)
{
$statements = [];

if (isset($config['isolation_level'])) {
$statements[] = sprintf('SESSION TRANSACTION ISOLATION LEVEL %s', $config['isolation_level']);
$connection->exec(sprintf('SET SESSION TRANSACTION ISOLATION LEVEL %s;', $config['isolation_level']));
}

$statements = [];

if (isset($config['charset'])) {
if (isset($config['collation'])) {
$statements[] = sprintf("NAMES '%s' COLLATE '%s'", $config['charset'], $config['collation']);
Expand Down
3 changes: 2 additions & 1 deletion tests/Database/DatabaseConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public function testMySqlConnectCallsCreateConnectionWithIsolationLevel()
$connector->expects($this->once())->method('getOptions')->with($this->equalTo($config))->willReturn(['options']);
$connector->expects($this->once())->method('createConnection')->with($this->equalTo($dsn), $this->equalTo($config), $this->equalTo(['options']))->willReturn($connection);
$connection->shouldReceive('exec')->once()->with('use `bar`;')->andReturn(true);
$connection->shouldReceive('exec')->once()->with("SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ, NAMES 'utf8' COLLATE 'utf8_unicode_ci';")->andReturn(true);
$connection->shouldReceive('exec')->once()->with('SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;')->andReturn(true);
$connection->shouldReceive('exec')->once()->with("SET NAMES 'utf8' COLLATE 'utf8_unicode_ci';")->andReturn(true);
$result = $connector->connect($config);

$this->assertSame($result, $connection);
Expand Down

0 comments on commit 202e521

Please sign in to comment.