rethrow failed exceptions after rolling back.
This commit is contained in:
parent
82e7c410b4
commit
54ea6d157e
3 changed files with 32 additions and 13 deletions
|
@ -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
|
||||
* Updates for `package:angel_orm@2.1.0-beta`.
|
||||
|
||||
|
|
|
@ -50,16 +50,32 @@ class PostgreSqlExecutor extends QueryExecutor {
|
|||
@override
|
||||
Future<T> transaction<T>(FutureOr<T> Function(QueryExecutor) f) async {
|
||||
if (_connection is! PostgreSQLConnection) return await f(this);
|
||||
T result;
|
||||
try {
|
||||
logger?.fine('Entering transaction');
|
||||
await (_connection as PostgreSQLConnection).transaction((ctx) async {
|
||||
|
||||
var conn = _connection as PostgreSQLConnection;
|
||||
T returnValue;
|
||||
|
||||
var txResult = await conn.transaction((ctx) async {
|
||||
try {
|
||||
logger?.fine('Entering transaction');
|
||||
var tx = PostgreSqlExecutor(ctx, logger: logger);
|
||||
result = await f(tx);
|
||||
});
|
||||
} finally {
|
||||
logger?.fine('Exiting transaction');
|
||||
return result;
|
||||
returnValue = await f(tx);
|
||||
} catch (e) {
|
||||
ctx.cancelTransaction(reason: e.toString());
|
||||
rethrow;
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ description: PostgreSQL support for Angel's ORM. Includes functionality for quer
|
|||
author: Tobe O <thosakwe@gmail.com>
|
||||
homepage: https://github.com/angel-dart/orm
|
||||
environment:
|
||||
sdk: '>=2.0.0-dev.1.2 <3.0.0'
|
||||
sdk: '>=2.0.0 <3.0.0'
|
||||
dependencies:
|
||||
angel_orm: ^2.1.0-beta
|
||||
logging: ^0.11.0
|
||||
|
@ -15,6 +15,6 @@ dev_dependencies:
|
|||
path: ../angel_orm_test
|
||||
pretty_logging: ^1.0.0
|
||||
test: ^1.0.0
|
||||
dependency_overrides:
|
||||
angel_orm:
|
||||
path: ../angel_orm
|
||||
# dependency_overrides:
|
||||
# angel_orm:
|
||||
# path: ../angel_orm
|
Loading…
Reference in a new issue