HHH-12097 - EntityManagerFactory open/closed checks per JPA spec
This commit is contained in:
parent
f669c4bcdf
commit
dced921456
|
@ -735,8 +735,8 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
|
|||
*/
|
||||
public void close() throws HibernateException {
|
||||
if ( isClosed ) {
|
||||
if ( sessionFactoryOptions.isJpaBootstrap() ) {
|
||||
throw new IllegalStateException( "EntityManagerFactory is closed" );
|
||||
if ( getSessionFactoryOptions().getJpaCompliance().isJpaClosedComplianceEnabled() ) {
|
||||
throw new IllegalStateException( "EntityManagerFactory is already closed" );
|
||||
}
|
||||
|
||||
LOG.trace( "Already closed" );
|
||||
|
|
|
@ -10,7 +10,10 @@ import java.util.Collections;
|
|||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.boot.spi.SessionFactoryBuilderImplementor;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseUnitTestCase;
|
||||
|
@ -26,103 +29,113 @@ import static org.junit.Assert.fail;
|
|||
public class ClosedFactoryTests extends BaseUnitTestCase {
|
||||
@Test
|
||||
public void testClosedChecks() {
|
||||
final SessionFactoryBuilderImplementor factoryBuilder = (SessionFactoryBuilderImplementor) new MetadataSources().buildMetadata()
|
||||
.getSessionFactoryBuilder();
|
||||
factoryBuilder.markAsJpaBootstrap();
|
||||
final SessionFactory sf = factoryBuilder.build();
|
||||
|
||||
sf.close();
|
||||
|
||||
assertTrue( sf.isClosed() );
|
||||
|
||||
// we now have a closed SF (EMF)... test the closed checks in various methods
|
||||
final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder()
|
||||
.applySetting( AvailableSettings.JPA_CLOSED_COMPLIANCE, "true" )
|
||||
.build();
|
||||
|
||||
try {
|
||||
sf.getCache();
|
||||
fail( "#getCache did not fail" );
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
// this is the expected outcome
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail( "#getCache failed, but not with the expected IllegalStateException : " + e.toString() );
|
||||
}
|
||||
final SessionFactoryBuilderImplementor factoryBuilder = (SessionFactoryBuilderImplementor) new MetadataSources()
|
||||
.buildMetadata()
|
||||
.getSessionFactoryBuilder();
|
||||
final SessionFactory sf = factoryBuilder.build();
|
||||
|
||||
try {
|
||||
sf.getMetamodel();
|
||||
fail( "#getMetamodel did not fail" );
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
// this is the expected outcome
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail( "#getMetamodel failed, but not with the expected IllegalStateException : " + e.toString() );
|
||||
}
|
||||
|
||||
try {
|
||||
sf.getCriteriaBuilder();
|
||||
fail( "#getCriteriaBuilder did not fail" );
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
// this is the expected outcome
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail( "#getCriteriaBuilder failed, but not with the expected IllegalStateException : " + e.toString() );
|
||||
}
|
||||
|
||||
try {
|
||||
sf.getProperties();
|
||||
fail( "#getProperties did not fail" );
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
// this is the expected outcome
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail( "#getProperties failed, but not with the expected IllegalStateException : " + e.toString() );
|
||||
}
|
||||
|
||||
try {
|
||||
sf.getPersistenceUnitUtil();
|
||||
fail( "#getPersistenceUnitUtil did not fail" );
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
// this is the expected outcome
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail( "#getPersistenceUnitUtil failed, but not with the expected IllegalStateException : " + e.toString() );
|
||||
}
|
||||
|
||||
try {
|
||||
sf.close();
|
||||
fail( "#close did not fail" );
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
// this is the expected outcome
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail( "#close failed, but not with the expected IllegalStateException : " + e.toString() );
|
||||
}
|
||||
|
||||
try {
|
||||
sf.createEntityManager();
|
||||
fail( "#createEntityManager did not fail" );
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
// this is the expected outcome
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail( "#createEntityManager failed, but not with the expected IllegalStateException : " + e.toString() );
|
||||
}
|
||||
assertTrue( sf.isClosed() );
|
||||
|
||||
try {
|
||||
sf.createEntityManager( Collections.emptyMap() );
|
||||
fail( "#createEntityManager(Map) did not fail" );
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
// this is the expected outcome
|
||||
// we now have a closed SF (EMF)... test the closed checks in various methods
|
||||
|
||||
try {
|
||||
sf.getCache();
|
||||
fail( "#getCache did not fail" );
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
// this is the expected outcome
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail( "#getCache failed, but not with the expected IllegalStateException : " + e.toString() );
|
||||
}
|
||||
|
||||
try {
|
||||
sf.getMetamodel();
|
||||
fail( "#getMetamodel did not fail" );
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
// this is the expected outcome
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail( "#getMetamodel failed, but not with the expected IllegalStateException : " + e.toString() );
|
||||
}
|
||||
|
||||
try {
|
||||
sf.getCriteriaBuilder();
|
||||
fail( "#getCriteriaBuilder did not fail" );
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
// this is the expected outcome
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail( "#getCriteriaBuilder failed, but not with the expected IllegalStateException : " + e.toString() );
|
||||
}
|
||||
|
||||
try {
|
||||
sf.getProperties();
|
||||
fail( "#getProperties did not fail" );
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
// this is the expected outcome
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail( "#getProperties failed, but not with the expected IllegalStateException : " + e.toString() );
|
||||
}
|
||||
|
||||
try {
|
||||
sf.getPersistenceUnitUtil();
|
||||
fail( "#getPersistenceUnitUtil did not fail" );
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
// this is the expected outcome
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail( "#getPersistenceUnitUtil failed, but not with the expected IllegalStateException : " + e.toString() );
|
||||
}
|
||||
|
||||
try {
|
||||
sf.close();
|
||||
fail( "#close did not fail" );
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
// this is the expected outcome
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail( "#close failed, but not with the expected IllegalStateException : " + e.toString() );
|
||||
}
|
||||
|
||||
try {
|
||||
sf.createEntityManager();
|
||||
fail( "#createEntityManager did not fail" );
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
// this is the expected outcome
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail( "#createEntityManager failed, but not with the expected IllegalStateException : " + e.toString() );
|
||||
}
|
||||
|
||||
try {
|
||||
sf.createEntityManager( Collections.emptyMap() );
|
||||
fail( "#createEntityManager(Map) did not fail" );
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
// this is the expected outcome
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail( "#createEntityManager(Map) failed, but not with the expected IllegalStateException : " + e.toString() );
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail( "#createEntityManager(Map) failed, but not with the expected IllegalStateException : " + e.toString() );
|
||||
// if an exception is
|
||||
StandardServiceRegistryBuilder.destroy( ssr );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue