HHH-6936 EntityManagerImpl.close() and EntityManagerImpl.isOpen() should check if the session factory is closed
This commit is contained in:
parent
5e3775afe8
commit
76a3ad2021
|
@ -23,13 +23,11 @@
|
|||
*/
|
||||
package org.hibernate.ejb;
|
||||
|
||||
import java.util.Map;
|
||||
import javax.persistence.PersistenceContextType;
|
||||
import javax.persistence.PersistenceException;
|
||||
import javax.persistence.spi.PersistenceUnitTransactionType;
|
||||
import javax.transaction.Synchronization;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.Interceptor;
|
||||
|
@ -39,6 +37,7 @@ import org.hibernate.annotations.common.util.ReflectHelper;
|
|||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.ejb.internal.EntityManagerMessageLogger;
|
||||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
/**
|
||||
* Hibernate implementation of {@link javax.persistence.EntityManager}.
|
||||
|
@ -127,6 +126,7 @@ public class EntityManagerImpl extends AbstractEntityManagerImpl {
|
|||
}
|
||||
|
||||
public void close() {
|
||||
checkEntityManagerFactory();
|
||||
if ( !open ) {
|
||||
throw new IllegalStateException( "EntityManager is closed" );
|
||||
}
|
||||
|
@ -157,6 +157,7 @@ public class EntityManagerImpl extends AbstractEntityManagerImpl {
|
|||
|
||||
public boolean isOpen() {
|
||||
//adjustFlushMode(); //don't adjust, can't be done on closed EM
|
||||
checkEntityManagerFactory();
|
||||
try {
|
||||
if ( open ) {
|
||||
getSession().isOpen(); //to force enlistment in tx
|
||||
|
@ -169,4 +170,9 @@ public class EntityManagerImpl extends AbstractEntityManagerImpl {
|
|||
}
|
||||
}
|
||||
|
||||
private void checkEntityManagerFactory() {
|
||||
if (! getEntityManagerFactory().isOpen()) {
|
||||
open = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,12 +24,6 @@
|
|||
*/
|
||||
package org.hibernate.ejb.test;
|
||||
|
||||
import javax.persistence.EntityExistsException;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import javax.persistence.FlushModeType;
|
||||
import javax.persistence.PersistenceException;
|
||||
import javax.persistence.Query;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
|
@ -38,6 +32,12 @@ import java.io.ObjectOutputStream;
|
|||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import javax.persistence.EntityExistsException;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import javax.persistence.FlushModeType;
|
||||
import javax.persistence.PersistenceException;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import org.hibernate.FlushMode;
|
||||
import org.hibernate.HibernateException;
|
||||
|
@ -46,11 +46,8 @@ import org.hibernate.ejb.AvailableSettings;
|
|||
import org.hibernate.ejb.HibernateEntityManager;
|
||||
import org.hibernate.ejb.HibernateEntityManagerFactory;
|
||||
import org.hibernate.stat.Statistics;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
@ -428,4 +425,23 @@ public class EntityManagerTest extends BaseEntityManagerFunctionalTestCase {
|
|||
em.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFactoryClosed() throws Exception {
|
||||
EntityManager em = createIsolatedEntityManager();
|
||||
assertTrue( em.isOpen() );
|
||||
assertTrue( em.getEntityManagerFactory().isOpen());
|
||||
|
||||
em.getEntityManagerFactory().close(); // closing the entity manager factory should close the EM
|
||||
assertFalse(em.isOpen());
|
||||
|
||||
try {
|
||||
em.close();
|
||||
fail("closing entity manager that uses a closed session factory, must throw IllegalStateException");
|
||||
}
|
||||
catch( IllegalStateException expected) {
|
||||
// success
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue