Skip to content

Commit 9183f8f

Browse files
authored
Prevent ContainerDatabaseDriver from throwing NPEs when on classpath but not initialized (#3976)
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.
1 parent 1e6deb1 commit 9183f8f

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,27 +230,30 @@ private void runInitFunctionIfRequired(final ConnectionUrl connectionUrl, Connec
230230

231231
@Override
232232
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
233-
return delegate.getPropertyInfo(url, info);
233+
return delegate != null ? delegate.getPropertyInfo(url, info) : new DriverPropertyInfo[0];
234234
}
235235

236236
@Override
237237
public int getMajorVersion() {
238-
return delegate.getMajorVersion();
238+
return delegate != null ? delegate.getMajorVersion() : 1;
239239
}
240240

241241
@Override
242242
public int getMinorVersion() {
243-
return delegate.getMinorVersion();
243+
return delegate != null ? delegate.getMinorVersion() : 0;
244244
}
245245

246246
@Override
247247
public boolean jdbcCompliant() {
248-
return delegate.jdbcCompliant();
248+
return delegate != null && delegate.jdbcCompliant();
249249
}
250250

251251
@Override
252252
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
253-
return delegate.getParentLogger();
253+
if (delegate != null) {
254+
return delegate.getParentLogger();
255+
}
256+
throw new SQLFeatureNotSupportedException("getParentLogger not supported");
254257
}
255258

256259
/**

0 commit comments

Comments
 (0)