testcontainers-java icon indicating copy to clipboard operation
testcontainers-java copied to clipboard

[Bug]: Underlying NullPointerException in ContainerLessJdbcDelegate when using ScriptUtils

Open nils-christian opened this issue 8 months ago • 6 comments

Module

Core

Testcontainers version

1.21.4

Using the latest Testcontainers version?

Yes

Host OS

Windows

Host Arch

x64

Docker version

Client:                                                              
 Version:           29.0.2-rd                                        
 API version:       1.51 (downgraded from 1.52)                      
 Go version:        go1.25.4                                         
 Git commit:        5a2d7eb                                          
 Built:             Tue Nov 18 23:34:16 2025                         
 OS/Arch:           windows/amd64                                    
 Context:           default                                          
                                                                     
Server:                                                              
 Engine:                                                             
  Version:          28.3.3                                           
  API version:      1.51 (minimum version 1.24)                      
  Go version:       go1.24.8                                         
  Git commit:       bea959c7b793b32a893820b97c4eadc7c87fabb0         
  Built:            Sun Oct 12 08:01:14 2025                         
  OS/Arch:          linux/amd64                                      
  Experimental:     false                                            
 containerd:                                                         
  Version:          v2.1.5                                           
  GitCommit:        fcd43222d6b07379a4be9786bda52438f0dd16a1         
 runc:                                                               
  Version:          1.3.3                                            
  GitCommit:        d842d7719497cc3b774fd71620278ac9e17710e0         
 docker-init:                                                        
  Version:          0.19.0                                           
  GitCommit:

What happened?

Hi,

When running a script with ScriptUtils on ContainerLessJdbcDelegate an underlying NullPointerException occurs.

ScriptUtils.runInitScript( new ContainerLessJdbcDelegate( connection ), initScript );

Reason is the code in org.testcontainers.jdbc.JdbcDatabaseDelegate#closeConnectionQuietly. It closes the connection which is null, as the ContainerLessJdbcDelegate has an own connection field. This could probably be solved by implementing closeConnectionQuietly in ContainerLessJdbcDelegate and closing the own field instead of the one on the super class.

Thank you and best regards

Nils

Relevant log output

2026-03-12 08:28:11 ERROR [main           ] o.t.j.JdbcDatabaseDelegate - Could not close JDBC connection
 java.lang.NullPointerException: Cannot invoke "java.sql.Connection.close()" because "this.connection" is null
 	at org.testcontainers.jdbc.JdbcDatabaseDelegate.closeConnectionQuietly(JdbcDatabaseDelegate.java:72)
 	at org.testcontainers.jdbc.JdbcDatabaseDelegate.closeConnectionQuietly(JdbcDatabaseDelegate.java:16)
 	at org.testcontainers.delegate.AbstractDatabaseDelegate.close(AbstractDatabaseDelegate.java:45)
 	at org.testcontainers.ext.ScriptUtils.executeDatabaseScript(ScriptUtils.java:308)
 	at org.testcontainers.ext.ScriptUtils.executeDatabaseScript(ScriptUtils.java:225)
 	at org.testcontainers.ext.ScriptUtils.runInitScript(ScriptUtils.java:213)

Additional Information

No response

nils-christian avatar Mar 12 '26 07:03 nils-christian

I would like to work on this issue

himi-sh avatar Mar 14 '26 22:03 himi-sh

I would like to work on this issue

Hi @himi-sh, are you still working on this? I'd be happy to pick it up if you're no longer planning to submit a PR — just want to make sure I'm not stepping on your work.

PreAgile avatar Mar 27 '26 14:03 PreAgile

@PreAgile I have the fix already for it. @nils-christian Could you make me the assignee and review the PR

himi-sh avatar Apr 02 '26 22:04 himi-sh

Hi @himi-sh ,

No, because I am not a maintainer of Testcontainers.

nils-christian avatar Apr 03 '26 06:04 nils-christian

@eddumelendez could you assign this to me and review the PR

himi-sh avatar Apr 16 '26 21:04 himi-sh

Hi, I'd like to work on this issue!

snehal242005 avatar Jul 11 '26 07:07 snehal242005