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

Steve Ebersole steve at hibernate.org
Sun Nov 16 11:57:58 EST 2014


Ugh.  The returning of Connection is a legacy thing that honestly makes no
sense imo.  I am sorry; I had forgotten it returned that.  I say it makes
no sense, because these are cases where the user has explicitly provided
Hibernate the Connection to use when opening the Session.  Its just silly
imo to then think it does not know that Connection later.

As you say, we are obviously stuck for 4.x.  But for 5.x my vote would
actually be to just change the return type, from:

   public Connection close() throws HibernateException;

to:

   public void close() throws HibernateException;

Then Closeable/AutoCloseable fits.

As for base lining on Java 7, I am just not seeing the benefit there.

On Sat, Nov 15, 2014 at 3:12 PM, Sanne Grinovero <sanne at hibernate.org>
wrote:

> 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
> _______________________________________________
> hibernate-dev mailing list
> hibernate-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/hibernate-dev
>


More information about the hibernate-dev mailing list