Skip to content

Exception when using transaction manually #43

@simolus3

Description

@simolus3

To implement nested transactions (with savepoints), I want to be able to control transactions manually (that is, I run the BEGIN and COMMIT statements with execute and make myself responsible for blocking other statements while the transaction is active).

However, it seems like this library crashes when doing that:

import 'package:postgres/postgres.dart';

// Reproducing with `docker run -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres postgres`
void main(List<String> args) async {
  final connection = PostgreSQLConnection('localhost', 5432, 'postgres',
      username: 'postgres', password: 'postgres');
  await connection.open();

  await connection.execute('BEGIN');
  print(await connection.execute('SELECT 1 + 2;'));
  await connection.execute('COMMIT');
}

This throws the following exception:

Unhandled exception:
type 'PostgreSQLConnection' is not a subtype of type '_TransactionProxy' in type cast
#0      _PostgreSQLConnectionStateBusy.onMessage (package:postgres/src/connection_fsm.dart:288:31)
#1      PostgreSQLConnection._readData (package:postgres/src/connection.dart:286:47)
#2      _RootZone.runUnaryGuarded (dart:async/zone.dart:1618:10)
#3      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
#4      _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#5      _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:774:19)
#6      _StreamController._add (dart:async/stream_controller.dart:648:7)
#7      _StreamController.add (dart:async/stream_controller.dart:596:5)
#8      _Socket._onData (dart:io-patch/socket_patch.dart:2314:41)
#9      _RootZone.runUnaryGuarded (dart:async/zone.dart:1618:10)
#10     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
#11     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#12     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:774:19)
#13     _StreamController._add (dart:async/stream_controller.dart:648:7)
#14     _StreamController.add (dart:async/stream_controller.dart:596:5)
#15     new _RawSocket.<anonymous closure> (dart:io-patch/socket_patch.dart:1839:33)
#16     _NativeSocket.issueReadEvent.issue (dart:io-patch/socket_patch.dart:1322:14)
#17     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#18     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)
#19     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:122:13)
#20     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:193:5)

Is there a way do manage those transactions manually?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions