I'm trying to connect my spring boot application with MySQL server.
I have followed the documentation properly but still, I'm getting some issue in connection
Following exception is throwing every time
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
In order for me to get around this error I have to log into MySql with the username & password stored in my application.properties file using MySql Workbench. After doing so the error no longer occurs. This only happens when trying to Run my spring boot project after restarting MySql server.
UPDATE: Adding "&allowPublicKeyRetrieval=true" to my spring.datasource.url in the application.properties file solved the problem. I figured this out after I noticed another error in the logs: "MySQL Public Key Retrieval is not allowed"
spring.datasource.url=jdbc:mysql://localhost:3306/db_example?autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
First of all restart MySQL server and retry again...
If it is not...
Did you add application.properties details and JPA, MySQL dependency?
Please Could you show pom.xml file and application.properties file.
appication.properties
spring.mvc.view.prefix=/WEB-INF/JSP/ #jsp file path
spring.mvc.view.suffix=.jsp
#Hibernate
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.generate-ddl=true
#JPA
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Thank you...!
Related
While running Spring boot project getting error as below. We are using Azure sql server 12 and able to connect SQL database from my local. I am using same database details in my local of the properties file
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2008Dialect
spring.datasource.url=jdbc:sqlserver://xxxxx.database.windows.net:1433;database=xxxxx
spring.datasource.username=xxxx
spring.datasource.password=xxxx
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
error:
Caused by: org.flywaydb.core.api.FlywayException: Unsupported Database: Microsoft SQL Server 12.0
Any idea?
Able to connect Azure SQL database from my local and showing version 12.X
It seems that you need to add another dependency for flyway to work with this database:
https://documentation.red-gate.com/fd/sql-server-184127608.html
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-sqlserver</artifactId>
</dependency>
I install SQL Server on my local computer and embedd it into my Spring Boot application. After starting Tomcat I get the following error:
'PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target'. ClientConnectionId:85411829-6853-4fdb-9373-b4c93e1d5e8f
I know that this error is well documenteted. I followed many guides and read much about it, but all advices I found did not fix my issue.
What I had done:
Download a random SSL-certificate from a website and add it to the
cacert file in the Java directory (descriped here).
Configure Spring Boot for SQL Server (descriped here)
Configure SSL Encryption for SQL Server (descriped here)
No one of these advices fixed the error. The only thing I realize is that if I set spring.jpa.hibernate.ddl-auto in my application.properties to none the program shows the error message, but it did not abort running.
The application.properties looks like this:
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=Car
spring.datasource.username=admin
spring.datasource.password=password123
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
server.port=8443
server.ssl.key-alias=selfsigned_localhost_sslserver
server.ssl.key-password=changeit
server.ssl.key-store=classpath:ssl-server.jks
server.ssl.key-store-provider=SUN
server.ssl.key-store-type=JKS
My dependencies:
<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-web</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
In SQL Server I create a database with tables and data in it.
Did someone of you have an further advice how to fix this error?
I faced the same issue with spring boot 2.7.4
and it Seems from the comments you're using driver 10.2.X
it turns out that since 2.7.0 the JDBC Driver 10.2 for SQL Server is used
instead of 9.4.1.jre8 for 2.6.x
So you've 1 of 2 solutions that worked for me:
1. Use the older version of mssql-jdbc driver
<properties>
<mssql-jdbc.version>9.4.1.jre8</mssql-jdbc.version>
</properties>
2. Or ask the driver to just trust the whatever the Sql server certiticate is
you can do so by adding this to the connection string:
jdbc:sqlserver://hOSt:pORt;databaseName=dbName;encrypt=true;trustServerCertificate=true
I am using postgresSQL instance on google cloud platform in spring boot app with spring data JPA.
But i am not able to connect to postgresSQL instance at the time of deployment.
I am not really sure on what dependency is required for this and what is the application properties configuration.
here is the code.
application.properties
spring.datasource.url=jdbc:postgresql://google/postgres?cloudSqlInstance=<instance-name>&socketFactory=com.google.cloud.sql.postgres.SocketFactory
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
pom.xml
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>postgres-socket-factory</artifactId>
<version>1.0.12</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-sql-postgresql</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
app.yml for deployment on flex environment:
runtime: java
env: flex
service: payment2
handlers:
- url: /.*
script: this field is required, but ignored
beta_settings:
cloud_sql_instances: <instance-name>
Thanks in advance.Please help!!!
I use Spring Boot and redis. I added in pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
And created class RedisConfig, which contains Beans JedisConnectionFactory jedisConnectionFactory and RedisTemplate< String, Object > redisTemplate().
When I run application, I get error:
***************************
APPLICATION FAILED TO START
***************************
Description:
Cannot determine embedded database driver class for database type NONE
Action:
If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
I don't use a embeded redis. Redis work on my computer on localhost.
application.properties:
spring.redis.host=localhost
spring.redis.port=6379
Why there is this error?
There is a couple of issues:
spring-boot-starter-redis is deprecated. Use spring-boot-starter-data-redis instead.
Remove the spring-boot-starter-data-jpa dependency. Spring Data Redis does not support JPA and it's not needed. This is actually causing your error.
I am able to use RABBITMQ and MYSQLSERVICES which is on pivotal.While binding services I am able to get the Credentials and using that credentials in my application.properties for spring data jpa project.
But this configuration that I am using is hard-coded in application.Properties To Make this configuration dynamically I came to know that we can use vcap services provided by pivotal.
So want to use run-time credentials for rabbimq and mysql.
My Code is below for reference.
File: application.propeties
rabbitmq.host=hostname
rabbitmq.virtual-host=vhost
rabbitmq.username=username
rabbitmq.password=password
rabbit.mainqueue=queue name
rabbit.errorqueue=erro queue name
spring.datasource.url=jdbc:mysql://hostname:postno
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
server.port=8000
The below is the repository file
package com.redistomysql.consumer.repo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface tblemployee_personal_infoRepository extends JpaRepository<tblemployee_personal_info, Long> {
}
Any help would be appreciated.
The link for reference **http://www.java-allandsundry.com/2016/05/approaches-to-binding-spring-boot.html**
Set this configuration in application-cloud.yml for Mysql
---
spring:
datasource:
url: ${vcap.services.mydb.credentials.jdbcUrl}
username: ${vcap.services.mydb.credentials.username}
password: ${vcap.services.mydb.credentials.password}
The config for rabbitMq:
System.getEnv("VCAP_SERVICES")
The dependencies in pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-spring-service-connector</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
<!-- If you intend to deploy the app on Cloud Foundry, add the following -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-cloudfoundry-connector</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-heroku-connector</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
**The manifest.yml**
---
applications:
- name: redistomysql-consumer
path: target/redistomysql-consumer-0.0.1-SNAPSHOT.jar
memory: 1024M
env:
JAVA_OPTS: -Djava.security.egd=file:/dev/./urandom
SPRING_PROFILES_ACTIVE: cloud
services:
- es-mysql-db
- es-consumer-rabbitmq-service
buildpack: https://github.com/cloudfoundry/java-buildpack.git
env:
JBP_CONFIG_SPRING_AUTO_RECONFIGURATION: '{enabled: false}'