3 months ago
Hi.
My Spring Boot backend service consistently fails to connect to my custom MySQL service with java.net.SocketTimeoutException: Connect timed outsuch that the backend gets stuck at "HikariPool-1 - Starting..." and never fully starts.
I am using:
Backend: Spring Boot app using custom Docker image, port 8080
MySQL: Custom Docker image based on mysql:8.0, port 3306 (Working no errors)
Frontend: React/nginx, working correctly (can display frontend)
MySQL Service:
Shows that it starts successfully and shows "ready for connections" in logs
Has public domain generated:
{RETRACTED}-mysql-production.up.railway.appEnvironment variables set correctly (MYSQL_ROOT_PASSWORD, MYSQL_DATABASE, etc.)
Backend Connection String:
jdbc:mysql://{RETRACTED-mysql-production.up.railway.app:3306/RETRACTED?useSSL=false&allowPublicKeyRetrieval=true
I have tried
public domain and internal
.railway.internalhostnames
Different connection parameters (timeouts, SSL settings)
Switched from MySQL 9.4.0 to MySQL 8.0
Verified MySQL service is running and accepting connections
so how can I fix this connection between the backend and database services?
Attachments
5 Replies
3 months ago
Hey there! We've found the following might help you get unblocked faster:
[🧵 Unable to acquire JDBC Connection HikariPool-1 - Connection is not available, request timed out - PostgreSQL
If you find the answer from one of these, please let us know by solving the thread!
rayrift
It looks like your Spring Boot app just can’t reach MySQL fast enough on Railway. The two biggest fixes people miss are:Use the internal hostname. In your application.properties (or yml), point tojdbc:mysql://mysql.railway.internal:3306/railwaywith the right username/password from Railway’s env vars. The public .up.railway.app host is for external connections and is much slower or blocked internally.Give HikariCP more time. Railway can take a bit to respond. Add higher timeouts and a smaller pool to start with, for example:spring.datasource.hikari.connection-timeout=60000 spring.datasource.hikari.maximum-pool-size=5 spring.datasource.hikari.minimum-idle=1 spring.datasource.hikari.idle-timeout=300000Double-check that your Spring Boot service and MySQL service are in the same Railway project and that your env vars (MYSQL_PASSWORD, MYSQL_DATABASE) match. If mysql.railway.internal doesn’t resolve, try the service name from your Railway dashboard instead.Most people find that switching to the internal host and tweaking the Hikari settings clears up the Connect timed out and pool startup errors.
3 months ago
Ok thanks that worked!
Finally would you happen to know why my HTTP requests such as POST are now GET requests with HTTP code 301 ?
I tried adding these settings to application.properties in springboot:
server.forward-headers-strategy=framework
server.tomcat.remoteip.remote-ip-header=x-forwarded-for
server.tomcat.remoteip.protocol-header=x-forwarded-proto
server.tomcat.remoteip.protocol-header-https-value=https
server.tomcat.internal-proxies=.*
server.ssl.enabled=falseadditionally I set the allowed origins to : config.setAllowedOrigins(List.of("http://localhost:3000", "http://localhost", "https://{RETRACTED}-frontend-production.up.railway.app", "https://*.railway.app"));
I have also included a screenshot of the requests which chows the NS_ERROR_REDIRECT_LOOP response possibly due to HTTP -> HTTPS redirection attempts?
3 months ago
Here is that screenshot for NS_ERROR_REDIRECT_LOOP as well as showing HTTP code 301 where GET should be POST
Attachments
Status changed to Solved noahd • 3 months ago
zkhan122
Here is that screenshot for NS_ERROR_REDIRECT_LOOP as well as showing HTTP code 301 where GET should be POST
3 months ago
fixed by using the correct urls...
Status changed to Awaiting Railway Response Railway • 3 months ago
3 months ago
Going to resolve this since it seems like you managed to get it working?
Status changed to Awaiting User Response Railway • 3 months ago
Status changed to Solved ray-chen • 3 months ago