Spring boot with Google Cloud SQL - java

im trying to deploy my spring boot app to Google App Engine Flex Environment and G Cloud MySQL DB.
I'm having issues connecting to the db.
Tried already some variants, but all unsuccessful.
spring-boot-with-google-cloud-datastore-api-fails-to-run
My properties:
spring:
profiles: googlecloud
jpa:
database: MYSQL
show-sql: false
hibernate:
ddl-auto: update
datasource:
url: jdbc:mysql://google/myproject?cloudSqlInstance=XXXX&user=xxx&password=xxx
My pom.xml (the db dependencies only):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
With this configuration, im getting:
Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.SocketFactory
...Error creating repository beans...
java.lang.NoClassDefFoundError: com/mysql/jdbc/SocketFactory

You can read more details about the change from 5x to 6x connector :
https://dev.mysql.com/doc/connector-j/6.0/en/connector-j-api-changes.html
For the NCDF exception, according to this issue :
https://github.com/GoogleCloudPlatform/cloud-sql-mysql-socket-factory/issues/18
I think you have to change your dependencies :
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory-connector-j-6</artifactId>
<version>1.0.2</version>
</dependency>

Related

Unable to load config data from 'aws-parameterstore:' when using Spring Boot with AWS Parameter store

Spring Boot newbie here. I am trying to read credentials from AWS Parameter Store. To do that, I have added the following dependencies to pom.xml:
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-starter-aws</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-parameter-store-config</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.4</version>
</dependency>
I am using Spring Boot version version 2.7.4. When I run the application, it fails to boot and prints the following error:
Caused by: java.lang.IllegalStateException: Unable to load config data from 'aws-parameterstore:'
How do I read credentials from AWS parameter store?
Update
There is a parameter stored at the path /config/myap_dev/dummy. I have added the following config to my bootstrap.properties:
spring.application.name=myapp
aws.paramstore.defaultContext=application
aws.paramstore.profileSeparator=_
aws.paramstore.prefix=/config
aws.paramstore.enabled=true
and in my application-dev.yaml I do the following to retrieve the parameter:
spring.config.import: "aws-parameterstore:"
param: "${dummy}"
I am running the application using mvn spring-boot:run.

Unable to integrate Hazelcast version 4.2 with Spring Boot 2.3.6.RELEASE

I am using Spring Boot version 2.3.6.RELEASE in my application and added hazelcast version 4.2 and wanted to use spring-integration-hazelcast of version 3.0.0 as this only is compatible with hazelcast 4.2 version.
But getting below error during build:
Require upper bound dependencies error for org.springframework.integration:spring-integration-core:5.3.4.RELEASE paths to dependency are:
+-package:ocapi-admin-service:4.0.0-SNAPSHOT
+-org.springframework.integration:spring-integration-core:5.3.4.RELEASE
and
+-package:ocapi-admin-service:4.0.0-SNAPSHOT
+-org.springframework.integration:spring-integration-hazelcast:3.0.0
+-org.springframework.integration:spring-integration-core:5.3.4.RELEASE (managed) <-- org.springframework.integration:spring-integration-core:5.4.0
]
Below is the pom snippet:
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-hazelcast</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>4.2</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-spring</artifactId>
<version>4.2</version>
</dependency>
I think you need to update the version of spring-integration-core to at least 5.4.0.
You don't specify it in the code snippet from pom.xml you shared, but I guess it must be taken from somewhere else since you have it in the logs. Try changing this dependency to the following one:
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>5.4.0</version>
</dependency>

PostgreSQL ‘database does not exist’ - Java Spring Boot, Flyway, Docker/PostgreSQL

I’m following this tutorial: https://www.youtube.com/watch?v=vtPkZShrvXQ
… and I am having trouble with database migrations. I am using Spring Boot 2.2.7, and I have created a PostgreSQL database called “demodb”
When I run the program, the console gives the error:
org.postgresql.util.PSQLException: FATAL: database "demodb" does not exist
Here is my application.yml file, which contains the database info:
app:
datasource:
plaltform: postgres
jdbc-url: jdbc:postgresql://localhost:5432/demodb
username: postgres
password: password
pool-size: 30
Here are my dependencies in the pom.xml file:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
</dependencies>
I'm running my migrations in a separate fold, and like I said, the database "demodb" DOES exist (I created it from the terminal), so I’m not sure why I’m getting this error. Any ideas?
So I was following along the same tutorial. Then I switched to the same persons video on installing postgres: https://youtu.be/4smnWU0BhrA?t=811
At the time stamp I linked you end up starting postgres in the mac application. Make sure you stop it. The issue for me was that since I started that postgres before the one inside docker, that original postgres was listening on port 5432. And that original postgres did not have demodb.
If that is not the solution try to kill whatever is listening to port 5432, then try again or restart the docker instance.
lsof -i :5432
Get the PID, lets say its 1001, then do:
kill -9 1001
could be related to a typo in your application.yml file...
app:
datasource:
platform: postgres
(your wrote plaltform)
other than that I wonder why you don't use the default Spring Boot properties to configure your database connection:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: dbc:postgresql://localhost:5432/demodb
username: postgres
password: password
please also have a look at this reference of Spring Boot properties: https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#data-properties

