Y
Y
yuradun2021-11-19 18:23:15
Java
yuradun, 2021-11-19 18:23:15

Why can't connect to remote database via Spring Boot JPA?

The hosting is MySQL database version 10.3, to which I try to connect, but no matter how hard I try, it crashes with a lot of errors.

I set up remote access on the hosting, everyone can connect to the host.
Looking through phpmyadmin, MySQL is running on localhost:3306.

I checked through the application for remote connection to the Sequel PRO database by setting the connection data - everything worked with full access. Accordingly, the problem is not on the hosting side.

I also raised a local server with MySQL, where, having also changed only the data in the application.properties file, I easily joined and created entities.

Could someone explain what's wrong and how to connect to a remote database?

Request Controller File

package com.example.API.Controllers.UserAuthControllers;

import com.example.API.Models.UserPassword;
import com.example.API.Repositories.AuthRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class RegistrationController {

    @Autowired
    private AuthRepository authRepository;

    @PostMapping("/auth/registrationCreateRequest")
    public String test(
            @RequestParam(value = "login") String login,
            @RequestParam(value = "passwordHASH") String passwordHASH
    ) {
        UserPassword userPassword = new UserPassword(login, passwordHASH);
        authRepository.save(userPassword);
        return "ok";
    }
}


Entity file for database
package com.example.API.Models;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "users_pwd")
public class UserPassword {

    @Id
    @GeneratedValue
    private int id;
    private String login;
    private String passwordHASH;

    public UserPassword(String login, String passwordHASH) {
        this.login = login;
        this.passwordHASH = passwordHASH;
    }

    public UserPassword() {
    }

    public long getId() {
        return id;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPasswordHASH() {
        return passwordHASH;
    }

    public void setPasswordHASH(String passwordHASH) {
        this.passwordHASH = passwordHASH;
    }
}


Repository file
package com.example.API.Repositories;

import com.example.API.Models.UserPassword;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface AuthRepository extends JpaRepository<UserPassword, Integer> {
}


application.properties file
# MySQL BD CONNECTION
spring.jpa.database=mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://remoteHost:3306/bdname
spring.datasource.username=username
spring.datasource.password=pwd
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=none


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>2.5.6</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.example</groupId>
  <artifactId>API</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>project</name>
  <description>desc</description>
  <properties>
    <java.version>11</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>2.5.7</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.27</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
      <version>2.5.7</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.6.1.Final</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.13.0</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
      <version>2.5.6</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-test</artifactId>
      <version>5.6.0</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>


Here are the error headers below, when the application crashes, and the error is "The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server." crashes already several times, I think it has the juice, but I couldn’t ask Google or the answers weren’t working for me.

2021-11-19 15:54:36.642  WARN 58707 --- [           main] o.h.e.j.e.i.JdbcEnvironmentInitiator     : HHH000342: Could not obtain connection to query metadata

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution

Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
Caused by: org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution

Process finished with exit code 1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-11-19
@yuradun

Good evening!

No appropriate protocol (protocol is disabled or cipher suites are inappropriate)

Have you tried that?
jdbc:mysql://remoteHost:3306/bdname?autoReconnect=true&useSSL=false

https://stackoverflow.com/questions/67899129/postf...
Here's what they say:
As far as I am concerned, the cause of the issue is that OpenJdk requires TLSv1.2 or TLSv1.3, starting from version 11.0.11. Update: The change will apply to at least OpenJDK 8u292 onward, OpenJDK 11.0.11 onward, and all versions of OpenJDK 16, following the JRE and JDK Crypto Roadmap published by Oracle

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question