My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Downloads
Links

Goal

This project is intended to provide extensions to JDK7's try-with-resources (formerly called Automatic Resource Management (ARM), thus the project name} to support areas without AutoCloseable support. This library provides adapter objects that implement AutoCloseable to manage the resource lifecycle. This management comes at the cost of an additional proxy object allocation, a tradeoff that is often worth making to avoid resource leaks.

Updates

Oracle's Lance Anderson recently pushed JDBC 4.1 updates to OpenJDK 7 which includes AutoCloseable support for JDBC's Connection, ResultSet, and Statement types. See Joe Darcy's Project Coin: JDBC 4.1 and try-with-resources post for more details. These changes fortunately make this library redundant; however, I like to think that this prototype prompted the JDBC expert group into adopting the try-with-resources features sooner than later. In any case, the adapter pattern can still be applied to other existing types holding resources such as java.awt.Graphics that have not or will not be updated to implement AutoCloseable.

API documentation

Example usage:

Here's a simple example of wrapping a javax.sql.DataSource to have it provide AutoCloseable connections, statements, and result sets:

import static com.google.code.arm.sql.AutoCloseables.arm;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.google.code.arm.sql.AutoCloseConnection;
import com.google.code.arm.sql.AutoClosePreparedStatement;
import com.google.code.arm.sql.AutoCloseResultSet;

public class AutoCloseExample {
    public static void main(String[] args) throws Exception {
        DataSource dataSource = getDataSource(); // could be injected
        // "arm" the datasource to get an AutoCloseable instance
        try ( AutoCloseConnection c = arm(dataSource).getConnection();
              AutoClosePreparedStatement stmt = c.prepareStatement("select 1 from dual");
              AutoCloseResultSet rs = stmt.executeQuery() ) {

            while (rs.next()) {
                // Do something useful with the result set
            }
            // Note: the connection's transaction must be explicitly committed
            // or rolled back prior to closing either from Connection.close()
            // or the automatic resource management block construct.
        } catch (SQLException e) {
            e.printStackTrace(); // Includes suppressed exceptions (if any) from closing the resources
        }
        // The result set, prepared statement, and connection are all automatically closed
    }
}

Requirements

OpenJDK 7 with full ARM support (currently implemented in http://hg.openjdk.java.net/jdk7/tl/), specifically the following changesets:

  • Changeset c4d60bcce958
    • 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
    • 6911261: Project Coin: Retrofit Automatic Resource Management (ARM) support onto platform APIs
    • 6962571: Infinite loop in printing out Throwable stack traces with circular references
  • Changeset 425960cef714
    • 6963723: Project Coin: Retrofit more JDK classes for ARM
  • Changeset 13354e1abba7
    • 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
    • 6964740: Project Coin: More tests for ARM compiler changes
    • 6965277: Project Coin: Correctness issues in ARM implementation
    • 6967065: add -Xlint warning category for Automatic Resource Management (ARM)

References

Project Coin: ARM API

Project Coin: Updated ARM Spec

Project Coin: ARM Implementation

Proposal: Automatic Resource Management

Powered by Google Project Hosting