spring jpa authentication fail - spring-boot

I'm running postgres as docker container and using spring jpa trying to connect with the DB. simple operation. Even though, I gave right username and password, still JPA throws following exception
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "admin"
docker-compose.yaml
version: '3.9'
services:
postgres:
image: postgres:14-alpine
ports:
- 5432:5432
environment:
- POSTGRES_PASSWORD=admin
- POSTGRES_USER=admin
- POSTGRES_DB=mydb
Application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=admin
spring.datasource.password=admin
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true

Related

docker spring boot web services cannot connect to the docker mysql database

Any help or hint would be greatly appreciated.
My docker spring boot web services cannot connect to my docker mysql database.
I can connect to my docker mysql database from toad.
This is the error:
com.mysql.cj.jdbc.exceptions.CommunicationsException:
docker-compose.yaml:
version: "3.7"
services:
mysql:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: Jadeite1!
MYSQL_DATABASE: account
ports:
- "3306:3306"
networks:
- jadeite1000
accountwebservices:
image: bithead/accountwebservices:latest
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://localhost:3306/account
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: Jadeite1!
mem_limit: 700m
ports:
- "8080:8080"
networks:
- jadeite1000
networks:
jadeite1000: null
This is my application.properties file:
#Enabling MYSQL:
spring.datasource.url=jdbc:mysql://localhost:3306/account
spring.datasource.username=root
spring.datasource.password=Jadeite1!
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
server.port=8080
#spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
rosalind_url=https://uguc.outsystemsenterprise.com/Rosalind/rest/v1/
x_api_key=cc6c6117-ce9a-41a4-a2a0-34a992d27d5c
x_fapi_financial_id=UHCE00VKVHPGEYRZB906
spring.jackson.serialization.fail-on-empty-beans=false
account.app.jwtSecret= bezKoderSecretKey
account.app.jwtExpirationMs= 86400000
Your database and application runs in the separate container. So I recommend using the internal address and port instead of using external port and address for database connection.
SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/account

How dockerize Spring Boot app if Postgres db is going to be AWS RDS?

version: "3.7"
services:
api_service:
build: .
restart: always
ports:
- 8080:8080
depends_on:
- postgres_db
links:
- postgres_db:database
postgres_db:
image: "postgres:11.4"
restart: always
ports:
- 5435:5432
environment:
POSTGRES_DB: testDb
POSTGRES_PASSWORD: admin
this is my YAML file.
and I got properties
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://database:5432/testDb
spring.datasource.username=postgres
spring.datasource.password=admin
if my Postgres is rds then do I need to compose them or I just can go with Dockerfile for jar only and not YAML file?
You can create environment variables for the RDS address, RDS username, RDS password and RDS port. Pass it to the Dockerfile to the api_service. Your api_service should know to assemble Postgres connection string based on the environment variables. Please check - Spring Profiles in connection String
Those Spring properties values are probably incorrect in most of the environments in which you might run your application. In your unit-test environment the database might be H2 or HDBC instead of PostgreSQL; in a local-developer setup it will be on localhost; in RDS it will be a different name again. These host names and credentials should not be in your src/main/resources tree anywhere.
Spring allows you to set Spring properties using environment variables. So you can set e.g. spring.datasource.url at the point where you deploy your application. In your Compose setup, for example:
version: "3.8"
services:
api_service:
build: .
restart: always
ports:
- 8080:8080
depends_on:
- postgres_db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://postgres_db:5432/testDb
- SPRING_DATASOURCE_USERNAME=postgres
- SPRING_DATASOURCE_PASSWORD=admin
postgres_db:
image: "postgres:11.4"
restart: always
ports:
- 5435:5432
environment:
POSTGRES_DB: testDb
POSTGRES_PASSWORD: admin
If your production environment uses RDS, then it should be enough to remove the postgres_db container and change the SPRING_DATASOURCE_* environment variables to match the RDS host name and credentials. You don't have to recompile anything or change the contents of your jar file.

Why is springboot connecting to the default postgres database instead of my specified one?

So I've got a springboot backend that I'm trying to connect to a postgres database. I've got a docker-compose file for it, yet the backend seems to connect to the wrong database. It seems to be connecting to the default postgres database instead of my specified vve database.
My docker-compose
version: '3'
services:
vve-postgres:
image: postgres:14.2
environment:
- POSTGRES_DATABASE=vve
- POSTGRES_USER=vve
- POSTGRES_PASSWORD=admin
volumes:
- vve-postgres-volume:/var/lib/postgresql/data
ports:
- "5432:5432"
vve-api:
build: .
restart: on-failure
env_file: .env
environment:
- DATABASE_HOST
- DATABASE_PORT
- DATABASE_NAME
- DATABASE_USER
- DATABASE_PASSWORD
depends_on:
- vve-postgres
volumes:
- ./data:/data
ports:
- "8080:8080"
volumes:
vve-postgres-volume:
My Environment file
DATABASE_HOST=vve-postgres
DATABASE_PORT=5432
DATABASE_NAME=vve
DATABASE_USER=vve
DATABASE_PASSWORD=admin
My application.properties
spring.config.import=optional:file:.env[.properties]
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.sql.init.mode=always
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.url=jdbc:postgresql://${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}
spring.datasource.username=${DATABASE_USER}
spring.datasource.password=${DATABASE_PASSWORD}
server.error.include-stacktrace=never
server.error.include-message=always
Picture showing the two databases, I'm wrongly connected to the postgres#localhost
How do I make sure my back end connects to the right database?
Environment in docker compose should be key-value pair:
vve-api:
build: .
restart: on-failure
env_file: .env
environment:
- DATABASE_HOST: ${DATABASE_HOST}
- DATABASE_PORT: ${DATABASE_PORT}
...
Etc for each one

