{"record":{"id":"7553ac9c9c02d22a","repo":"quarkusio/quarkus","slug":"cannot-retrieve-a-connection-to-the-database-durin","errorCode":null,"errorMessage":"Cannot retrieve a connection to the database during Quarkus' static initialization. Delay the connection retrieval until runtime.","messagePattern":"Cannot retrieve a connection to the database during Quarkus' static initialization\\. Delay the connection retrieval until runtime\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/service/QuarkusStaticInitConnectionProvider.java","lineNumber":12,"sourceCode":"package io.quarkus.hibernate.orm.runtime.service;\n\nimport java.sql.Connection;\nimport java.sql.SQLException;\n\nimport org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;\nimport org.hibernate.service.UnknownUnwrapTypeException;\n\npublic class QuarkusStaticInitConnectionProvider implements ConnectionProvider {\n    @Override\n    public Connection getConnection() throws SQLException {\n        throw new UnsupportedOperationException(\n                \"Cannot retrieve a connection to the database during Quarkus' static initialization. Delay the connection retrieval until runtime.\");\n    }\n\n    @Override\n    public void closeConnection(Connection connection) throws SQLException {\n        // Should not be called, since getting a connection is impossible.\n    }\n\n    @Override\n    public boolean supportsAggressiveRelease() {\n        return false;\n    }\n\n    @Override\n    public boolean isUnwrappableAs(final Class<?> unwrapType) {\n        return ConnectionProvider.class.equals(unwrapType);\n    }\n","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/service/QuarkusStaticInitConnectionProvider.java#L1-L30","documentation":"QuarkusStaticInitConnectionProvider is used while Hibernate services are initialized during Quarkus' static (build/native-image) initialization, where no database connection can be made. Its getConnection() always throws UnsupportedOperationException with this message, meaning Hibernate attempted to obtain a JDBC connection too early — during static init instead of runtime.","triggerScenarios":"Hibernate attempting connection acquisition during static initialization: e.g. schema generation/export or DDL validation configured to run at startup being executed during the Quarkus static-init phase, or code that triggers a JDBC connection from a static initializer of a class processed at image build time.","commonSituations":"Setting quarkus.hibernate-orm.database.generation=create-drop/update in a native image context that executes during static init; custom code running DB access inside static blocks or constructors captured for native image build; using features that eagerly connect at build time instead of runtime init.","solutions":["Delay the database access: move connection-requiring logic out of static initializers into runtime beans (@Startup/@Observes StartupEvent or lazy CDI injection).","For native images, ensure Hibernate is initialized at runtime (RUNTIME_INIT), not build time — avoid static fields holding SessionFactory/EntityManager.","Check schema generation settings so DDL work happens at runtime startup, not during static init."],"exampleFix":"// before\npublic class DbWarmup {\n    static {\n        sessionFactory.openSession(); // runs during static init\n    }\n}\n\n// after\n@ApplicationScoped\npublic class DbWarmup {\n    void onStart(@Observes StartupEvent ev) {\n        sessionFactory.openSession(); // runs at runtime startup\n    }\n}","handlingStrategy":"validation","validationCode":"// Detect DB access in static initializers before native build\n// (run a build-time check)\nif (isStaticInitPhase()) {\n    throw new IllegalStateException(\"Do not acquire DB connections during static init; \"\n        + \"move work to @Observes StartupEvent or runtime-init beans\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    connection = connectionProvider.getConnection();\n} catch (UnsupportedOperationException e) {\n    if (e.getMessage().contains(\"static initialization\")) {\n        log.error(\"DB access attempted during static init — defer to runtime (StartupEvent / @Transactional)\");\n    }\n    throw e;\n}","preventionTips":["Never open sessions/connections in static blocks or build-time-init constructors","Initialize Hibernate lazily or at runtime init (RUNTIME_INIT) in native images","Move schema generation to runtime startup rather than static init","Keep static fields free of SessionFactory/EntityManager references"],"tags":["hibernate-orm","static-init","native-image","connection","quarkus"],"backgroundTag":"connection-during-static-init","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}