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


(Marcos Pereira) #7

Hi @gavin,

We made some changes in Play to make this work. See the following pull requests:

  1. https://github.com/playframework/playframework/pull/8502
  2. https://github.com/playframework/playframework/pull/8524

So, in Play 2.6.17+ you will be able to use the configuration as explained by @schmitch.

Thanks for sharing your solution here. These are MySQL connection options, right? Not sure that you are aware of it, but keep in mind these configurations are not always recommended. From MySQL docs:

Although not recommended, you can make the driver perform failovers without invalidating the active Statement or ResultSet instances by setting either the parameter autoReconnect or autoReconnectForPools to true . This allows the client to continue using the same object instances after a failover event, without taking any exceptional measures. This, however, may lead to unexpected results: for example, if the driver is connected to the primary host with read/write access mode and it fails-over to a secondary host in real-only mode, further attempts to issue data-changing queries will result in errors, and the client will not be aware of that. This limitation is particularly relevant when using data streaming: after the failover, the ResultSet looks to be alright, but the underlying connection may have changed already, and no backing cursor is available anymore.

Best.