depth > 0) { throw new \LogicException('Nested transaction.'); } $this->depth++; if (false === $this->db->query('START TRANSACTION')) { $this->depth--; throw new \RuntimeException('Could not start a transaction: ' . esc_html($this->db->last_error)); } try { $result = $work(); if (false === $this->db->query('COMMIT')) { throw new \RuntimeException('Could not commit: ' . esc_html($this->db->last_error)); } return $result; } catch (\Throwable $e) { $this->db->query('ROLLBACK'); throw $e; } finally { $this->depth--; } } }