Is it possible to wait database connection?


#1

I add database configuration in application.conf file.
if database is ready, then play will start success,
but if database is not ready, play will exit with:
Configuration error: Configuration error[Cannot connect to database [default]]

so, Is possible to wait database ready and then continue to start play?
how to implement?


(Rich Dougherty) #2

You mean you want the database connection code to keep retrying until the database becomes available instead of throwing an error immediately?


#3

yes, I dont want play exit because of database connection.

is possible to retry database connection?


(Schmitt Christian) #4

It can be set with:

play.db.default.hikaricp.initializationFailTimeout = -1

(https://github.com/brettwooldridge/HikariCP#infrequently-used)

This means that any user would get an 500er Status Code if the database Exception is not handled.


#5

my current play version is 2.5.10,
and hikaricp.initializationFailTimeout seems do not work in 2.5.x

I try set initializationFailFast to false, but play will exit when database is not ready.

the error log is as following:
2018/04/11 02:32:30 Oops, cannot start the server.
2018/04/11 02:32:30 Configuration error: Configuration error[Cannot connect to database [default]]

2018/04/11 02:32:30 Caused by: java.net.ConnectException: Connection refused (Connection refused)
2018/04/11 02:32:30 rest exited with error: rest: exit status 255


#6

this issue can be fixed by jdbc connection options.
for example add the following options.
autoReconnect=true&failOverReadOnly=false

then, it will not exist when database is not ready to connect