Implementing AutoCloseable or Closeable

2,060 views
Skip to first unread message

Henrik Nordvik

unread,
Dec 8, 2012, 6:55:05 PM12/8/12
to jd...@googlegroups.com
Hi,
Are there any plans on making the closeable stuff in JDBI (e.g. Handle) implement the AutoCloseable or Closeable interface so that they can be used in try-with-resource block in java 7?
Are there any reasons not to?

-
Henrik

Brian McCallister

unread,
Dec 8, 2012, 9:30:13 PM12/8/12
to jd...@googlegroups.com, jd...@googlegroups.com
AutoCloseble is a java7 thing, so not an option until we are willing to drop 1.6 support, and io.Closeable throws IOException, which is awkward, and would break backwards compatibility. 

One option would be something like:

try (Java7Handle h = new Java7Handle(dbi.open())) {
  ...
}

As this has to be explicitly newed up it would not break compat for old code. It might be tricky to compile jdbi, so maybe a companion project which is explicitly for 7.

Either way, it is my intention to do a JDBI3 when Java8 is finalized which will require 8 and take advantage of the shinies.



-
Henrik

--
 
 

Danilo Reinert

unread,
Dec 18, 2012, 3:38:46 PM12/18/12
to jd...@googlegroups.com
In the case of the Handle interface it won't hurt.
 
Just implement this little bit of code:

 public interface Handle extends AutoCloseable

In fact, i've already implemented it and i'm using try-whith-resources java 7 feature.
Greetings,
Danilo Reinert

Brian McCallister

unread,
Dec 18, 2012, 4:17:20 PM12/18/12
to jd...@googlegroups.com
On Tue, Dec 18, 2012 at 12:38 PM, Danilo Reinert <danilo...@gmail.com> wrote:
In the case of the Handle interface it won't hurt.
 
Just implement this little bit of code:

 public interface Handle extends AutoCloseable


This will, I believe, prevent use of JDBI on Java 6 -- AutoCloseable was introduced in 7.

-Brian

 
In fact, i've already implemented it and i'm using try-whith-resources java 7 feature.
Greetings,
Danilo Reinert
 

Em sábado, 8 de dezembro de 2012 23h30min13s UTC-3, Brian McCallister escreveu:

On Dec 8, 2012, at 4:55 PM, Henrik Nordvik <henr...@gmail.com> wrote:

Hi,
Are there any plans on making the closeable stuff in JDBI (e.g. Handle) implement the AutoCloseable or Closeable interface so that they can be used in try-with-resource block in java 7?
Are there any reasons not to?

AutoCloseble is a java7 thing, so not an option until we are willing to drop 1.6 support, and io.Closeable throws IOException, which is awkward, and would break backwards compatibility. 

One option would be something like:

try (Java7Handle h = new Java7Handle(dbi.open())) {
  ...
}

As this has to be explicitly newed up it would not break compat for old code. It might be tricky to compile jdbi, so maybe a companion project which is explicitly for 7.

Either way, it is my intention to do a JDBI3 when Java8 is finalized which will require 8 and take advantage of the shinies.



-
Henrik

--
 
 

--
 
 

Danilo Reinert

unread,
Dec 19, 2012, 7:12:53 AM12/19/12
to jd...@googlegroups.com
AutoCloseable throws Exception
 
the close method from Handle implementations throws RunTimeException, wich subclasses from Exception.
 
So, there will be no incompatibility.
 
AutoCloseable was designed to be compatible with already existing implementations of close methods, including Closeable interface, who throws IOException.
Actually, Closeable now extends AutoCloseable.
 
You can either use AutoCloseable for Handle interface, or declare your own interface extending from AutoCloseable.
 
Suppose:
 
public interface RunTimeCloseable extends AutoCloseable {
 
  void close() throws RunTimeException;
 
}
 
I would like to have this patch. =)

Danilo Reinert

unread,
Dec 19, 2012, 7:32:00 AM12/19/12
to jd...@googlegroups.com
Paraphrasing Oracle:
 
"
Making an Auto-Closeable Class

As you probably guessed already, a try-with-resources statement cannot manage every class. A new interface called java.lang.AutoCloseable was introduced in Java SE 7. All it does is provide a void method named close() that may throw a checked exception (java.lang.Exception). Any class willing to participate in try-with-resources statements should implement this interface. It is strongly recommended that implementing classes and sub-interfaces declare a more precise exception type than java.lang.Exception, or, even better, declare no exception type at all if invoking close() should not fail.

Such close() methods have been retro-fitted into many classes of the standard Java SE run-time environment , including the java.io, java.nio, javax.crypto, java.security, java.util.zip, java.util.jar, javax.net, and java.sql packages. The major advantage of this approach is that existing code continues working just as before, while new code can easily take advantage of the try-with-resources statement."

more at: http://www.oracle.com/technetwork/articles/java/trywithresources-401775.html

It worth reading. There is a new feature in Java 7 for exceptions called Supressed Exceptions, wich is even provided by try-with-resources technique.

 

Greetings,

D. Reinert

Steven Schlansker

unread,
Dec 19, 2012, 12:59:47 PM12/19/12
to jd...@googlegroups.com
On Dec 19, 2012, at 4:32 AM, Danilo Reinert <danilo...@gmail.com> wrote:

> Paraphrasing Oracle:
>
> "
> Making an Auto-Closeable Class
> As you probably guessed already, a try-with-resources statement cannot manage every class. A new interface called java.lang.AutoCloseable was introduced in Java SE 7. All it does is provide a void method named close() that may throw a checked exception (java.lang.Exception). Any class willing to participate in try-with-resources statements should implement this interface…



> .
>
> Hi,
> Are there any plans on making the closeable stuff in JDBI (e.g. Handle) implement the AutoCloseable or Closeable interface so that they can be used in try-with-resource block in java 7?
> Are there any reasons not to?

The problem has nothing to do with the (correctly identified as a non-issue) adding of another interface, as this would not break any existing consumers.
The problem is that AutoCloseable does not exist in Java < 7, so any users of JDBI still stuck on Java 6 would not be able to use it anymore -- JDBI would refer to classes that are not available to them, and the VM would throw linking errors.

There's a couple of (nontrivial) workarounds highlighted in the coin-dev mailing list:
http://mail.openjdk.java.net/pipermail/coin-dev/2011-February/003057.html
but it feels like a lot of effort for unfortunately little gain.

The *most* straightforward of these solutions involves doing two completely separate builds with different javac versions and different source files.

Henrik Nordvik

unread,
Jan 4, 2013, 2:43:13 PM1/4/13
to jd...@googlegroups.com


On Wednesday, December 19, 2012 6:59:47 PM UTC+1, Steven Schlansker wrote:

The problem has nothing to do with the (correctly identified as a non-issue) adding of another interface, as this would not break any existing consumers.
The problem is that AutoCloseable does not exist in Java < 7, so any users of JDBI still stuck on Java 6 would not be able to use it anymore -- JDBI would refer to classes that are not available to them, and the VM would throw linking errors.

There's a couple of (nontrivial) workarounds highlighted in the coin-dev mailing list:
http://mail.openjdk.java.net/pipermail/coin-dev/2011-February/003057.html
but it feels like a lot of effort for unfortunately little gain.

The *most* straightforward of these solutions involves doing two completely separate builds with different javac versions and different source files.

 
There's also the Closeable interface which is in JDK6 and can be used if you're throwing IOExceptions or not throwing checked exceptions. I tried changing Handle to extend from Closeable and it seems to work fine in both java 6 and 7 because it doesn't throw checked exceptions. Does this break anything else?
-
Henrik

Steven Schlansker

unread,
Jan 4, 2013, 3:04:28 PM1/4/13
to jd...@googlegroups.com
That's a good point. I'm surprised that nobody mentioned that Closeable extends AutoCloseable yet! I didn't even notice that…

http://docs.oracle.com/javase/7/docs/api/java/io/Closeable.html

This sounds like a good change to me, it would be awesome to be able to use try-with-resources. Maybe write up a pull request?

Brian McCallister

unread,
Jan 4, 2013, 3:30:09 PM1/4/13
to jd...@googlegroups.com
WOuld there be any binary compatibility issues with adding an interface? I don't *think* there is. 

You can implement the interface and leave off throws clauses, so we can just add it quietly.

-Brian



--



Brian McCallister

unread,
Jan 4, 2013, 4:12:16 PM1/4/13
to jd...@googlegroups.com
Checked behavior, we're good.

I added java.io.Closeable to Handle and ResultIterator, cutting 2.44 now.
Reply all
Reply to author
Forward
0 new messages