Docker : Trying to connect Spring Java Container app to MS Sql Server container : getting error : connection failed

I am trying to connect to docker container of ms sql server 2017, through java spring boot application, In containerized environment its not getting connected and giving me error :
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.
However If I run Spring app locally it gets connected to Ms sql server container.
My spring boot application.properties:
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=master
spring.datasource.username=sa
spring.datasource.password=Ms#12345
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql = true
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.hibernate.ddl-auto = update
spring.main.allow-bean-definition-overriding=true
Configured docker-compose.yml as below
version: '3'
services:
myteamapp-mssqlserver:
image: "mcr.microsoft.com/mssql/server:2017-latest"
networks:
- myteamapp-net
volumes:
- myteamapp-data:/var/opt/mssql
ports:
- 1433:1433
environment:
- SA_PASSWORD=Ms#12345
- ACCEPT_EULA=Y
- MSSQL_PID=Express
myteamapp-app:
image: webservices:latest
networks:
- myteamapp-net
ports:
- 8080:8080
depends_on:
- myteamapp-mssqlserver
myteamapp-ui:
image: myteamapp
networks:
- myteamapp-net
depends_on:
- myteamapp-app
ports:
- 4200:4200
networks:
myteamapp-net:
volumes:
myteamapp-data:
Let me know what is I am missing here.

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Fa

I am developing a web application by using the Spring Boot, Spring Cloud Config and Docker. There are 3 projects. One is springboot web, Spring Cloud Config Server and MySql Database. I have use the Spring Boot Web with Themeleaf. It's basically perform a CRUD operation. In the development phase it's working fine. But when I deploy it in the Docker
Spring-Boot webapp container(springboot-thymeleaf-web-crud) is giving me the following error--
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).
I have created 3 container on Docker Compose file.
version: '3'
services:
springboot-thymeleaf-web-crud:
image: numery/springboot-thymeleaf-web-crud:latest
networks:
- employee-network
ports:
- 8080:8080
depends_on:
- employee-database
- spring-cloud-config-server-employee
spring-cloud-config-server-employee:
image: numery/spring-cloud-config-server-employee:latest
networks:
- employee-network
ports:
- 8888:8888
employee-database:
image: mysql:5
networks:
- employee-network
environment:
- MYSQL_ROOT_PASSWORD=rootpassword
- MYSQL_DATABASE=employee_directory
volumes:
- /home/numery/Docker-Database/employee_directory:/var/lib/mysql
networks:
employee-network:
SpringBoot Web Application.properties is given below
spring.application.name=employee-service-mysql
server.port=8080
spring.cloud.config.uri=http://spring-cloud-config-server-
employee:8888
spring.profiles.active=dev
SpringBoot Config Server is providing the following properties--
spring.datasource.url: jdbc:mysql://employee-
database:3306/employee_directory?
useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.datasource.username: root
spring.datasource.password: rootpassword
spring.datasource.validationQuery: SELECT 1
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.tomcat.max-wait: 20000
spring.tomcat.max-active: 50
spring.tomcat.max-idle: 20
spring.tomcat.min-idle: 15
spring.jpa.properties.hibernate.dialect:
org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql: true
spring.jpa.format-sql: true
spring.jpa.database: MYSQL
spring.jpa.hibernate.ddl-auto: create
Out of these 3 containers Mysql and Spring Cloud Config container is working fine. But for the Spring Boot Webapp(springboot-thymeleaf-web-crud) is exiting by giving the error above.
Note: I use the same datasouce(Spring Data JPA) configuration on some other SpringBoot Rest API. Which are working fine. But this is the first time I am using SpringBoot Web. Do I need to explicitly define any configuration on the data-source.
Please help!!
The datasource URL, that is spring.datasource.url: jdbc:mysql://employee- may be wrong.
OR Should add your datasource url to your Application.properties file
The url should be in the format jdbc:oracle:thin:#hostname:portNumber/schemaName
For further details and clarification, Not using the hostname and port in jdbc url
I perform the following task to solve the problem--
1) We should have up and running the mysql
container.
2) Inspect mysql container which ip running--
docker inspect mysqlcontainer | grep IP
3) Add the IP of that mysql container to my
springboot web app--
"spring.datasource.url:
jdbc:mysql://172.20.0.2:3306/employee_directory?
useSSL=false&serverTimezone
=UTC&useLegacyDatetimeCode=false"
4) run mvn clean, mvn install and build the
SpringBoot Web image.
5) If we down the container we need to perform the
same task again. Because each time Mysql
container obtain the different ip.

Resources