Uncaught exception from servlet java.lang.RuntimeException: Driver org.hsqldb.jdbc.JDBCDriver claims to not accept jdbcUrl, jdbc:mysql:///iworks_db

I am working on a Java Google App Engine app, and when I deploy my app and open it on my browser I get the above error. iworks_db is the name of my database, and for some reason my app fails to connect to it. I'm using the guide found here: https://cloud.google.com/sql/docs/mysql/connect-app-engine.
My createConnectionPool method:
private DataSource createConnectionPool() {
HikariConfig config = new HikariConfig();
config.setJdbcUrl(String.format("jdbc:mysql:///%s", DB_NAME));
config.setUsername(DB_USER);
config.setPassword(DB_PASS);
config.setDriverClassName("org.hsqldb.jdbc.JDBCDriver");// This line does NOT exist on the guide
// I added it because I was getting "failed
// to get driver instance" error
config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.mysql.SocketFactory");
config.addDataSourceProperty("cloudSqlInstance", CLOUD_SQL_CONNECTION_NAME);
config.addDataSourceProperty("useSSL", "false");
config.setMaximumPoolSize(5);
config.setMinimumIdle(5);
config.setConnectionTimeout(10000); // 10 seconds
config.setIdleTimeout(600000); // 10 minutes
config.setMaxLifetime(1800000); // 30 minutes
DataSource pool = new HikariDataSource(config);
return pool;
}
The dependencies in my pom.xml file:
<!-- Compile/runtime dependencies -->
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>1.9.59</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.2</version>
</dependency>
Any help would be much appreciated!
Your URL is a mysql url while your driver is HyperSQL DataBase (hsqldb). That is the reason you get the exception regarding not acceptable jdbcUrl.
Your DB is Google Cloud mysql, which I believe is the case as the rest of your code use mysql factory (com.google.cloud.sql.mysql.SocketFactory), then change the driver class name to following:
config.setDriverClassName("com.mysql.jdbc.GoogleDriver");
Also replace the hsldb maven dependency with
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory-connector-j-8</artifactId>
<version>1.0.14</version>
</dependency>
The com.google.cloud.sql.mysql.SocketFactory is provided in the mysql-socket-factory-connector-j-8 maven artifact.
Update:
You could also try not to use the DriverClassName at all.
Try adding the above two dependencies and remove the setDriverClassName line.

Connecting Squirrel Client to Embedded Teiid Server

I have a Spring Boot project, on which I have some csv files and have converted them into entities and querying them based on my requirement. For this approach, I am using Teiid Spring Boot Starter, which is starting a embedded server -
This is the console startup log -
Starting embedded database: url='jdbc:teiid:spring;PassthroughAuthentication=true;useCallingThread=true;autoFailover=true;waitForLoad=5000;autoCommitTxn=OFF;disableLocalTxn=true', username='null' ````
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.teiid</groupId>
<artifactId>teiid-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.teiid</groupId>
<artifactId>teiid-12.1.1-jdbc</artifactId>
<version>12.2.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/teiid-12.2.1-jdbc.jar</systemPath>
</dependency>
</dependencies>
application.properties
spring.application.name=Teiid-spring-boot
spring.teiid.model.package=com.example.demo.model
spring.teiid.file.parent-directory=src/main/resources/csv
#######
logging.level.org.teiid.spring=TRACE
spring.main.allow-bean-definition-overriding=true
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
I have Squirrel SQL client setup, have added the driver for Teiid as well.
teiid-12.2.1-jdbc.jar
Squirrel SQL connection settings used -
name - Teiid
example url - jdbc:teiid:spring
website url - http://teiid.org
But while connecting, getting this following error -
teiid: JDBC Driver class not found
class java.lang.ClassNotFoundException: org.jboss.modules.ModuleLoadException
There are multiple issues that need to be fixed.
You do not need the teiid-12.1.1-jdbc dependency in pom.xml
Add teiid.jdbc-enable=true to your application.properties that will open a jdbc port 31000 for the application you built.
Run your application
Then add the Teiid JDBC driver to SquirreL (which you seemed to be already done)
Use the URL as jdbc:teiid:spring#mm://localhost:31000 where localhost is host where you are running your teiid-spring application.

Categories