-
Henrik
--
public interface Handle extends AutoCloseable
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 ReinertEm sábado, 8 de dezembro de 2012 23h30min13s UTC-3, Brian McCallister escreveu:
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
--
--
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
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.
--