Monday 6 April 2009

Resource Handling in Java: Summary

My last 5 blog entries have covered the thorny issue of how to handle ensuring resources are closed correctly. I recommended two solutions out of the various approaches. If you don't care about close() exceptions eclipsing ealier ones, go for the simple:


void foo() throws IOException {
    SomeResource r = new SomeResource();
    try {
        //do some stuff with r
    }
    finally {
        r.close();
    }
}

But don't forget to nest your try loops for multiple resources!
If you want to avoid the eclipsing problem, or you wish to avoid the nesting of try loops, then you need to write a framework API, which would enable you to write:

void foo() throws IOException {
    Closer closer = new Closer();//the new helper class
    SomeResource r1=null;
    SomeResource r2=null;
    try {
        //create and use r1 & r2
        closer.completed();
    }
    finally {
        closer.close(r2,r1);
    }
}

There are many subtle issues to decide upon when writing Closer so I have purposefully refrained from writing a specific implementation so that the argument is about the general design rather than the specifics. (For example, how we cope with classes that don't implement closable?). I may blog an example implementation at a later date.

2 comments:

  1. Your closer approach is incorrect when the code using the resources contains a break, continue, or return statement that exits the block containing the use of the resource.

    ReplyDelete
  2. There's not a problem exiting the block with a break/continue/return statement - you simply have to call closer.completed() before that. If you forget to do so then it will assert when that codepath executes.

    ReplyDelete