0

We have 10 separate projects that all access the same database. Initially, all 10 projects had database credentials hardcoded into them. I decided to move the credentials into a utility method and have all of the projects call that instead. For example

public String getDBUser() {
   return "username";
}

So instead of hardcoding it in 10 places, I hardcode it in one place. All was fine.

Fast-forward some months and I run into a wall with this solution, because now there are different credentials depending on which database server we're trying to connect to.

For context, I'm trying to set up properly separated testing/production environments, but the servers have different username and passwords.

One solution is to simply change the credentials to make them the same, but I imagine this is sometimes not an option and if I run into such a situation I would like to be prepared.

What is a good way to manage multiple database credentials across multiple applications?

1

In .NET, for example, connection strings are stored in web.config, which is an XML file in the app's directory. There you can store as many connection strings as you need. Then, in your app, you just configure which of those you need (reading the file and extracting the connection string), you configure this parameter as you go.

This is for one project. For many projects, the .NET solution is to add every connection string in a "master file" and then, when the project is built, the building process will copy the relevant connection string from the "master file" to the project's web.config.

I quickly tried to find some Java equivalent but I was unsuccessful. In Java the equivalent is persistence.xml (thanks, Bob!), so an option would be to transform that file. It shouldn't be too hard to do, even if it means running an extra command every time you change something to the master file.

Something about having the exact same credentials rubs me the wrong way, but that would depend on your architecture. Also, just as an extra suggestion if you haven't done so, take your time to properly store sensitive information. You never know...

  • Java EE environments often have a persistence.xml file which stores database credentials. – BobDalgleish Mar 14 '14 at 18:50
  • I guess it depends what a "project" is to get concerned about shared credentials. – JeffO Mar 14 '14 at 18:54
  • @JeffO What do you mean? – ArthurChamz Mar 14 '14 at 18:59
  • @ArthurChamz It's basically a single massive tool set that has been broken down into separate projects, where each project represents one tool. They all access the same database with the exact same permissions. I am not sure whether it would be useful to think about how the credentials should be stored since at some point they're going to have to be placed somewhere that the tools can read. – MxLDevs Mar 19 '14 at 18:49
  • The linked question seems to be relevant only to systems that involve users. The tools we use do not have a concept of "users". The only authentication required is to be able to access our database. – MxLDevs Mar 19 '14 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.