Home > Java, Java 7 > Java 7 Awesome Aspect-Automatic Resource Management

Java 7 Awesome Aspect-Automatic Resource Management

Introduction:

In my previous article,” Java 7 Awesome Features” I had missed one of the greatest aspect of Java 7.That is nothing but “Automatic Resource Management”. This article deals with the same.

Managing Resource is one of the keen thing in development. Because if we don’t manage the resource effectively it will cause lot of issues like PermGen Space Error, TooManyConnections Error etc.

For avoid those problems Java 7 introduce the great aspect “Automatic Resource Management”. By this feature java can automatically manage the resources effectively. So java frees the developer from lots of overhead. Lets have a prolix look on this.

Syntax:


try(Resource 1;Resource 2;Resource 3….)

{

//block of statements

}

catch(Exception e)

{

//block of statements

}

Here is simple code snippet which shows you the typical try-catch-finally block. In the code we create the jdbc connection for access the data from database.


Connection connection = null;

Statement statement = null;

try

{

connection =    DriverManager.getConnection(“databaseurl”,”username(opt)”,”password(opt)”);

statement = connection.createStatemnet();

boolean executionStatus= statement.execute(“query”);

}

catch(Exception e)

{

//Block of code for handle the exception

e.printStacktrace();

}

finally

{

try

{

statement.close();

connection.close();

}

catch(Exception e)

{

//block of statements for handle exceptions.

}

}

In the above snippet, suppose we forget to close the statement and connection, what happened? May be after some operations (which also open the connection) “Too Many connection error” will occurred.

But java 7 keeps us in safe zone from those kinds of errors by mange the resource automatically. In java 7 we can write the above snippet like this,


Connection connection = null;

Statement statement = null;

try(connection =    DriverManager.getConnection(“databaseurl”,”username(opt)”,”password(opt)”);

statement = connection.createStatemnet())

{

boolean executionStatus= statement.execute(“query”);

}

catch(Exception e)

{

//block of statements for handles the exceptions

}

In the above snippet, there are no overheads like close the connections and statements properly. Java 7 can manage those things automatically for us.

Note:

Java 7 mange the resources which are sub interfaces and implementing classes of AutoCloseable.So we can pass the resources which are extends or implements the interface AutoCloseable.

That’s all folks, I think this article makes you bit more knowledgeable about java 7 awesome aspect “Automatic resource Management”, If you feel the same, please leave your footprints(comments) here. Joyous coding day…..

  1. Selim
    December 21, 2010 at 10:39 am

    Very cool feature

  2. Tbee
    December 21, 2010 at 11:12 am

    It is allowed to move the actual variable declaration into the try-block as well, so:

    Statement statement = connection.createStatement();

    • December 22, 2010 at 1:34 pm

      Hi Tbee,
      Thanks for your informative comment.

  3. JayG
    December 21, 2010 at 4:02 pm

    Interesting. However, this takes away few lines of code but reduces the flexibility. For example, with this feature, if any Exception is raised and I want to handle the Exceptions differently for different scenarios i.e. if the Exception is due to not able to get connection I want to retry, but if it is due to closing resources, probably want to just log it and move on.

  4. Milan
    December 22, 2010 at 10:25 am

    Yes, I agree with JayG – it reduces flexibility. How can be solved the following situation?
    In your snipped I would like to use PreparedStatement (Subinterface of Statement), which I do not want to close? Usually I just change the input param of the preparedStatement and run it again. But in some cases (maybe) I need to close it after every execution.

    • December 22, 2010 at 1:32 pm

      Hi Jay & Milan,

      There is traditional try-catch-finally block is also available.More over Java 7 has the another feature Multi-Exception Catch.Have a look at my previous post “Java 7 awesome Features“.It makes you clear about this.So Java 7 satisfies your expectations also.Happy coding…

  5. April 20, 2013 at 7:01 am

    Good Post.. Keep it up.

  1. No trackbacks yet.

Leave a comment