So far I was using Postgres on my localhost to run as my DB. Everything was ok, I had this dependency in my pom.xml and the following config (note that I didn't have the Driver specified explicitely):
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
application.configuration:
spring.datasource.url=jdbc:postgresql://*********
spring.datasource.username=********
spring.datasource.password=*********
All good:
2020-10-31 13:47:03.078 INFO 28600 --- [ task-1] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL10Dialect
2020-10-31 13:47:04.037 INFO 28600 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8091 (http) with context path ''
2020-10-31 13:47:04.270 INFO 28600 --- [ task-1] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-10-31 13:47:04.283 INFO 28600 --- [ task-1] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-10-31 13:47:04.659 INFO 28600 --- [ main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-10-31 13:47:04.915 INFO 28600 --- [ main] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-10-31 13:47:04.925 INFO 28600 --- [ main] c.a.u.f.FetcherserviceApplication : Started FetcherserviceApplication in 8.87 seconds (JVM running for 10.334)
Now I wanted to switch to an oracle DB, so I've deleted the above postgresql dependency from my pom.xml and added one for Oracle plus deleted the postgres lines from my configuration and added the following:
spring.datasource.url=jdbc:oracle:thin:#********
spring.datasource.username=********
spring.datasource.password=*********
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.jpa.datasbase-platform=org.hibernate.dialect.Oracle10gDialect
spring.jpa.hibernate.ddl-auto=update
Now, if at this point I try to rerun the application I see that the Oracle Driver is being picked up but then the application still fails to start after because:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-10-31 14:29:30.746 ERROR 6384 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource:
Property: driver-class-name
Value: org.postgresql.Driver
Origin: "driverClassName" from property source "source"
Reason: Failed to load driver class org.postgresql.Driver in either of HikariConfig class loader or Thread context classloader
Action:
Update your application's configuration
Where is this coming from? I did updated the configuration as described above but more importantly the project shouldn't even know at this point that Postgres ever existed as as I've purged it from the pom.xml. So how is it possible there is an issue with it here?
I assume there is still something I don't understand about how Maven manages dependencies.
Thanks!
/András
This is due to the maven unable to import oracle dependencies automatically. Following will help you to resolve the issue
Remove PostgreSQL dependencies from the pom.xml
Then add oracle JDBC dependencies to the pom.xml
https://mvnrepository.com/artifact/com.oracle/ojdbc14
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
Download oracle dependencies and install manually to maven local repository
mvn install:install-file -Dfile="Your downloaded oracle jar file location" -DgroupId=com.oracle.ojdbc -DartifactId="artifact id" -Dversion="downloaded version number" -Dpackaging=jar
Then clean your project and run. It seemed to work.
You should use the latest 19.8 JDBC driver. Refer to this guide for more details.
Also, you can use DataSourceSample.java to verify if everything works fine.
<dependencies>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8-production</artifactId>
<version>19.8.0.0</version>
<type>pom</type>
</dependency>
</dependencies>
Related
I created a Springboot application using https://start.spring.io/.
After adding some initial application properties for spring-data-jpa, the application starts fine however, I do not see any tables created in my local database.
To test this further, I provided incorrect credentials in application.properties and started the application expecting it to fail, however it started fine.
I see in logs it is connecting to a Mysql database but not sure how and where.
Has anyone faced this strange issue ?
application.properties
spring.datasource.url = jdbc:mysql://localhost:3306/db
spring.datasource.username = db
spring.datasource.password = pass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
Pom.xml
<?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.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<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-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I even provided an incorrect port in database url, it still started fine with following logs :
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.4)
2022-10-01 22:01:21.025 INFO 20928 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 16.0.2 on DESKTOP-S112VSS with PID 20928 (C:\Users\Paras\Downloads\demo\demo\target\classes started by Paras in C:\Users\Paras\Downloads\demo\demo)
2022-10-01 22:01:21.036 INFO 20928 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to 1 default profile: "default"
2022-10-01 22:01:22.278 INFO 20928 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-10-01 22:01:22.302 INFO 20928 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 9 ms. Found 0 JPA repository interfaces.
2022-10-01 22:01:23.557 INFO 20928 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-10-01 22:01:23.583 INFO 20928 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-10-01 22:01:23.584 INFO 20928 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-10-01 22:01:23.957 INFO 20928 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-10-01 22:01:23.957 INFO 20928 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2789 ms
2022-10-01 22:01:24.267 INFO 20928 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-10-01 22:01:24.524 INFO 20928 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2022-10-01 22:01:24.600 INFO 20928 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-10-01 22:01:24.697 INFO 20928 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.11.Final
2022-10-01 22:01:24.971 INFO 20928 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-10-01 22:01:25.150 INFO 20928 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2022-10-01 22:01:25.472 INFO 20928 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-10-01 22:01:25.489 INFO 20928 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-10-01 22:01:25.550 WARN 20928 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2022-10-01 22:01:26.069 INFO 20928 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-10-01 22:01:26.086 INFO 20928 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 5.957 seconds (JVM running for 6.799)
The issue has been resolved, somehow there were environment variables being set from a previous project, so Spring was taking that as a priority instead of the values defined in the properties file.
Look again at your "log" and your configuration file. In the first one it indicates a dialect of MySQL version 8, however in the configuration file, it shows version 5. That's for starters.
I think you should see this log
[0;39m [2m:[0;39m Registered driver with driverClassName=com.mysql.jdbc.Driver
was not found, trying direct instantiation.
it,s the driver settings incorrect?
setting this configure properties correct
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Please check the following line, driver class
Set it to like.
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
also, check your system's MySQL version
if it's 5.7 then make your hibernate dialect version 57,
if it's 5.8 then make your hibernate dialect to version 8.
Example, My SQL version is
mysql Ver 8.0.30 for macos12.4 on x86_64 (Homebrew)
so, I have set my hibernate dialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
I hope, it helps!
Add this to the configuration
logging.level.com.zaxxer.hikari.HikariConfig=DEBUG
You will see the database URL in the logs.
The problem can be, IDE doesn't sync your changes to the build folder. Check application.properties in the build folder.
Also It is not necessary that application will connect to the database during startup. You can try to save() a simple entity.
https://stackoverflow.com/a/74019458/3405171
You can add spring-boot-starter-actuator library to check the connection during startup for sure.
I am creating a brand new spring boot application,
I have the below dependencies.
<artifactId>spring-boot-starter</artifactId>
<artifactId>spring-boot-starter-test</artifactId>
<artifactId>spring-kafka</artifactId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<artifactId>spring-boot-starter-actuator</artifactId>
<artifactId>lombok</artifactId>
<artifactId>mysql-connector-java</artifactId>
The application starts but it shutsdown with below warning logs.
2022-02-25 23:58:10.225 INFO 72504 --- [ main] c.d.c.l.LibrarydemoApplication : Started LibrarydemoApplication in 1.512 seconds (JVM running for 2.016)
2022-02-25 23:58:10.230 INFO 72504 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2022-02-25 23:58:10.232 INFO 72504 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2022-02-25 23:58:10.236 INFO 72504 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
Mysql connectivity is good. below is the config of application props.
spring.datasource.url=jdbc:mysql://localhost:3306/repo
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
Please advise how resolve this issue.
The app started and then exited successfully. Looks like you want it to be a webapp and for that you need to start a webserver.
You can do by adding spring-boot-starter-web dependency to your app.It will add spring-boot-starter-tomcat to your app.
In your maven file add this
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
You can then get rid of spring-boot-starter as it is included with starter-web dependency
have un problem connection data base with spring boot
I'm the sequel of a developer that is already working. I've been provided the sources but I can't launch the Spring Boot Java project with IntelliJ. I use a sever Xampp for my dataBase. But I have the following error
:: Spring Boot :: (v2.3.3.RELEASE)
2020-12-15 16:39:28.411 INFO 2384 --- [ main] c.a.myapp.myappApplication : Starting myappApplication on DESKTOP-DRP2JSE with PID 2384 (C:\Users\Admin\Downloads\myapp 2.0 Final\myappBack 2.0 - Final\myappBack\out\production\myappBack started by Admin in C:\Users\Admin\Downloads\myapp 2.0 Final\myappBack 2.0 - Final\myappBack)
2020-12-15 16:39:28.415 INFO 2384 --- [ main] c.a.myapp.myappApplication : No active profile set, falling back to default profiles: default
2020-12-15 16:39:29.382 INFO 2384 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-12-15 16:39:29.496 INFO 2384 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 108ms. Found 8 JPA repository interfaces.
2020-12-15 16:39:30.032 INFO 2384 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-12-15 16:39:30.040 INFO 2384 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-12-15 16:39:30.040 INFO 2384 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-12-15 16:39:30.396 INFO 2384 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-12-15 16:39:30.396 INFO 2384 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1922 ms
2020-12-15 16:39:30.498 WARN 2384 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2020-12-15 16:39:30.500 INFO 2384 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2020-12-15 16:39:30.513 INFO 2384 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-12-15 16:39:30.519 ERROR 2384 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
Process finished with exit code 1
server.port=8081
server.servlet.session.timeout=1200
# JDBC URL of the database.
spring.datasource.url=jdbc:mysql://localhost:3306/myapp?zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC
# Login username of the database.
spring.datasource.username= root
# Login password of the database.
spring.datasource.password=
# Fully qualified name of the JDBC driver. Auto-detected based on the URL by default.
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Whether to enable logging of SQL statements.
spring.jpa.show-sql=true
# DDL mode. This is actually a shortcut for the "hibernate.hbm2ddl.auto" property. Defaults to "create-drop" when using an embedded database and no schema manager was detected. Otherwise, defaults to "none".
spring.jpa.hibernate.ddl-auto=update
# Additional native properties to set on the JPA provider.
spring.jpa.database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.storage.storage_engine=innodb
# Avoid to restart server (only during dev phase) if DevTools are uninstall
# spring.thymeleaf.cache=false
# spring.security.user.name="root"
# spring.security.user.password="123"
spring.resources.add-mappings=true
The problem is written Reason: Failed to determine a suitable driver class. Classloader can not find driver-class-name in you classpath. Maybe missing MySQL library definition in the maven / gradle configuration.
Maven:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
Gradle:
runtimeOnly 'mysql:mysql-connector-java'
If MySQL library is existed, try to change driver-class-name definition as below.
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
... instead of
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
it works, just force to reload the maven dependency and is ok;
just warning to fix
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/Admin/Downloads/myapp%202.0%20Final/myappBack%202.0%20-%20Final/myappBack/target/myappService/WEB-INF/lib/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/Admin/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
Check your mysql-connector-java version jar
if it's 5 it should be
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
if its 8 it should be
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Also I don't think you should have spaces after equal sign where is your username and password
Try and remove the spring.datasource.class-name property from the config file.
The url should be good enough to tell which classname is to be used.
Also set "logging.level.root=debug" in the properties to get some more details. See if that helps.
I have a Spring boot project that should connect with an instance of Cloud SQL with spring-cloud-gcp-starter-sql-postgresql in order to avoid the explicit use of an IP in the project.
So far, It connects well but it delayed a lot (around 30 seconds to start) because it tries to connect via SSL socket and after a lot of tries, it connects.
In the logs there is a line that says:
2020-02-19 00:10:09.809 INFO 6779 --- [ main] o.s.c.g.a.s.GcpCloudSqlAutoConfiguration : Default POSTGRESQL JdbcUrl provider. Connecting to jdbc:postgresql://google/test?socketFactory=com.google.cloud.sql.postgres.SocketFactory&cloudSqlInstance=XXXXXX:us-central1:test&useSSL=false with driver org.postgresql.Driver
As long as I know, the parameter useSSL=false won't work for postgresql. The correct one is ssl=false but when I try to overwrite the JDBC Url with the application.yml, It prints the following log:
2020-02-19 00:10:09.816 WARN 6779 --- [ main] o.s.c.g.a.s.GcpCloudSqlAutoConfiguration : Ignoring provided spring.datasource.url. Overwriting it based on the spring.cloud.gcp.sql.instance-connection-name.
I suspect that the delay is because of the SSL connection. So I have two questions:
How can I avoid the use of SSL connection? Since I am not setting a JDBC URL explicitly, I cannot use the ssl=false in the parameter.
I suspect that is delaying because the SSL Client certificate is not set. If this is the case, how can I set it? I already have the .pem but I don't know how to implement it
I add my application.yml with configurations and the mentioned log:
application.yml
spring:
cloud:
gcp:
project-id: xxxxxxx
config
sql:
instance-connection-name: xxxxxxx:us-central1:test
database-name: test
enabled: true
datasource:
username: test
password: 123456
initialization-mode: always
jpa:
hibernate:
ddl-auto: update
The credentials are set in a environment variable "GOOGLE_APPLICATION_CREDENTIALS" calling the service account json
Log:
2020-02-19 00:10:09.692 INFO 6779 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-02-19 00:10:09.699 INFO 6779 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-02-19 00:10:09.699 INFO 6779 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.30]
2020-02-19 00:10:09.767 INFO 6779 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-02-19 00:10:09.767 INFO 6779 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 790 ms
2020-02-19 00:10:09.809 INFO 6779 --- [ main] o.s.c.g.a.s.GcpCloudSqlAutoConfiguration : Default POSTGRESQL JdbcUrl provider. Connecting to jdbc:postgresql://google/test?socketFactory=com.google.cloud.sql.postgres.SocketFactory&cloudSqlInstance=XXXXXXX:us-central1:test&useSSL=false with driver org.postgresql.Driver
2020-02-19 00:10:09.816 WARN 6779 --- [ main] o.s.c.g.a.s.GcpCloudSqlAutoConfiguration : Ignoring provided spring.datasource.url. Overwriting it based on the spring.cloud.gcp.sql.instance-connection-name.
2020-02-19 00:10:09.885 INFO 6779 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-02-19 00:10:09.940 INFO 6779 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.4.10.Final}
2020-02-19 00:10:10.039 INFO 6779 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-02-19 00:10:10.109 INFO 6779 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-02-19 00:10:10.193 INFO 6779 --- [ main] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:10.193 INFO 6779 --- [ main] c.g.cloud.sql.core.CoreSocketFactory : First Cloud SQL connection, generating RSA key pair.
2020-02-19 00:10:13.690 INFO 6779 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2020-02-19 00:10:13.711 INFO 6779 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL10Dialect
2020-02-19 00:10:13.791 INFO 6779 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:15.042 INFO 6779 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:16.333 INFO 6779 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:17.653 INFO 6779 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:19.314 INFO 6779 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:20.643 INFO 6779 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:21.938 INFO 6779 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:23.227 INFO 6779 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:24.561 INFO 6779 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:35.164 INFO 6779 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-02-19 00:10:35.173 INFO 6779 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-02-19 00:10:35.294 WARN 6779 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-02-19 00:10:35.507 INFO 6779 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-02-19 00:10:35.611 INFO 6779 --- [ main] o.s.c.g.core.DefaultCredentialsProvider : Default credentials provider for service account cloud-code#XXXXXXX.iam.gserviceaccount.com
2020-02-19 00:10:35.612 INFO 6779 --- [ main] o.s.c.g.core.DefaultCredentialsProvider : Scopes in use by default credentials: [https://www.googleapis.com/auth/pubsub, https://www.googleapis.com/auth/spanner.admin, https://www.googleapis.com/auth/spanner.data, https://www.googleapis.com/auth/datastore, https://www.googleapis.com/auth/sqlservice.admin, https://www.googleapis.com/auth/devstorage.read_only, https://www.googleapis.com/auth/devstorage.read_write, https://www.googleapis.com/auth/cloudruntimeconfig, https://www.googleapis.com/auth/trace.append, https://www.googleapis.com/auth/cloud-platform, https://www.googleapis.com/auth/cloud-vision, https://www.googleapis.com/auth/bigquery]
2020-02-19 00:10:35.612 INFO 6779 --- [ main] o.s.c.g.a.c.GcpContextAutoConfiguration : The default project ID is XXXXXXX
2020-02-19 00:10:35.730 INFO 6779 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-02-19 00:10:35.733 INFO 6779 --- [ main] c.r.c.CloudSqlTestApplication : Started CloudSqlTestApplication in 27.027 seconds (JVM running for 27.464)
Also, just in case it helps, i am adding my pom.xml:
<?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.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ramonparis</groupId>
<artifactId>cloud-sql-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cloud-sql-test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-sql-postgresql</artifactId>
</dependency>
<dependency>
........
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
there are several ways you can connect to Cloud SQL , in your case you should use a sockect
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>postgres-socket-factory</artifactId>
<version>1.0.15</version>
</dependency>
spring.datasource.url: jdbc:postgresql://google/cloudSqlInstance=${instance}&socketFactory=com.google.cloud.sql.postgres.SocketFactory
the other way is using a cloud_sql_proxy (more compless and i would indicate if you are using on GKE) Like :
wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
chmod +x cloud_sql_proxy
create a proxy user :
gcloud iam service-accounts create proxy-user --display-name "proxy-account-user"
gcloud projects add-iam-policy-binding [PROJECT_ID] --member \
serviceAccount:[SERVICE_ACCOUNT_EMAIL] --role roles/cloudsql.client
gcloud iam service-accounts keys create key.json --iam-account [SERVICE_ACCOUNT_EMAIL]
./cloud_sql_proxy -instances=[INSTANCE_CONNECTION_NAME]=tcp:5432 -credential_file=key.json &
now you are listenning to your instance db : check the video (www.youtube.com/watch?v=iKoaiH_xYB8)
I am setting up a Configuration manager using Spring cloud config, I'm having an issue where the Client is not picking up the property source (Hosted HTTPS Server URI) when I deploy the application to a Docker container on Azure, works when the same code is running on Localhost which picks the same URI, thoughts on how to proceed ?
I have provided all the required parameters including the app name and the SERVER URI along with the active profile (dev) and label (1.0) in the bootstrap.properties, I also tried disabling security by setting management.security.enabled to false thinking it is a Security issue, but nothing seems to locate the property source. (Worst case is the HTTPS server URI seems to be located when the client is running locally), It is only when the code is containerized using Docker and deployed to Azure, it is showing this behavior. Any help greatly appreciated !
P.S - Working on latest snapshots of SpringBoot 2.1.3 and latest snapshot of Spring Cloud starter 2.1.1, No problems with any of the dependencies I have added.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<org.apache.commons.version>3.5</org.apache.commons.version>
<java.version>1.8</java.version>
<lombok.version>1.16.12</lombok.version>
<apt.version>1.1.3</apt.version>
<querydsl.version>4.1.4</querydsl.version>
<log4j-api.version>2.6</log4j-api.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
bootstrap.properties
spring.cloud.config.uri=example.org
spring.application.name=MyApp
spring.profiles.active=dev
spring.cloud.config.profile=dev
spring.cloud.config.label=1.0
application.properties
management.endpoints.web.exposure.include=*
Expected when running on Localhost
2019-04-03 14:48:42.499 INFO 50217 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : example.org
2019-04-03 14:48:43.961 INFO 50217 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=MyApp, profiles=[dev], label=1.0, version=null, state=null
2019-04-03 14:48:43.961 INFO 50217 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='MyApp-dev'}]}
2019-04-03 14:48:43.965 INFO 50217 --- [ main] c.s.c.backroom.claims.ClaimsApplication : The following profiles are active: dev
2019-04-03 14:48:44.985 INFO 50217 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-04-03 14:48:45.135 INFO 50217 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 138ms. Found 17 repository interfaces.
2019-04-03 14:48:45.929 INFO 50217 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=1f523ae6-9bf7-3733-ae13-ea87a67e1564
201
Actual Result when running on Docker container on Azure
2019-04-03T17:35:03.470926214Z 2019-04-03 17:35:03.470 INFO 8 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : example.org
2019-04-03T17:35:09.723760869Z 2019-04-03 17:35:09.723 WARN 8 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: Could not extract response: no suitable HttpMessageConverter found for response type [class org.springframework.cloud.config.environment.Environment] and content type [text/html;charset=ISO-8859-1]
2019-04-03T17:35:09.752928427Z 2019-04-03 17:35:09.752 INFO 8 --- [ main] c.s.c.backroom.claims.ClaimsApplication : The following profiles are active: dev
2019-04-03T17:35:19.974721803Z 2019-04-03 17:35:19.971 INFO 8 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.