SetTransactionIsolationLevel.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\DB;
  8. use Doctrine\Common\EventSubscriber;
  9. use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
  10. use Doctrine\DBAL\Event\ConnectionEventArgs;
  11. use Doctrine\DBAL\Events;
  12. use Doctrine\DBAL\Platforms\MySQLPlatform;
  13. use Doctrine\DBAL\TransactionIsolationLevel;
  14. class SetTransactionIsolationLevel implements EventSubscriber {
  15. /**
  16. * @param ConnectionEventArgs $args
  17. * @return void
  18. */
  19. public function postConnect(ConnectionEventArgs $args) {
  20. $connection = $args->getConnection();
  21. if ($connection instanceof PrimaryReadReplicaConnection && $connection->isConnectedToPrimary()) {
  22. $connection->setTransactionIsolation(TransactionIsolationLevel::READ_COMMITTED);
  23. if ($connection->getDatabasePlatform() instanceof MySQLPlatform) {
  24. $connection->executeStatement('SET SESSION AUTOCOMMIT=1');
  25. }
  26. }
  27. }
  28. public function getSubscribedEvents() {
  29. return [Events::postConnect];
  30. }
  31. }