Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConnectionPoolDataSourceImpl Returns Dead Connections to Connection Pool #124

Open
vnayar opened this issue Mar 28, 2024 · 0 comments
Open
Labels

Comments

@vnayar
Copy link
Contributor

vnayar commented Mar 28, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants