Java try-with-resource

Prior to Java SE 7, you can use a finally block to ensure that a resource is closed regardless of whether the try statement completes normally or abruptly. The following example uses a finally block:

Connection conn = connectionPool.getConnection();
try {
  // All uses of conn should go here
  doStuff(conn);
} finally {
  // If you forget this, then you leak the connection.
  // And if you don't have the try-finally or equivalent, an exception
  // in doStuff can still lead to a leak.
  conn.close();
}
// At this point conn is closed, and attempting to use
// it results in an exception.

The exception thrown from the finally block would be propagated up the call stack, even if the exception thrown from the try block would probably be more relevant to propagate.

In Java 7 you can write the code from the example above using the try-with-resource construct like this:

try (Connection conn = connectionPool.getConnection()) { // All uses of conn should go here doStuff(conn); }

The Connection variable is declared inside the parentheses after the try keyword. Additionally, a Connection is instantiated and assigned to the variable.

When the try block finishes the Connection will be closed automatically. This is possible because Connection implements the Java interface java.lang.AutoCloseable. All classes implementing this interface can be used inside the try-with-resources construct.

If an exception is thrown both from inside the try-with-resources block, and when the Connection is closed (when close() is called), the exception thrown inside the try block is thrown to the outside world. The exception thrown when the Connection was closed is suppressed.

Some of the benefits of using try with resources in java are:

  • More readable code and easy to write.
  • Automatic resource management.
  • Number of lines of code is reduced.
  • No need of finally block just to close the resources.
  • We can open multiple resources in try-with-resources statement separated by a semicolon.

However, not every resource implements this interface.
In that case, lambdas and method references can be used to write more compact code.

The following example shows how SAP Java Connector JCoDestination class can be transformed into an instance of AutoCloseable using the method reference:

JCoDestination destination = (JCoDestination)session.getAttribute(“jco_destination”);
try (AutoCloseable jcodest = () -> JCoContext.end(destination) )
{ JCoContext.begin(destination);

}

Oglasi
Ovaj unos je objavljen u Nekategorizirano. Bookmarkirajte stalnu vezu.

Komentiraj

Popunite niže tražene podatke ili kliknite na neku od ikona za prijavu:

WordPress.com Logo

Ovaj komentar pišete koristeći vaš WordPress.com račun. Odjava /  Izmijeni )

Google+ photo

Ovaj komentar pišete koristeći vaš Google+ račun. Odjava /  Izmijeni )

Twitter picture

Ovaj komentar pišete koristeći vaš Twitter račun. Odjava /  Izmijeni )

Facebook slika

Ovaj komentar pišete koristeći vaš Facebook račun. Odjava /  Izmijeni )

Spajanje na %s