Unable to create initial pool of connections in springboot - java

I am trying to connect a database with the springboot but that is not running in any way. I have made about 3 applications till now but all have the same issue. I am a total spring newbie and working for the first time.
I have used JPA and Mysql for the application but all the time the error will show itself.
application file
datasource:
url: jdbc:mysql://localhost:3306/test_schema
username: root
password: user
jpa:
generate-ddl: true
show-sql: true
Error
Sat Jun 08 11:02:13 IST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
2019-06-08 11:02:13.362 ERROR 14040 --- [ main] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:868) ~[mysql-connector-java-5.1.41.jar:5.1.41]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:864) ~[mysql-connector-java-5.1.41.jar:5.1.41]
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1746) ~[mysql-connector-java-5.1.41.jar:5.1.41]
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226) ~[mysql-connector-java-5.1.41.jar:5.1.41]
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2205) ~[mysql-connector-java-5.1.41.jar:5.1.41]
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2236) ~[mysql-connecto......
Error continues and is really a very long list
I have asked a similar question before but that solved the problem of database schema and table but now this is showing up . And this error is showing in almost all the applications. Mysql is up and running. The username password are correct. The database also has some initial entries.

caching_sha2_password is the default authentication plugin for MySQL.
Please try updating the mysql connector library to say mysql-connector-java-8.0.12 ( try to match mysql version and connector version )
Probably yo are using version around "5.1.44".
Also, if this doesnt work, try as what is suggested above
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'user';
If the change fixes this issue but leads to a different exception : java.sql.SQLException: Unknown system variable 'query_cache_size', it probably indicates the version of mySQL connector is not correct/matching for MySQL 8x versions
Remember, "The query cache is deprecated as of MySQL 5.7.20, and is removed in MySQL 8.0"
Update the dependency for MYSQL connector library to use latter versions, latest on date is 8.0.15:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>

Run this command on your mysql server :
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'user';

Related

Java application not connecting to oracle database with correct username/password

I have spring boot application which used ojdbc6 11.2.0.3 driver. Following are my spring data source properties
spring.datasource.username=abc
spring.datasource.password=abc
spring.datasource.url=jdbc:oracle:thin:#myhost:1521:orcl
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.physical-strategy= org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.generate-ddl=true
#spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
This gives error java.sql.SQLException: ORA-01017: invalid username/password; logon denied
i'm 100% sure username/password correct. Also db has SEC_CASE_SENSITIVE_LOGON=FALSE. I checked with both uppercase and lowercase username/password and still get error. Simple java app with same driver connect to db fine.
What would cause this ? What direction should i investigate. Is this could related to https://community.oracle.com/thread/2188514. My JDK is 14.0.1.
If you are 100% sure the password is right, perhaps you are affected by the problem in the JIT
Connection To Database From Oracle JVM Using Server Side Jdbc Thin Driver Throws Ora-01017
When the Java code is deployed to a pre-11g database the server-side JDBC/thin connection works fine.
After the Java code is deployed to a 11.2 database the server-side JDBC/thin connection fails with ORA-01017: invalid username/password; logon denied.
You can try this workaround
alter system set java_jit_enabled=FALSE;

Can not connect to MS SQL Server using JDBC driver SQLState 08001

I am trying to connect to a MS SQL Server database using Java but I keep running into the same error.
I am using MS SQL Server for developers and am maniging it through SSMS. When I installed it I set is so it would use my windows credentials as password.
The JDBC driver is added to my project as a Maven dependency.
From what I understand reading the Microsoft doc's I should be able to use the following connection string:
String connectionUrl ="jdbc:sqlserver://localhost;integratedSecurity=true";
However this does not seem to work for me as it returns SQLState 08001. After reading up on this state I understand that it has something to do with failing to make the connection.
I also tried to form my connection string using my windows login name, which results in the same:
String connectionUrl = "jdbc:sqlserver:localhost:1433;databaseName=DBNAME;user=John Doe;password=0123456789";
I also tried to create a login using T-SQL to use as credentials using the same string as the previous (only changing the username and password):
CREATE ROLE [USER]
GRANT SELECT, INSERT ON SCHEMA :: [dbo] TO USERNAME
CREATE LOGIN USERNAME_LOGIN WITH PASSWORD = '1234567890'
CREATE USER USER_USERNAME FROM LOGIN USERNAME_LOGIN
ALTER ROLE [USER] ADD MEMBER USER_USERNAME
I could really use some help to understand what I am doing wrong and what I can do to make it work. If you require me to check anything please let me know how as I am still pretty new to this.
Edit
As requested here is the info coming from getMessage():
No suitable driver found for
jdbc:sqlserver:localhost:1433;databaseName=.....
I am guessing this would also be the moment to mention the Maven dependency I am importing:
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.3.0.jre8-preview</version>
</dependency>
I think the question now becomes, do I have the wrong driver as a dependency for MS SQL Server 2017?
I turned out I did not had TCP/IP connections enabled for MS SQL Server.
To enable this in MS SQL Server 2017:
Start SQLServerManager14.msc from the windows application menu.
Go to SQL Server Network Configuration.
Go to Protocols for YOURINSTANCE.
Set TCP/IP to enabled.

Glassfish Keystore error when creating a mysql datasource in AWS RDS

Can someone help me solve this problem, I just installed glassfish 5.0 in EC2, unzipped the files, changed the master password and admin password, ran the domain, but when I try to create a datasource to a Mysql RDS instance it gave this error
Connection could not be allocated because: Cannot open file:C:\glassfish4\glassfish\domains\domain1/config/keystore.jks [Keystore was tampered with, or password was incorrect]
I tried a lot of things, made a new domain, used the default master pass, etc, but I'm unable to find an answer for this,.
New Info:
I managed to connect removing ssl wit useSSL = false property, but can someone explain how to set up SSL to glassfish in order to conect to RDS winth SSL to me or tell me where can I read abou it.
Glassfish log
SEVERE
Complete Message
Mon Aug 20 23:36:32 UTC 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
WARNING
Message ID test.connection.pool.failed
Complete Message
RAR8054: Exception while creating an unpooled [test] connection for pool [DB ], Connection could not be allocated because: Cannot open file:C:\glassfish4\glassfish\domains\domain1/config/keystore.jks [Keystore was tampered with, or password was incorrect]
SEVERE
Message ID
RestResponse.getResponse() gives FAILURE. endpoint = 'http://localhost:4848/management/domain/resources/ping-connection-pool.json'; attrs = '{id=DB}'
Thank you

How to make Spring server to start even if database is down?

I'm using a Spring Boot(1.4.7) & MyBatis.
spring.main1.datasource.url=jdbc:mariadb://192.168.0.11:3306/testdb?useUnicode=true&characterEncoding=utf8&autoReconnect=true&socketTimeout=5000&connectTimeout=3000
spring.main1.datasource.username=username
spring.main1.datasource.password=password
spring.main1.datasource.driverClassName=org.mariadb.jdbc.Driver
spring.main1.datasource.tomcat.test-on-borrow=true
spring.main1.datasource.tomcat.test-while-idle=true
spring.main1.datasource.tomcat.validation-query=SELECT 1
spring.main1.datasource.tomcat.validation-query-timeout=5000
spring.main1.datasource.tomcat.validation-interval=5000
spring.main1.datasource.tomcat.max-wait=5000
spring.main1.datasource.continue-on-error=true
I cannot start program with errors when database is disconnected on Eclipse or Linux server.
(Database is not located on localhost.)
When I try to start program with disconnected database,
print this.
java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=192.168.0.11)(port=3306)(type=master) : connect timed out
Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=192.168.0.11)(port=3306)(type=master) : connect timed out
Stopping service [Tomcat]
Application startup failed
Is there any way?
Thanks
You can set:
spring.sql.init.continue-on-error=true
in your application.properties.
According to the Spring Boot 2.5.5 user guide:
By default, Spring Boot enables the fail-fast feature of its script-based database initializer. This means that, if the scripts cause exceptions, the application fails to start. You can tune that behavior by setting spring.sql.init.continue-on-error.
P.S.: Before Spring Boot 2.5, the property was named spring.datasource.continue-on-error.
I was able to solve this. One main difference between what I got working and the code in the question, though, is that I'm using Hikari instead of Tomcat for the connection pool.
These were the key settings I had to make:
spring.datasource.hikari.minimum-idle: 0
spring.datasource.hikari.initialization-fail-timeout: -1
spring.datasource.continue-on-error: true
spring.datasource.driver-class-name: org.postgresql.Driver
spring.jpa.database-platform: org.hibernate.dialect.PostgreSQLDialect
Setting minimum-idle to 0 allows Hikari to be happy without any connections.
The initialization-fail-timeout setting of -1 tells Hikari that I don't want it to get a connection when the pool fires up.
From the HikariCP documentation:
A value less than zero will bypass any initial connection attempt, and the pool will start immediately while trying to obtain connections in the background. Consequently, later efforts to obtain a connection may fail.
The continue-on-error setting true allows the service to continue even when encountering an error.
Both the driver-class-name and database-platform were required. Otherwise, Hikari tries to figure out those values by connecting to the database (during startup).
Just in case I'm missing something, though, here's my full Spring config:
spring:
application:
name: <redacted>
datasource:
url: <redacted>
username: <redacted>
password: <redacted>
driver-class-name: org.postgresql.Driver
hikari:
minimum-idle: 0
maximum-pool-size: 15
connection-timeout: 10000 #10s
idle-timeout: 300000 #5m
max-lifetime: 600000 #10m
initialization-fail-timeout: -1
validation-timeout: 1000 #1s
continue-on-error: true
jpa:
open-in-view: false
database-platform: org.hibernate.dialect.PostgreSQLDialect
And my project has the following Spring Boot dependencies:
org.springframework.boot:spring-boot
org.springframework.boot:spring-boot-actuator
org.springframework.boot:spring-boot-actuator-autoconfigure
org.springframework.boot:spring-boot-autoconfigure
org.springframework.boot:spring-boot-configuration-processor
org.springframework.boot:spring-boot-devtools
org.springframework.boot:spring-boot-starter
org.springframework.boot:spring-boot-starter-actuator
org.springframework.boot:spring-boot-starter-jdbc
org.springframework.boot:spring-boot-starter-jooq
org.springframework.boot:spring-boot-starter-json
org.springframework.boot:spring-boot-starter-logging
org.springframework.boot:spring-boot-starter-security
org.springframework.boot:spring-boot-starter-test
org.springframework.boot:spring-boot-starter-tomcat
org.springframework.boot:spring-boot-starter-validation
org.springframework.boot:spring-boot-starter-web
You need to add
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
in order to make it works
If the tips above didn't help and you use jpa, then set
spring.jpa.hibernate.ddl-auto=none
it worked for me.
When you build your app you can add that
mvn clean install -DskipTests
it will skip the tests of database connection
(-D is used to define a system property)

Error connection to herokupostgresql locally

I am writing a struts2 app and using hibernate for persistence. I deploy may app on heroku and everything works ok, but when ever I run it locally I get:
org.postgresql.util.PSQLException: FATAL:no pg_hba.conf entry for host "xx.xx.xxx.xxx", user "someuser", database "somedatabase", SSL off
I know the problem is I need to connect to the database over ssl but how can I set this up locally?
Add this to the end of your JDBC connection URL:
?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory
The key here is "SSL off".
You must use SSL to connect to Heroku. How to enable it depends on the client you are using, which I'm guessing is PgJDBC since you're using Java and Hibernate.
The SSL section of the manual for PgJDBC covers what you need.

Categories