Skip to content

Question About Prepared Statement Behavior in Transaction #433

@qlina911-commits

Description

@qlina911-commits

Hi,

I’ve encountered some strange behavior in your package. If I prepare a statement using a given connection, and then try to execute that prepared statement inside a transaction on the same connection, the execution hangs indefinitely.

However, if I prepare the statement inside the transaction, everything works as expected.

Does this mean that it’s not possible to use pre-prepared statements within a transaction, even if they are created on the same connection? Is this the intended behavior?

Thanks in advance for your clarification.

Best regards,

import 'dart:async';
import 'package:postgres/postgres.dart';
import 'package:dotenv/dotenv.dart';
import 'dart:io';

void main() async {
  final envPath = Platform.environment['DB_CONFIG'];
  final env = DotEnv()..load([envPath!]);

  final endpoint = Endpoint(
    host: env['DB_HOST']!,
    database: env['DB_NAME']!,
    port: int.parse(env['DB_PORT']!),
    username: env['DB_USER']!,
    password: env['DB_PASSWORD']!,
  );

  var c1 = await Connection.open(endpoint);

  var s1 = await c1.prepare('SELECT * FROM catalog.test FOR SHARE');
  var completer1 = Completer();
  c1.runTx(
    (txSession) async {
      print('try lock');

      var s2 = await txSession.prepare('SELECT * FROM catalog.test FOR SHARE');
      // s1 = s2;

      // Here the request hangs for some reason.
      // But if you use the second query instead of the current value, then
      // everything is fine (s1 = s2).
      await s1.run(null);

      print('blocking completed');
      return completer1.future;
    },
  );
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions