rethrow failed exceptions after rolling back.

This commit is contained in:
Tobe O 2019-10-20 11:23:40 -04:00
parent 82e7c410b4
commit 54ea6d157e
3 changed files with 32 additions and 13 deletions

View file

@ -1,3 +1,6 @@
# 1.1.0-beta.1
* Improvements in how transactions are handled; rethrow failed exceptions after rolling back.
# 1.1.0-beta # 1.1.0-beta
* Updates for `package:angel_orm@2.1.0-beta`. * Updates for `package:angel_orm@2.1.0-beta`.

View file

@ -50,16 +50,32 @@ class PostgreSqlExecutor extends QueryExecutor {
@override @override
Future<T> transaction<T>(FutureOr<T> Function(QueryExecutor) f) async { Future<T> transaction<T>(FutureOr<T> Function(QueryExecutor) f) async {
if (_connection is! PostgreSQLConnection) return await f(this); if (_connection is! PostgreSQLConnection) return await f(this);
T result;
try { var conn = _connection as PostgreSQLConnection;
logger?.fine('Entering transaction'); T returnValue;
await (_connection as PostgreSQLConnection).transaction((ctx) async {
var txResult = await conn.transaction((ctx) async {
try {
logger?.fine('Entering transaction');
var tx = PostgreSqlExecutor(ctx, logger: logger); var tx = PostgreSqlExecutor(ctx, logger: logger);
result = await f(tx); returnValue = await f(tx);
}); } catch (e) {
} finally { ctx.cancelTransaction(reason: e.toString());
logger?.fine('Exiting transaction'); rethrow;
return result; } finally {
logger?.fine('Exiting transaction');
}
});
if (txResult is PostgreSQLRollback) {
if (txResult.reason == null) {
throw StateError('The transaction was cancelled.');
} else {
throw StateError(
'The transaction was cancelled with reason "${txResult.reason}".');
}
} else {
return returnValue;
} }
} }
} }

View file

@ -4,7 +4,7 @@ description: PostgreSQL support for Angel's ORM. Includes functionality for quer
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/orm homepage: https://github.com/angel-dart/orm
environment: environment:
sdk: '>=2.0.0-dev.1.2 <3.0.0' sdk: '>=2.0.0 <3.0.0'
dependencies: dependencies:
angel_orm: ^2.1.0-beta angel_orm: ^2.1.0-beta
logging: ^0.11.0 logging: ^0.11.0
@ -15,6 +15,6 @@ dev_dependencies:
path: ../angel_orm_test path: ../angel_orm_test
pretty_logging: ^1.0.0 pretty_logging: ^1.0.0
test: ^1.0.0 test: ^1.0.0
dependency_overrides: # dependency_overrides:
angel_orm: # angel_orm:
path: ../angel_orm # path: ../angel_orm