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

JDBC: Prevent ContainerDatabaseDriver from throwing NPEs when on classpath but not initialized #3976

Merged
merged 1 commit into from
Apr 11, 2021

Conversation

mrotteveel
Copy link
Contributor

When ContainerDatabaseDriver hasn't been used yet to connect, the delegate is null. This causes various JDBC metadata methods of the driver to fail with a NullPointerException. The change prevents this by providing reasonable defaults.

As an example, when org.testcontainers:jdbc is on the classpath, but hasn't been used yet, the following produces a NullPointerException:

DriverManager.drivers()
    .forEach(driver -> System.out.printf("Driver: '%s', version: %d.%d%n", 
        driver.getClass().getName(), driver.getMajorVersion(), driver.getMinorVersion()));
Exception in thread "main" java.lang.NullPointerException
	at org.testcontainers.jdbc.ContainerDatabaseDriver.getMajorVersion(ContainerDatabaseDriver.java:237)
	at nl.lawinegevaar.pg.DriverVersion.lambda$main$1(DriverVersion.java:39)
	at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1655)
	at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:658)
	at nl.lawinegevaar.pg.DriverVersion.main(DriverVersion.java:39)

The interrogation of the java.sql.Driver metadata shouldn't produce a NPE.

When ContainerDatabaseDriver hasn't been used yet to connect, the delegate is null. This causes various JDBC metadata methods of the driver to fail with a NullPointerException. Prevent this by providing reasonable defaults.
Copy link
Member

@rnorth rnorth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mrotteveel, this sounds like a good and useful improvement. I'm happy to approve.

@rnorth rnorth changed the title Prevent ContainerDatabaseDriver from throwing NPEs. Prevent ContainerDatabaseDriver from throwing NPEs when on classpath but not initialized Apr 11, 2021
@rnorth rnorth added this to the next milestone Apr 11, 2021
@rnorth rnorth merged commit 9183f8f into testcontainers:master Apr 11, 2021
@mrotteveel mrotteveel deleted the driver-null-safety branch April 12, 2021 08:05
@rnorth rnorth changed the title Prevent ContainerDatabaseDriver from throwing NPEs when on classpath but not initialized JDBC: Prevent ContainerDatabaseDriver from throwing NPEs when on classpath but not initialized Apr 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants