Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch Throwable in Connection::transactional() #2390

Merged
merged 3 commits into from
May 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -773,7 +774,7 @@ public function prepare($statement)
{
try {
$stmt = new Statement($statement, $this);
} catch (\Exception $ex) {
} catch (Exception $ex) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this because it was incosistent in this file (didn't look at the other files tho).

throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
}

Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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]);
}

Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -1103,6 +1104,9 @@ public function transactional(Closure $func)
} catch (Exception $e) {
$this->rollBack();
throw $e;
} catch (Throwable $e) {
$this->rollBack();
throw $e;
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', '<')) {
Copy link
Contributor Author

@fprochazka fprochazka May 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the convention for skipping tests in older php versions. If this is not good I can change it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also use @requires php 7 in an annotation on this method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ocramius thanks, updated

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fprochazka did you push? I still see the if block

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I did, and it failed hard #2390 (diff)

$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) {
Expand Down