Jawad AboFakher
Jawad AboFakher

Reputation: 311

Cannot invoke "org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(java.sql.SQLException, String)

I am trying to build a web application, I didn't add any classes or packages I have just ran the app but it says

java.lang.NullPointerException: Cannot invoke "org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(java.sql.SQLException, String)" because the return value of "org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.sqlExceptionHelper()" is null
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:116) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.getJdbcEnvironmentUsingJdbcMetadata(JdbcEnvironmentInitiator.java:279) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:193) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:69) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:119) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:264) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:239) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:216) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.boot.model.relational.Database.<init>(Database.java:45) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.getDatabase(InFlightMetadataCollectorImpl.java:231) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:199) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:169) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1383) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1454) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:75) ~[spring-orm-6.0.13.jar:6.0.13]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:376) ~[spring-orm-6.0.13.jar:6.0.13]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-6.0.13.jar:6.0.13]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-6.0.13.jar:6.0.13]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:352) ~[spring-orm-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1817) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1766) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:325) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1166) ~[spring-context-6.0.13.jar:6.0.13]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:940) ~[spring-context-6.0.13.jar:6.0.13]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:616) ~[spring-context-6.0.13.jar:6.0.13]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.1.5.jar:3.1.5]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:738) ~[spring-boot-3.1.5.jar:3.1.5]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:440) ~[spring-boot-3.1.5.jar:3.1.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) ~[spring-boot-3.1.5.jar:3.1.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-3.1.5.jar:3.1.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-3.1.5.jar:3.1.5]
    at com.cafemanagementsystem.CafeManagementSystemProjectApplication.main(CafeManagementSystemProjectApplication.java:10)

The pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cafemanagementsystem</groupId>
    <artifactId>CafeManagementSystemProject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>CafeManagementSystemProject</name>
    <description>Cafe Management System Project using Spring Boot Framework.</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

application properties

server.port = 6969
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/CafeManagementDatabase
spring.datasource.username = user
spring.datasource.password = 12345
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
spring.jpa.properties.hibernate.dialect.format_sql = true
package com.cafemanagementsystem;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CafeManagementSystemProjectApplication {

    public static void main(String[] args) {
        SpringApplication.run(CafeManagementSystemProjectApplication.class, args);
    }

}

Upvotes: 31

Views: 89169

Answers (11)

cancomert
cancomert

Reputation: 51

It looks like a Bug on the Hibernate Side. I get the same error as well but the real reason behind was my Connection blocked by Firewall and I get "Connection Timeout" exception.

However the Bug on the Hibernate side is shadowing the real "Connection Timeout" and causes this NPE, which makes it hard to debug the real reason of the error.

Suggested ways for disabling meta-data access or setting Dialect explicitely may fix the reason behind (without seeing it on the logs) but there is still one remaining Problem on the Hibernate side; the real reason is shadowed by a NullPointerException. I can create a Bug for this but I am not sure where exactly in Hibernate JIRA it should be placed..

Ah it is already reported and also fixed: https://hibernate.atlassian.net/browse/HHH-18313

Upvotes: 0

Hhovhann
Hhovhann

Reputation: 769

Tests and documentation for Spring Data JPA with Hibernate and HikariCP.

== Prevent early database interaction

Non-embedded databases require extra configuration to avoid Hibernate to contact the remote database before the refresh phase:

# Specify explicitly the dialect (here for PostgreSQL, adapt for your database)
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

# Disable Hibernate usage of JDBC metadata
spring.jpa.properties.hibernate.boot.allow_jdbc_metadata_access=false

# Database initialization should typically be performed outside of Spring lifecycle
spring.jpa.hibernate.ddl-auto=none
spring.sql.init.mode=never

NOTE: The supported dialects can be found https://docs.jboss.org/hibernate/orm/6.5/userguide/html_single/Hibernate_User_Guide.html#compatibility-database[here].

NOTE: With Spring Boot 3.2 / Hibernate 6.4 and earlier, use spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false instead of spring.jpa.properties.hibernate.boot.allow_jdbc_metadata_access=false.

=== JVM Checkpoint Restore

