HHH-8385 - Check whether EMF is closed (throwing ISE if so) when indicated methods are called

This commit is contained in:
Steve Ebersole 2013-07-26 18:26:33 -05:00
parent c101258e03
commit 033bf4a22e
1 changed files with 17 additions and 8 deletions

View File

@ -309,6 +309,8 @@ public class EntityManagerFactoryImpl implements HibernateEntityManagerFactory {
@Override @Override
public EntityManager createEntityManager(SynchronizationType synchronizationType, Map map) { public EntityManager createEntityManager(SynchronizationType synchronizationType, Map map) {
validateNotClosed();
//TODO support discardOnClose, persistencecontexttype?, interceptor, //TODO support discardOnClose, persistencecontexttype?, interceptor,
return new EntityManagerImpl( return new EntityManagerImpl(
this, this,
@ -322,42 +324,49 @@ public class EntityManagerFactoryImpl implements HibernateEntityManagerFactory {
} }
public CriteriaBuilder getCriteriaBuilder() { public CriteriaBuilder getCriteriaBuilder() {
validateNotClosed();
return criteriaBuilder; return criteriaBuilder;
} }
public Metamodel getMetamodel() { public Metamodel getMetamodel() {
validateNotClosed();
return metamodel; return metamodel;
} }
public void close() { public void close() {
// The spec says so, that's why :(
validateNotClosed();
sessionFactory.close(); sessionFactory.close();
EntityManagerFactoryRegistry.INSTANCE.removeEntityManagerFactory(entityManagerFactoryName, this); EntityManagerFactoryRegistry.INSTANCE.removeEntityManagerFactory(entityManagerFactoryName, this);
} }
public Map<String, Object> getProperties() { public Map<String, Object> getProperties() {
validateNotClosed();
return properties; return properties;
} }
public Cache getCache() { public Cache getCache() {
validateNotClosed();
// TODO : cache the cache reference? // TODO : cache the cache reference?
if ( ! isOpen() ) {
throw new IllegalStateException("EntityManagerFactory is closed");
}
return new JPACache( sessionFactory ); return new JPACache( sessionFactory );
} }
public PersistenceUnitUtil getPersistenceUnitUtil() { protected void validateNotClosed() {
if ( ! isOpen() ) { if ( ! isOpen() ) {
throw new IllegalStateException( "EntityManagerFactory is closed" ); throw new IllegalStateException( "EntityManagerFactory is closed" );
} }
}
public PersistenceUnitUtil getPersistenceUnitUtil() {
validateNotClosed();
return util; return util;
} }
@Override @Override
public void addNamedQuery(String name, Query query) { public void addNamedQuery(String name, Query query) {
if ( ! isOpen() ) { validateNotClosed();
throw new IllegalStateException( "EntityManagerFactory is closed" );
}
if ( StoredProcedureQueryImpl.class.isInstance( query ) ) { if ( StoredProcedureQueryImpl.class.isInstance( query ) ) {
final ProcedureCall procedureCall = ( (StoredProcedureQueryImpl) query ).getHibernateProcedureCall(); final ProcedureCall procedureCall = ( (StoredProcedureQueryImpl) query ).getHibernateProcedureCall();