Skip to content

Agroal leaks connections when unwrap is used #37173

Open
@Serkan80

Description

Describe the bug

The following code leaks connections:

@Inject AgroalDataSource ds;

public void foo() {
    try (var con = this.ds.getConnection()) {
         var pgCon = con.unwrap(PgConnection.class);
         new CopyManager(pgCon).copyOut("copy .. to ....", new FileOutputStream("output.csv");
         pgCon.close();
     }
}

It doesn't matter whether you close pgCon or not, it always logs that there is a connection leak:

WARN [io.agr.pool] (agroal-21) Datasource '': A possible cause for the leak of connection org.postgresql.jdbc.PgConnection@2a74e319 is a call to the unwrap() method. close() needs to be called on the connection object provided by the pool.
2023-11-15 15:58:32,373 INFO [io.agr.pool] (agroal-21) Datasource '': Connection leak of org.postgresql.jdbc.PgConnection@2a74e319
Leaked connection org.postgresql.jdbc.PgConnection@34c8b989 is not enlisted. To return it to the pool us
e the flush(LEAK) operation.

2023-11-15 15:58:32,375 INFO [io.agr.pool] (agroal-21) Datasource '': Operations executed on leaked connection org.postgresql.jdbc.PgConnection@2a74e319:

Expected behavior

The unwrapped connection should be automatically closed/returned to the pool after usage.

Actual behavior

Connection leak occurs and after some time we get the Acquisition errors, and the pod gets restarted.

How to Reproduce?

No response

Output of uname -a or ver

No response

Output of java -version

JDK 17 Temurin

Quarkus version or git rev

3.2.8.Final

Build tool (ie. output of mvnw --version or gradlew --version)

Maven 3.8.x

Additional information

No response

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions