|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\Tests\DBAL\Functional\LockMode; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Connection; |
| 8 | +use Doctrine\DBAL\Driver\OCI8; |
| 9 | +use Doctrine\DBAL\Exception; |
| 10 | +use Doctrine\DBAL\LockMode; |
| 11 | +use Doctrine\DBAL\Platforms\SqlitePlatform; |
| 12 | +use Doctrine\DBAL\Platforms\SQLServerPlatform; |
| 13 | +use Doctrine\DBAL\Schema\Table; |
| 14 | +use Doctrine\DBAL\TransactionIsolationLevel; |
| 15 | +use Doctrine\Tests\DbalFunctionalTestCase; |
| 16 | +use Doctrine\Tests\TestUtil; |
| 17 | + |
| 18 | +class NoneTest extends DbalFunctionalTestCase |
| 19 | +{ |
| 20 | + /** @var Connection */ |
| 21 | + private $connection2; |
| 22 | + |
| 23 | + public function setUp(): void |
| 24 | + { |
| 25 | + parent::setUp(); |
| 26 | + |
| 27 | + if ($this->connection->getDriver() instanceof OCI8\Driver) { |
| 28 | + // https://github.com/doctrine/dbal/issues/4417 |
| 29 | + self::markTestSkipped('This test fails on OCI8 for a currently unknown reason'); |
| 30 | + } |
| 31 | + |
| 32 | + if ($this->connection->getDatabasePlatform() instanceof SQLServerPlatform) { |
| 33 | + // Use row versioning instead of locking on SQL Server (if we don't, the second connection will block when |
| 34 | + // attempting to read the row created by the first connection, instead of reading the previous version); |
| 35 | + // for some reason we cannot set READ_COMMITTED_SNAPSHOT ON when not running this test in isolation, |
| 36 | + // there may be another connection active at this point; temporarily forcing to SINGLE_USER does the trick. |
| 37 | + $db = $this->connection->getDatabase(); |
| 38 | + $this->connection->executeStatement('ALTER DATABASE ' . $db . ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE'); |
| 39 | + $this->connection->executeStatement('ALTER DATABASE ' . $db . ' SET READ_COMMITTED_SNAPSHOT ON'); |
| 40 | + $this->connection->executeStatement('ALTER DATABASE ' . $db . ' SET MULTI_USER'); |
| 41 | + } |
| 42 | + |
| 43 | + $table = new Table('users'); |
| 44 | + $table->addColumn('id', 'integer'); |
| 45 | + $table->setPrimaryKey(['id']); |
| 46 | + |
| 47 | + $this->connection->getSchemaManager()->dropAndCreateTable($table); |
| 48 | + |
| 49 | + $this->connection2 = TestUtil::getConnection(); |
| 50 | + |
| 51 | + if ($this->connection2->getSchemaManager()->tablesExist('users')) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + if ($this->connection2->getDatabasePlatform() instanceof SqlitePlatform) { |
| 56 | + self::markTestSkipped('This test cannot run on SQLite using an in-memory database'); |
| 57 | + } |
| 58 | + |
| 59 | + self::fail('Separate connections do not seem to talk to the same database'); |
| 60 | + } |
| 61 | + |
| 62 | + public function tearDown(): void |
| 63 | + { |
| 64 | + parent::tearDown(); |
| 65 | + |
| 66 | + if ($this->connection2->isTransactionActive()) { |
| 67 | + $this->connection2->rollBack(); |
| 68 | + } |
| 69 | + |
| 70 | + $this->connection2->close(); |
| 71 | + |
| 72 | + $this->connection->getSchemaManager()->dropTable('users'); |
| 73 | + |
| 74 | + if (! $this->connection->getDatabasePlatform() instanceof SQLServerPlatform) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + $db = $this->connection->getDatabase(); |
| 79 | + $this->connection->executeStatement('ALTER DATABASE ' . $db . ' SET READ_COMMITTED_SNAPSHOT OFF'); |
| 80 | + } |
| 81 | + |
| 82 | + public function testLockModeNoneDoesNotBreakTransactionIsolation(): void |
| 83 | + { |
| 84 | + try { |
| 85 | + $this->connection->setTransactionIsolation(TransactionIsolationLevel::READ_COMMITTED); |
| 86 | + $this->connection2->setTransactionIsolation(TransactionIsolationLevel::READ_COMMITTED); |
| 87 | + } catch (Exception $e) { |
| 88 | + self::markTestSkipped('This test must be able to set a transaction isolation level'); |
| 89 | + } |
| 90 | + |
| 91 | + $this->connection->beginTransaction(); |
| 92 | + $this->connection2->beginTransaction(); |
| 93 | + |
| 94 | + $this->connection->insert('users', ['id' => 1]); |
| 95 | + |
| 96 | + $query = 'SELECT id FROM users'; |
| 97 | + $query = $this->connection2->getDatabasePlatform()->appendLockHint($query, LockMode::NONE); |
| 98 | + |
| 99 | + self::assertFalse($this->connection2->fetchOne($query)); |
| 100 | + } |
| 101 | +} |
0 commit comments