From 7c006f7fcd2258d5253dc9e573aaa433201572ae Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Sat, 9 May 2015 12:59:23 +0100 Subject: [PATCH] HHH-8898 Change the Session API to enable auto-closeable when running on Java7 --- hibernate-core/src/main/java/org/hibernate/Session.java | 5 ++--- .../org/hibernate/engine/spi/SessionDelegatorBaseImpl.java | 4 ++-- .../src/main/java/org/hibernate/internal/SessionImpl.java | 7 ++++--- .../test/java/org/hibernate/test/legacy/FooBarTest.java | 3 +-- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/Session.java b/hibernate-core/src/main/java/org/hibernate/Session.java index 383cf855f4..94c651c7b2 100644 --- a/hibernate-core/src/main/java/org/hibernate/Session.java +++ b/hibernate-core/src/main/java/org/hibernate/Session.java @@ -89,7 +89,7 @@ import org.hibernate.stat.SessionStatistics; * @see SessionFactory * @author Gavin King */ -public interface Session extends SharedSessionContract { +public interface Session extends SharedSessionContract, java.io.Closeable { /** * Obtain a {@link Session} builder with the ability to grab certain information from this session. * @@ -164,10 +164,9 @@ public interface Session extends SharedSessionContract { * 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; + public void close() throws HibernateException; /** * Cancel the execution of the current query. diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java index 9cff127b75..5bb4aa96e4 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java @@ -473,8 +473,8 @@ public class SessionDelegatorBaseImpl implements SessionImplementor, Session { } @Override - public Connection close() throws HibernateException { - return session.close(); + public void close() throws HibernateException { + session.close(); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java index f9d4149ecb..d8edf850c7 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java @@ -389,7 +389,7 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc } @Override - public Connection close() throws HibernateException { + public void close() throws HibernateException { LOG.trace( "Closing session" ); if ( isClosed() ) { throw new SessionException( "Session was already closed" ); @@ -402,7 +402,8 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc try { if ( !isTransactionCoordinatorShared ) { - return jdbcCoordinator.close(); + jdbcCoordinator.close(); + return; } else { if ( getActionQueue().hasBeforeTransactionActions() || getActionQueue().hasAfterTransactionActions() ) { @@ -410,7 +411,7 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc "On close, shared Session had before / after transaction actions that have not yet been processed" ); } - return null; + return; } } finally { diff --git a/hibernate-core/src/test/java/org/hibernate/test/legacy/FooBarTest.java b/hibernate-core/src/test/java/org/hibernate/test/legacy/FooBarTest.java index 42478f3af8..78809c99c1 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/legacy/FooBarTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/legacy/FooBarTest.java @@ -4075,7 +4075,7 @@ public class FooBarTest extends LegacyTestCase { throw e; } finally { - assertTrue( s.close()==null ); + s.close(); } } @@ -4489,7 +4489,6 @@ public class FooBarTest extends LegacyTestCase { tx = s.beginTransaction(); s.createQuery( "from Fo" ).list(); tx.commit(); - assertTrue( s.close() == c ); c.close(); }