Sunday, 19 October 2008

Boiler Plating Database Resource Cleanup

I've been using Java for nearly twelve months now and I am finding that I like it. There are only two things that I have discovered so far that make make me wonder what the creators of Java were thinking: exception handling and layout managers. I'll cover layout managers in a later article.

Java is a garbage collected language. Which means that, most of the time, you don't have to worry how the memory previously used by dead objects is cleaned up. To me garbage collection has always felt a bit like a knee jerk reaction for people who can't use smart pointers properly in C++ and C programmers who must pay very close attention to the points at which they release memory allocated to the heap. If garbage collection is meant to help you clean up memory, why hasn't something been developed to help objects release resources? Java has finalizers, but as Joshua Bloch points out in Effective Java finalizers cannot be relied upon. Proper cleanup of resources is left to the developer whose only real friend is finally.

In this article I'll look briefly at the vast amount of exception handling code that must be written to cleanly close result set, statement and connection objects when accessing a database and then more in depth at one possible method of boiler plating it.

Read more.

1 comment:

  1. Good article (well, I didn't actually read to the end as I threw an exception and dropped out without cleaning up resources - what a mess!).

    This is situation is one of the things I like least about Java (the general propensity to verbosity and nouning and verbising everything, as well XML configuring to death, being the others). C# is somewhat better (with Using), but still places the burden on client code.

    You might also find my own blog entries relevant:

    and (more C# focused):