Skip to content

Commit d894af9

Browse files
fix: Check database connectivity only once
To avoid strain on the database, check connectivity only once on instance start (`lazy val`). We should see the log line exactly once per instance, regardless of how often the healthcheck endpoint is queried. Co-authored-by: Julia <JuliaBrigitte@users.noreply.github.com>
1 parent e462e3e commit d894af9

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

common/src/main/scala/db/SqlRegistrationRepository.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class SqlRegistrationRepository[F[_]: Async](xa: Transactor[F])
8282
* We just select one topic and a constant value as this is sufficient to check connectivity (we don't care about what data is returned).
8383
*/
8484
override def simpleSelectForHealthCheck(): Stream[F, TopicCount] = {
85+
logger.info("Performing a query to check DB connectivity")
8586
sql"""
8687
SELECT topic
8788
, 1

registration/app/registration/controllers/Main.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ final class Main(
3333

3434
private val logger: Logger = LoggerFactory.getLogger(this.getClass)
3535

36+
// Check if we can talk to the registration database
37+
private lazy val dbConnectivityCheck = registrar.dbHealthCheck()
3638

3739
def healthCheck: Action[AnyContent] = Action.async {
38-
// Check if we can talk to the registration database
39-
registrar.dbHealthCheck()
40+
dbConnectivityCheck
4041
.map(_ => {
4142
// This forces Play to close the connection rather than allowing
4243
// keep-alive (because the content length is unknown)

0 commit comments

Comments
 (0)