0

I have a Java Spring Boot app deployed on Microsoft Azure that is connected to a PostgreSQL database. I want to connect to the database using credentials stored in Azure as Connection strings. The problem is that when the app is starting, it fails because the environment variable is not valid.

In Azure, I configured a connection string called DBCONN_IP with "Custom" type. As the documentation says, this will be available as environment variable called CUSTOMCONNSTR_DBCONN_IP in app. In application.properties I have the following: database.ip = ${CUSTOMCONNSTR_DBCONN_IP:localhost} When application starts, the connection is refused, because the environment variable is not valid and it replaces it with localhost, therefore the application is not started at all. However, I added a simple controller that just returns the value of another environment variable set as a connection string:

    @GetMapping
    public ResponseEntity<String> test() {
        try {
            String envString = System.getenv("CUSTOMCONNSTR_TEST_ENV_VARIABLE");
            return new ResponseEntity<>(envString, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.FORBIDDEN);
        }
    }

This works and the correct value is returned. My question is why the connection string accessed in application.properties is invalid? Should I set something else in Azure or am I missing something?

New contributor
paulfilip is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
5
  • those are 2 different variables: 1.CUSTOMCONNSTR_DBCONN_IP and 2. CUSTOMCONNSTR_TEST_ENV_VARIABLE
    – J Asgarov
    Commented 1 hour ago
  • Yes, indeed. In the first phase, I tried only with CUSTOMCONNSTR_TEST_ENV_VARIABLE in the controller and it worked. Then, I added a new connection string to establish the db connection. The point is why it worked in code, but not in application.properties
    – paulfilip
    Commented 1 hour ago
  • why don't u try printing out the correct variable CUSTOMCONNSTR_DBCONN_IP and see which value is being returned?
    – J Asgarov
    Commented 1 hour ago
  • Just did it now. It prints the right value set in Azure. However, the connection to DB is still refused if I try to access this env variable in application.properties
    – paulfilip
    Commented 56 mins ago
  • That doesn't make any sense. I would suggest to remove :localhost part so that it connects with the correct value and see what the error message is, in the logs
    – J Asgarov
    Commented 55 mins ago

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.