In order to allow checkpoint/restore to work as expected, Hikari pool suspension should be enabled:

spring.datasource.hikari.allow-pool-suspension=true

Upvotes: 17

Tushar Kawade
Tushar Kawade

Reputation: 19

In my case, I did not have a database in the MySQL server. After I created the database, the issue was resolved

Upvotes: -1

Jose Pose S
Jose Pose S

Reputation: 1295

The issue was resolved by adding the following property to my application.properties file:

spring.jpa.properties.hibernate.boot.allow_jdbc_metadata_access=false

Explanation

By setting spring.jpa.properties.hibernate.boot.allow_jdbc_metadata_access to false, Hibernate no longer tries to access the JDBC metadata during startup, which can help avoid certain connection-related issues. This change allowed my application to start without errors.

More info https://github.com/spring-projects/spring-lifecycle-smoke-tests/tree/main/data/data-jpa

Upvotes: 8

Haythem Khiari
Haythem Khiari

Reputation: 9

check that the name of the database that you have created is "CafeManagementDatabase"

i got also that problem and it happened for me because i did some typo when i named my database

Upvotes: -1

Fatiq Hussnain
Fatiq Hussnain

Reputation: 181

spring.application.name=SpringBoot-PostgreSQL

# To connect to PostgreSQL locally
spring.datasource.url=jdbc:postgresql://localhost:5432/practice-db
spring.datasource.username=postgres
spring.datasource.password=123

#spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

server.port=8080

spring.datasource.hikari.allow-pool-suspension=true


# Specify explicitly the dialect (here for PostgreSQL, adapt for yourdatabase)
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

# Disable Hibernate usage of JDBC metadata
spring.jpa.properties.hibernate.boot.allow_jdbc_metadata_access=false

# Database initialization should typically be performed outside of Springlifecycle
spring.jpa.hibernate.ddl-auto=update
spring.sql.init.mode=never

This error comes in Spring Boot 3.3.2 when we use Hibernate for Database connectivity.

You can change username, password and database name other things should be same. If error persists then create a database in PostgreSQL of your own and replace your database name in url. In this way you explicitly create database and put its name in url.

Upvotes: 3

dong sheng
dong sheng

Reputation: 364

my case : org.postgresql.util.PSQLException: Connection to localhost:6257 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. so,just correct your ip:host in application.yaml.

Upvotes: -1

Archie
Archie

Reputation: 5431

FWIW, this simple patch fixes it for me (Hibernate 6.4.4.Final):

--- src/main/java/org/hibernate/engine/jdbc/internal/JdbcServicesImpl.java  2024-06-25 12:02:34.922703631 -0500
+++ src/main/java/org/hibernate/engine/jdbc/internal/JdbcServicesImpl.java  2024-06-28 12:18:31.478949616 -0500
@@ -89,7 +90,7 @@
        if ( jdbcEnvironment != null ) {
            return jdbcEnvironment.getSqlExceptionHelper();
        }
-       return null;
+       return new SqlExceptionHelper( false );
    }
 
    @Override

Upvotes: 1

Parth Patel
Parth Patel

Reputation: 5

I faced the same error, but I resolved it by replacing the import for the @Id annotation from:

import org.springframework.data.annotation.Id;

to:

import jakarta.persistence.Id;

in all my entity classes.

Upvotes: -3

Phani Indra
Phani Indra

Reputation: 21

Try this way as I got error during my docker run method which shown that port is not available.

docker: Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:3306 -> 0.0.0.0:0: listen tcp 0.0.0.0:3306: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.

So I changed the port number to 3307 as follows:

docker run --detach --env MYSQL_ROOT_PASSWORD=hari --env MYSQL_USER=phani --env MYSQL_PASSWORD=hari --env MYSQL_DATABASE=shopping --name mysql --publish 3307:3306 mysql:8-oracle

In your application.properties file change it to the port number you passed i.e.3307:

spring.datasource.url=jdbc:mysql://localhost:3307/shopping

Upvotes: 2

Berk
Berk

Reputation: 17

I'd just verify the DB credentials and URL. I've ran into same problem, turns out the DB credentials in the docker image has been changed.

Upvotes: -1

Related Questions