Skip to content

Open Connections Remain After Query Errors, Causing DB Connection Saturation and Crashes #463

Description

@AmoonPod

What version of Prisma Dart is running?

5.3.1

What version of Prisma CLI is running?

6.3.1

What type of app are you using?

Flutter

What database are you using?

PostgreSQL

What steps can reproduce the bug?

  1. Create a Dart script using the ORM package as shown below:

final prisma = PrismaClient();

void main(List<String> args) async {
  try {
    var query = '''
    SELECT * FROM "Contract"; 
    ''';

    // A series of valid queries
    await prisma.$raw.query(query);
    await prisma.$raw.query(query);
    await prisma.$raw.query(query);
    await prisma.$raw.query(query);
    await prisma.$raw.query(query);

    // Execute multiple queries concurrently, including one with an intentional syntax error
    Future.wait([
      prisma.$raw.query(query),
      prisma.$raw.query(query),
      prisma.$raw.query('SELEC Q FROM Q'), // Intentional syntax error
      prisma.$raw.query(query),
      prisma.$raw.query(query),
      prisma.$raw.query(query),
      prisma.$raw.query(query),
      prisma.$raw.query(query),
      prisma.$raw.query(query),
      prisma.$raw.query(query),
      prisma.$raw.query(query),
    ]);

    // More queries after the batch
    await prisma.$raw.query(query);
    await prisma.$raw.query(query);
    await prisma.$raw.query(query);
    await prisma.$raw.query(query);
    await prisma.$raw.query(query);
  } catch (e) {
    print(e);
  } finally {
    await prisma.$disconnect();
  }
}
  1. Run the script and observe that when the erroneous query ('SELEC Q FROM Q') is executed, an error is thrown.
  2. Check your PostgreSQL database for open connections. You will notice that connections remain open even after the error, and each subsequent run increases the number of open connections.

What is the expected behavior?

The ORM should handle query errors by properly closing or cleaning up the connection (or rolling back the transaction), ensuring that no open connections remain.
This behavior would prevent the accumulation of database connections and avoid saturating the maximum allowed connections.

What do you see instead?

An error is thrown when a query fails due to syntax issues.
Open connections are not closed after a query error, leading to the creation of additional connections on subsequent queries.
Running the script multiple times results in a continual accumulation of open connections, which eventually overloads and crashes the production database.

Additional information

When executing raw queries using the ORM package with PostgreSQL, queries that produce syntax errors result in open connections that are not properly closed. This issue causes an accumulation of database connections over time, which eventually saturates the maximum allowed connections. In our production environment—where the application is actively used—this leads to connection overloads and system crashes. Resolving this problem is critical to maintain the stability and reliability of our service.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions