[hibernate-dev] Allowing auto-close usage for Session

Sanne Grinovero sanne at hibernate.org
Sat Nov 15 16:12:55 EST 2014


I was expecting this to be a trivial and non-API breaking change, but
it turns out the close() method for Session doesn't return void, and
so is not compatible with the Closeable inteface:

   /**
    * End the session by releasing the JDBC connection and cleaning up.  It is
    * not strictly necessary to close the session but you must at least
    * {@link #disconnect()} it.
    *
    * @return the connection provided by the application or null.
    * @throws HibernateException Indicates problems cleaning up.
    */
   public Connection close() throws HibernateException;

So this change can't be applied in 4.3 yet, but I'd hope to finally
send a PR for 5.0 at least;

Assuming that some applications really need that Connection instance,
my proposal would be to split the functionality across two methods:

   public Connection closeAndReturnConnection();

   public void close();

Any better names / ideas?

Second question: we had previously decided to implement
java.io.Closeable, so that it would work also for users of Java6. But
if for the above reason, this is getting into master only, then I
guess we could reopen that subject: would you prefer to have it extend
just java.lang.AutoCloseable ?

Sanne


More information about the hibernate-dev mailing list