/** * Execute a Closure within a transaction. * * @param \Closure $callback * @return mixed * * @throws \Exception */ public function transaction(Closure $callback) { if ($this->getDriverName() == 'sqlsrv') { return parent::transaction($callback); } $this->pdo->exec('BEGIN TRAN'); try { $result = $callback($this); $this->pdo->exec('COMMIT TRAN'); } catch (\Exception $e) { $this->pdo->exec('ROLLBACK TRAN'); throw $e; } return $result; }
/** * Execute a Closure within a transaction. * * @param \Closure $callback * @return mixed * * @throws \Exception */ public function transaction(Closure $callback) { if ($this->getDriverName() == 'sqlsrv') { return parent::transaction($callback); } $this->pdo->exec('BEGIN TRAN'); // We'll simply execute the given callback within a try / catch block // and if we catch any exception we can rollback the transaction // so that none of the changes are persisted to the database. try { $result = $callback($this); $this->pdo->exec('COMMIT TRAN'); } catch (\Exception $e) { $this->pdo->exec('ROLLBACK TRAN'); throw $e; } return $result; }