java.sql.SQLException: No suitable driver found

This seems to happen sometimes in test and dev. After digging into java.sql.DriverManager, which is responsible for loading all drivers, it seems to use ServiceLoader to discover all subclasses of java.sql.Driver. Apparently, sometimes it fails in test. No idea why.

To fix it, we can register the driver manually before getting a connection like below:

try {
  DriverManager.registerDriver(new org.postgresql.Driver());
} catch (SQLException e) {
  throw new RuntimeException(e);
}

Subscribe to tanin

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe