HHH-12473 - EntityManager.close() should throw an ISE if called on already closed EntityManager

This commit is contained in:
Steve Ebersole 2018-04-11 16:02:03 -05:00
parent e2b7317560
commit a99fecca29
3 changed files with 42 additions and 3 deletions

View File

@ -403,7 +403,21 @@ public final class SessionImpl
@Override @Override
@SuppressWarnings("StatementWithEmptyBody")
public void close() throws HibernateException { public void close() throws HibernateException {
if ( isClosed() ) {
if ( getFactory().getSessionFactoryOptions().getJpaCompliance().isJpaClosedComplianceEnabled() ) {
throw new IllegalStateException( "Illegal call to #close() on already closed Session/EntityManager" );
}
log.trace( "Already closed" );
return;
}
closeWithoutOpenChecks();
}
public void closeWithoutOpenChecks() throws HibernateException {
log.tracef( "Closing session [%s]", getSessionIdentifier() ); log.tracef( "Closing session [%s]", getSessionIdentifier() );
// todo : we want this check if usage is JPA, but not native Hibernate usage // todo : we want this check if usage is JPA, but not native Hibernate usage
@ -515,7 +529,7 @@ public final class SessionImpl
private void managedClose() { private void managedClose() {
log.trace( "Automatically closing session" ); log.trace( "Automatically closing session" );
close(); closeWithoutOpenChecks();
} }
@Override @Override

View File

@ -58,6 +58,7 @@ public class EntityManagerTest extends BaseEntityManagerFunctionalTestCase {
@SuppressWarnings( {"unchecked"}) @SuppressWarnings( {"unchecked"})
protected void addConfigOptions(Map options) { protected void addConfigOptions(Map options) {
options.put( Environment.GENERATE_STATISTICS, "true" ); options.put( Environment.GENERATE_STATISTICS, "true" );
options.put( Environment.JPA_CLOSED_COMPLIANCE, "true" );
} }
@Override @Override

View File

@ -6,9 +6,9 @@
*/ */
package org.hibernate.test.jpa.compliance.tck2_2; package org.hibernate.test.jpa.compliance.tck2_2;
import javax.persistence.Parameter;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query; import org.hibernate.query.Query;
import org.hibernate.test.jpa.AbstractJPATest; import org.hibernate.test.jpa.AbstractJPATest;
@ -23,6 +23,12 @@ import static org.junit.Assert.fail;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class ClosedManagerTests extends AbstractJPATest { public class ClosedManagerTests extends AbstractJPATest {
@Override
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setProperty( AvailableSettings.JPA_CLOSED_COMPLIANCE, "true" );
}
@Test @Test
public void testQuerySetMaxResults() { public void testQuerySetMaxResults() {
final Session session = sessionFactory().openSession(); final Session session = sessionFactory().openSession();
@ -139,4 +145,22 @@ public class ClosedManagerTests extends AbstractJPATest {
catch (IllegalStateException expected) { catch (IllegalStateException expected) {
} }
} }
@Test
public void testClose() {
final Session session = sessionFactory().openSession();
// 1st call - should be ok
session.close();
try {
// 2nd should fail (JPA compliance enabled)
session.close();
fail();
}
catch (IllegalStateException expected) {
// expected outcome
}
}
} }