ARM API Support

David Schlosnagle schlosna at gmail.com
Sat Jul 24 18:38:33 PDT 2010


On Wed, Jun 23, 2010 at 8:08 PM, Joe Darcy <joe.darcy at oracle.com> wrote:
> Compared to the API support in earlier versions of the ARM proposal, the
> top-level interface to mark participation in ARM is in package java.lang
> rather than its own package and, after consultation with the JDBC and
> graphics teams, neither java.sql.* nor java.awt.Graphics were
> retrofitted for ARM.

I'm a little disappointed that JDBC wasn't retrofitted for
AutoCloseable as this is one of the areas I've seen where resources
aren't always released appropriately and can have disasterous
consequences for the database. Can you provide any insight into the
reasoning why JDBC was not updated to implement AutoCloseable at this
time?

Many of the JDBC interfaces already have `public void close() throws
SQLException` methods (e.g. java.sql.Connection, java.sql.Statement,
java.sql.ResultSet, etc.), so it seems a small step to have them also
implement AutoCloseable. There are other JDBC interfaces that define a
`public void free() throws SQLException` (e.g. java.sql.Blob,
java.sql.Clob, etc.) that could benefit from AutoCloseable support,
but those would require incompatible changes.

Was one of the concerns JDBC providers needing to provide JDK7
specific versions of their drivers? The JDBC 4.0 update in Java 6
required Java 6 specific JDBC drivers, so there is at least some
precedent for major releases requiring new drivers (whether that was a
lesson learned and influenced the decision not to make JDBC changes is
another story).

As a chance to test out ARM, I created simple wrappers for most of the
JDBC API to provide support for AutoCloseable, and I'd love any
feedback: http://code.google.com/p/arm-extensions/

Here's a simple example of wrapping a javax.sql.DataSource to have it
provide AutoCloseable connections:

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
            }
        } 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
    }
}

Thanks,
Dave



More information about the coin-dev mailing list