Recover from missing database connection at startup
Asked by Christian:

I just realized [recovering from a lost database connection](https://confluence.atlassian.com/doc/surviving-database-connection-closures-297665664.html) does not work when there was no database connection during startup at all. It will [stuck](https://confluence.atlassian.com/confkb/startup-check-confluence-won-t-start-as-it-cannot-connect-to-a-database-953127209.html) in the *Confluence had problems starting up* page forever.

Is there a Confluence way to self heal this situation? Or at least an option such that Confluence will restart in that situation? That would already fix the problem.

FYI: I am running Confluence on a QNAP NAS through Container Station using the official latest docker image (which works great btw). Each time I boot my NAS, postgres will be available about 3 minutes after Confluence already started and therefore fails to start. Postgres runs through a different platform, so there is no elegant way to bound their lifecycles together. Anyway, a Confluence solution would be the robust way, anyway.


Answers:
author: Christian
body: Hi Daniel,

thank you for your help. I did a bit of a research especially for the delay approach and found a life hack more than enough for my home deployment. It's quite robust and based on delays, restarts and checking the database TCP port to respond until Confluence will be started.

In a linux environment tools like netcat and the so-called wait-for-it script (on github) are really helpful. The latter one is also recommended by docker. I've just placed that script into the confluence home folder to survive container updates, together with a start script and created a docker-compose yaml file.

For those few (or many?) people that run Confluence on a QNAP through Container-Station like me: you can create the yaml files through ContainerStation \> Create \> Create Application.

++docker compose yaml:++

```
version: '3'

services:
  confluence:
    image: atlassian/confluence-server:latest
    ports:
      - 32769:8090
    volumes:
      - /share/Container/container-station-data/application/confluence-server/:/var/atlassian/application-data/confluence/
    command: ["/var/atlassian/application-data/confluence/wait-for-it.sh", "nas:5432", "-s", "--timeout=0", "--", "/var/atlassian/application-data/confluence/start_confluence.sh"]
    deploy:
      restart_policy:
        condition: always
        delay: 5s
```

++start_confluence.sh++

```
#!/bin/sh
sleep 30
/entrypoint.py

```

cheers,

Christian


author: Daniel Ebers
body: Hi Christian,

welcome to Atlassian Community!

During startup Confluence expects the database to be there and ready. Your setup is valid (thanks for explaining!) but, I assume, when the application was designed such a scenario was not considered.

Two possibilities which come to mind are:

1. check if Confluence is in the state of an error presenting the page "had problems starting up" and restart it if it is the case. To my knowledge there is no ready-made solution - you would have to code this yourself.
2. delay the Confluence startup for the time you know the database is needing to come up.

Please keep in mind there are only two theories of somewhat 'lifehack fixes' - in a data center-like scenario such workarounds are rarely needed :) Maybe you could put the database on a host where it comes alive faster.

Cheers,  
Daniel