In order to make deployment easier I want my application to self create pre-defined roles on the database.
If for some reason it is the first time running on the current database but it fails to create the roles, PE, there was a connection error, the application immediately terminates with an Environment.Exit(-1);
.
Is this a bad practice? If yes, why? What are my alternatives?
CodePudding user response:
Yes it is bad practice, however it depends on who uses this. Are you the only one or are people going to be disturbed by a force quit with no explanation?
Showing an alert/message with a little bit of explanations or an error code would be useful in a lot of cases. Log the error you get from the database if possible and if the users from your app know what to do with it you can also share it in the alert/message.
CodePudding user response:
As long as you know why the application terminated, it could be fine. If this is in Production, you also should get an alert if this happens.
At the very least, you should log the error. For a DB connection, you can add logic to try to reconnect either for some time or forever depending on your requirements. Depending on the error you get back from the database, you can either try again or not.
In my applications, I take an approach of forever attempts but make sure to log any errors I get back such as bad credentials or timeout. It is also good to have a 3rd system to monitor for errors and alert you.