ddbc icon indicating copy to clipboard operation
ddbc copied to clipboard

ConnectionPoolDataSourceImpl Returns Dead Connections to Connection Pool

Open vnayar opened this issue 11 months ago • 0 comments

Summary

Once database connections fail, due to a database being restarted or going down, these invalid connections always return to the connection pool, preventing a DB connection from being re-established when the DB comes back online.

Steps to Reproduce

  1. Start a local PostgreSQL database using default settings, e.g. blank username/password and database named "postgres".
  2. Run the following program.
import std.stdio;
import core.thread : Thread;
import core.time : seconds;
import ddbc.drivers.pgsqlddbc;
import ddbc.core : DataSource, Driver, Connection, PreparedStatement;
import ddbc.common : ConnectionPoolDataSourceImpl, DataSourceImpl;

void main() {
  writeln("Edit source/app.d to start your project.");
  Driver driver = new PGSQLDriver();
  string url = PGSQLDriver.generateUrl("localhost", 5432, "postgres");
  string[string] params = PGSQLDriver.setUserAndPassword("", "");
  // DataSource dataSource = new DataSourceImpl(driver, url, params);
  DataSource dataSource = new ConnectionPoolDataSourceImpl(driver, url, params);

  while (true) {
    writeln("Testing connection...");
    try {
      Connection connection = dataSource.getConnection();
      scope (exit) connection.close();

      PreparedStatement statement = connection.prepareStatement("SELECT 1");
      statement.executeQuery();
    } catch (Exception e) {
      writeln(e);
    }
    Thread.sleep(2.seconds);
  }
}
  1. Observe the program running with output "Testing connection...".
  2. Shut down the database.
  3. Observe the program running, printing exceptions like:
ddbc.core.SQLException@../../../.dub/packages/ddbc/0.5.9/ddbc/source/ddbc/drivers/pgsqlddbc.d(787): Error while executing prepared statement SELECT 1
  1. Restart the database.

Expected Result

The DB connection will be re-established, and the program should again print "Testing connection...".

Actual Result

The program continues to print exceptions and never recovers.

Additional Notes

If DataSourceImpl is used instead of ConnectionPoolDataSourceImpl, the program recovers as expected.

vnayar avatar Mar 28 '24 12:03 vnayar