Cant connect dockerized springboot application to not dockerized postgres database (localhost) [duplicate] - java

This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(40 answers)
Closed 1 year ago.
Im getting the connection refused error, but in my eclipse it connects with no problem. I wonder if it is not possible to connect to a database that hasnt been dockerized also or if its possible what should I do?
thise is my Dockerfile:
FROM openjdk:11
EXPOSE 8080
ADD target/permisos.jar permisos.jar
ENTRYPOINT ["java","-jar","/permisos.jar"]
this is my application.yml file:
spring:
profiles:
active: local
jpa:
database: POSTGRESQL
show-sql: true
hibernate:
ddl-auto: none
datasource:
platform: postgres
url: jdbc:postgresql://localhost:5432/permisosdb
username: postgres
password: postgres
driverClassName: org.postgresql.Driver

The IP address of the host is host.docker.internal on, AFAIK, all modern Docker installations.
So your datasource URL should be
url: jdbc:postgresql://host.docker.internal:5432/permisosdb
If that doesn't work, try
url: jdbc:postgresql://172.17.0.1:5432/permisosdb

Related

javax.net.ssl.SSLHandshakeException on spring boot application

The spring boot app should send an email after the registration to the user.
When I test the registration with postman, I got this error message..
"message": "Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 1025;\n nested exception is:\n\tjavax.net.ssl.SSLHandshakeException: Remote host terminated the handshake. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 1025;\n nested exception is:\n\tjavax.net.ssl.SSLHandshakeException: Remote host terminated the handshake"
**
As mail server I use mailDev in a docker container. The container terminal logs these messages:
MailDev webapp running at http://0.0.0.0:80
MailDev SMTP Server running at 0.0.0.0:25
The Logs sound fine, but despite this I have no access to the web app of the mail-server (I use a MacBook). Is there maybe a fault at the mail server?
The app is running with this properties..
server:
error:
include-message: always
include-binding-errors: always
spring:
datasource:
password: password
url: jdbc:postgresql://localhost:5432/postgres
username: postgres
jpa:
hibernate:
ddl-auto: create-drop
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true
show-sql: true
mail:
host: localhost
port: 1025
username: hello
password: hello
properties:
mail:
smtp:
auth: true
socketFactory:
class: javax.net.ssl.SSLSocketFactory
fallback: false
port: 1025
ssl:
enable: true
The project is public under my GitHub profile
https://github.com/farmerWhoCode/TrainingBooking
I find some hints here at stackoverflow, that point out wrong properties in my application.yml file. But nothing helps me to solve this problem.

How to connect azure postgres from my microservice

I have a microservice to read data from json and to save in local postgres but now i want to use azure postgres instead of local DB. When i try to connect using application properties im getting timeout error. Is there anything im missing?
In my connection security i have enabled Allow access to Azure services, i have also added my ip address in firewall rule and my SSl setting is disabled.
My application properties file for local postgres, which is working fine
spring.datasource.url=jdbc:postgresql://localhost:5432/MyDB
spring.datasource.username=postgres
spring.datasource.password=1234
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.platform=postgres
My application properties for azure postgres which is not working
spring.datasource.url : myapp.postgres.database.azure.com/test
spring.datasource.username: adminmyapp#myapp
spring.datasource.password: 123456
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.platform=postgres
Is my application properties correct?
I think you need to mention the driver as well,
spring.datasource.url=jdbc:postgresql://myapp.postgres.database.azure.com:5432/MyDB

Not able to access H2 Console in jhipster

I am not able to open H2 console on the web browser.
My jhipster application is running on 8088 port. In server logs I am getting that H2 database is available on port 18088.
Tried below.
http://localhost:18088/h2-console
Getting Below response.
Below are the details in application-dev.yml.
devtools:
restart:
enabled: true
additional-exclude: .h2.server.properties
livereload:
enabled: false # we use Webpack dev server + BrowserSync for livereload
jackson:
serialization:
indent-output: true
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:h2:mem:jhipstersampleapplication;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
username: jhipsterSampleApplication
password:
hikari:
poolName: Hikari
auto-commit: false
h2:
console:
enabled: true
jpa:
database-platform: io.github.jhipster.domain.util.FixedH2Dialect
database: H2
show-sql: true
properties:
hibernate.id.new_generator_mappings: true
hibernate.connection.provider_disables_autocommit: true
hibernate.cache.use_second_level_cache: true
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: false
Please Help me out with this.
Adding slash solved my problem http://localhost:8080/h2-console to http://localhost:8080/h2-console/
There are 2 different ports:
HTTP port 8088 for h2 web console, it's served by your web app (same as your REST API)
TCP port 18088 that can be used by other applications using JDBC, it can't be used from a browser. This port is opened in DatabaseConfiguration.java in your project
Go to root of the app url (Access Url that you see in the application start)
then type "/h2-console" in the end.
localhost:8081/services//h2-console
Ensure you are accessing the correct port (8088 in your case), and that you do not change jhipster's setting for h2.console.enabled.
http://localhost:8088/h2-console should work.

Grails + mysql : The driver has not received any packets from the server

I am attempting to connect my grails application to a Mysql database running locally via MAMP. I am getting the following error, and nothing else. I've tried every configuration. I don't think this is a Grails issue but more of a configuration issue. Any help would be most appreciated.
The driver has not received any packets from the server.
Here is my application.yml
environments:
development:
dataSource:
dbCreate: update
driverClassName: "com.mysql.jdbc.Driver"
dialect: org.hibernate.dialect.MySQL5Dialect
url: "jdbc:mysql://localhost:3306/development"
username: "root"
password: ""
I've tried with the port, without port different dialects and still no luck. Any ideas on what could be going on?
Thanks in advance for your help.

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)

Categories