quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.85k stars 2.7k forks source link

NullPointerException from Hibernate ORM when there is a problem establishing a connection #44645

Open shawkins opened 3 days ago

shawkins commented 3 days ago

Describe the bug

In some circumstances hibernate fails to report a meaningful error message and instead shows a nullpointerexception:

2024-11-21 19:00:18,827 WARN  [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator] (JPA Startup Thread) HHH000342: Could not obtain connecti                    on to query metadata: java.lang.NullPointerException: Cannot throw exception because the return value of "java.util.function.BiFunction.apply(Object, Obj                    ect)" is null
        at org.hibernate.resource.transaction.backend.jta.internal.JtaIsolationDelegate.doTheWork(JtaIsolationDelegate.java:202)
        at org.hibernate.resource.transaction.backend.jta.internal.JtaIsolationDelegate.lambda$delegateWork$3(JtaIsolationDelegate.java:91)
        at org.hibernate.resource.transaction.backend.jta.internal.JtaIsolationDelegate.doInSuspendedTransaction(JtaIsolationDelegate.java:123)
        at org.hibernate.resource.transaction.backend.jta.internal.JtaIsolationDelegate.delegateWork(JtaIsolationDelegate.java:88)
        at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.getJdbcEnvironmentUsingJdbcMetadata(JdbcEnvironmentInitiator.java:276)
        at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:107)
        at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68)
        at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:130)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:238)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:215)
        at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:52)
        at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:136)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:247)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:215)
        at org.hibernate.boot.internal.SessionFactoryOptionsBuilder.<init>(SessionFactoryOptionsBuilder.java:276)
        at io.quarkus.hibernate.orm.runtime.recording.PrevalidatedQuarkusMetadata.buildSessionFactoryOptionsBuilder(PrevalidatedQuarkusMetadata.java:70)
        at io.quarkus.hibernate.orm.runtime.boot.FastBootEntityManagerFactoryBuilder.build(FastBootEntityManagerFactoryBuilder.java:84)
        at io.quarkus.hibernate.orm.runtime.FastBootHibernatePersistenceProvider.createEntityManagerFactory(FastBootHibernatePersistenceProvider.java:72)
        at jakarta.persistence.Persistence.createEntityManagerFactory(Persistence.java:80)
        at jakarta.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
        at io.quarkus.hibernate.orm.runtime.JPAConfig$LazyPersistenceUnit.get(JPAConfig.java:157)
        at io.quarkus.hibernate.orm.runtime.JPAConfig$1.run(JPAConfig.java:64)
        at java.base/java.lang.Thread.run(Thread.java:1583)

See https://github.com/keycloak/keycloak/issues/35181 - this error also appears in other Keycloak and Quarkus issues, such as https://github.com/quarkusio/quarkus/issues/31842

Expected behavior

A meaningful exception.

Actual behavior

A nullpointerexception.

How to Reproduce?

Keycloak is using a persistence.xml, I'm not sure if that's a requirement to reproduce this. If you then misconfigure the datasource, such as an invalid password, you should see the null pointer exception.

Output of uname -a or ver

No response

Output of java -version

No response

Quarkus version or git rev

3.8.5

Build tool (ie. output of mvnw --version or gradlew --version)

No response

Additional information

No response

quarkus-bot[bot] commented 3 days ago

/cc @gsmet (hibernate-orm), @yrodiere (hibernate-orm)

yrodiere commented 3 days ago

I think the most likely cause would be either the sqlExceptionConverter being passed a null exception (in which case the bug is in whatever called the sqlExceptionConverter), or the sqlExceptionConverter returning null as the "converted" exception (in which case the bug is in the sqlExceptionConverter)

Quoting https://github.com/keycloak/keycloak/issues/35014#issuecomment-2493780098 :

it seems caused by the exception converter returning null, which certainly looks like a bug, possibly caused by this line.

We probably need, at least, to check in Hibernate ORM that sqlExceptionConverter implementations never return null, because I think that would be a bug.

yrodiere commented 3 days ago

Also, thanks for creating the bug report @shawkins :)