diff --git a/lib/Doctrine/DBAL/Connection.php b/lib/Doctrine/DBAL/Connection.php index d3d7eb1ccc0..b710b4a42e2 100644 --- a/lib/Doctrine/DBAL/Connection.php +++ b/lib/Doctrine/DBAL/Connection.php @@ -32,6 +32,7 @@ use Doctrine\DBAL\Cache\ArrayStatement; use Doctrine\DBAL\Cache\CacheException; use Doctrine\DBAL\Driver\PingableConnection; +use Throwable; /** * A wrapper around a Doctrine\DBAL\Driver\Connection that adds features like @@ -773,7 +774,7 @@ public function prepare($statement) { try { $stmt = new Statement($statement, $this); - } catch (\Exception $ex) { + } catch (Exception $ex) { throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement); } @@ -824,7 +825,7 @@ public function executeQuery($query, array $params = array(), $types = array(), } else { $stmt = $this->_conn->query($query); } - } catch (\Exception $ex) { + } catch (Exception $ex) { throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $query, $this->resolveParams($params, $types)); } @@ -933,7 +934,7 @@ public function query() $statement = call_user_func_array(array($this->_conn, 'query'), $args); break; } - } catch (\Exception $ex) { + } catch (Exception $ex) { throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $args[0]); } @@ -984,7 +985,7 @@ public function executeUpdate($query, array $params = array(), array $types = ar } else { $result = $this->_conn->exec($query); } - } catch (\Exception $ex) { + } catch (Exception $ex) { throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $query, $this->resolveParams($params, $types)); } @@ -1015,7 +1016,7 @@ public function exec($statement) try { $result = $this->_conn->exec($statement); - } catch (\Exception $ex) { + } catch (Exception $ex) { throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement); } @@ -1103,6 +1104,9 @@ public function transactional(Closure $func) } catch (Exception $e) { $this->rollBack(); throw $e; + } catch (Throwable $e) { + $this->rollBack(); + throw $e; } } diff --git a/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php index bcb4b51f06a..3f91e61d3e7 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php @@ -196,11 +196,30 @@ public function testTransactionalWithException() $conn->executeQuery($conn->getDatabasePlatform()->getDummySelectSQL()); throw new \RuntimeException("Ooops!"); }); + $this->fail('Expected exception'); } catch (\RuntimeException $expected) { $this->assertEquals(0, $this->_conn->getTransactionNestingLevel()); } } + public function testTransactionalWithThrowable() + { + if (version_compare(PHP_VERSION, '7.0', '<')) { + $this->markTestSkipped('Only for PHP 7.0 and above.'); + } + + try { + $this->_conn->transactional(function($conn) { + /* @var $conn \Doctrine\DBAL\Connection */ + $conn->executeQuery($conn->getDatabasePlatform()->getDummySelectSQL()); + throw new \Error("Ooops!"); + }); + $this->fail('Expected exception'); + } catch (\Error $expected) { + $this->assertEquals(0, $this->_conn->getTransactionNestingLevel()); + } + } + public function testTransactional() { $this->_conn->transactional(function($conn) {