diff --git a/src/Mysqldump.php b/src/Mysqldump.php index 22fbe03a..89f5bb6e 100644 --- a/src/Mysqldump.php +++ b/src/Mysqldump.php @@ -193,6 +193,12 @@ public function start(?string $filename = '') $this->write($this->getDumpFileHeader()); } + // Initiate a transaction at global level to create a consistent snapshot. + if ($this->settings->isEnabled('single-transaction')) { + $this->conn->exec($this->db->setupTransaction()); + $this->conn->exec($this->db->startTransaction()); + } + // Store server settings and use saner defaults to dump $this->write($this->db->backupParameters()); @@ -234,6 +240,11 @@ public function start(?string $filename = '') // Restore saved parameters. $this->write($this->db->restoreParameters()); + // End transaction. + if ($this->settings->isEnabled('single-transaction')) { + $this->conn->exec($this->db->commitTransaction()); + } + // Write some stats to output file. if (!$this->settings->skipComments()) { $this->write($this->getDumpFileFooter()); @@ -887,11 +898,6 @@ private function prepareListValues(string $tableName) ); } - if ($this->settings->isEnabled('single-transaction')) { - $this->conn->exec($this->db->setupTransaction()); - $this->conn->exec($this->db->startTransaction()); - } - if ($this->settings->isEnabled('lock-tables') && !$this->settings->isEnabled('single-transaction')) { $this->db->lockTable($tableName); } @@ -926,10 +932,6 @@ private function endListValues(string $tableName, int $count = 0) $this->write($this->db->endAddLockTable($tableName)); } - if ($this->settings->isEnabled('single-transaction')) { - $this->conn->exec($this->db->commitTransaction()); - } - if ($this->settings->isEnabled('lock-tables') && !$this->settings->isEnabled('single-transaction')) { $this->db->unlockTable($tableName);