HHH-12473 - EntityManager.close() should throw an ISE if called on already closed EntityManager
This commit is contained in:
parent
e2b7317560
commit
a99fecca29
|
@ -403,7 +403,21 @@ public final class SessionImpl
|
|||
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("StatementWithEmptyBody")
|
||||
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() );
|
||||
|
||||
// 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() {
|
||||
log.trace( "Automatically closing session" );
|
||||
close();
|
||||
closeWithoutOpenChecks();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -58,6 +58,7 @@ public class EntityManagerTest extends BaseEntityManagerFunctionalTestCase {
|
|||
@SuppressWarnings( {"unchecked"})
|
||||
protected void addConfigOptions(Map options) {
|
||||
options.put( Environment.GENERATE_STATISTICS, "true" );
|
||||
options.put( Environment.JPA_CLOSED_COMPLIANCE, "true" );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -6,9 +6,9 @@
|
|||
*/
|
||||
package org.hibernate.test.jpa.compliance.tck2_2;
|
||||
|
||||
import javax.persistence.Parameter;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.query.Query;
|
||||
|
||||
import org.hibernate.test.jpa.AbstractJPATest;
|
||||
|
@ -23,6 +23,12 @@ import static org.junit.Assert.fail;
|
|||
* @author Steve Ebersole
|
||||
*/
|
||||
public class ClosedManagerTests extends AbstractJPATest {
|
||||
@Override
|
||||
public void configure(Configuration cfg) {
|
||||
super.configure( cfg );
|
||||
cfg.setProperty( AvailableSettings.JPA_CLOSED_COMPLIANCE, "true" );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuerySetMaxResults() {
|
||||
final Session session = sessionFactory().openSession();
|
||||
|
@ -139,4 +145,22 @@ public class ClosedManagerTests extends AbstractJPATest {
|
|||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue