Quit Hardcoding Your *#%^ Database Connections!

Over and over I encounter Java code that hard codes database connection creation or resorts to pulling all of the connection parameters from a properties file bundled in with the .WAR or .JAR, or even a resource bundle (yes, that’s you Kasai). QUIT IT!
There are these things in Java called data sources. They are an object you can go to and ask for a database connection, it hands it off, you use it to perform a query or two, you close it. That’s it. If any database pooling, testing of new connections, etc. takes place it is hidden inside the data source and you don’t have to think about it. In fact, because some data source implementions do things like pooling and testing of connections, they will probably work better than that five line version of “how to create a JDBC connection” you found online and pasted into your code.
Just about any type of server or framework you might choose to use around your application typically makes it easy to create data sources, specify all the details of how they connect to a database, and all you have to do is perform a couple of calls to get a data source. In the majority of your code, you don’t even worry about the details of how the data source was obtained, you just accept it as one of the parameters to your class or to your method and you use it.
If your application isn’t running inside a J2EE server, or Tomcat, or Spring, or some other place where a data source object is easy to come by, you can use the Jakarta Commons DBCP library to quickly create one of your own (hopefully configured using parameters that can be easily changed).
Here’s an example of what I’m talking about that takes its configuration from system parameters:

BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(System.getProperty("driverClassName"));
dataSource.setUsername(System.getProperty("username"));
dataSource.setPassword(System.getProperty("password"));
dataSource.setUrl(System.getProperty("url"));

There you go, a data source that has caching and all kinds of other features you can tweak if you need them. Once you’ve got it you can call dataSource.getConnection() and out pops the connection ready to use. If you are running inside of Tomcat or WebLogic or something like that then you have even less of an excuse. In Tomcat for example, they provide a way to create data sources inside the Administration interface, you can just go to a web page, log in and create them. Retrieving one of the data sources you created in your code is this simple:

// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
// Look up our data source by the name we gave it when we created it. In this case that's "jdbc/EmployeeDB".
DataSource ds = (DataSource) envCtx.lookup("jdbc/EmployeeDB");

If you keep making everybody jump through hoops to set up your database connections when all we want to do is configure a data source, we’ll just start rewriting your code. If you are writing a library to be used by other people, or an application that has to be configured by someone other than you, make it take a data source!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s