Search in sources :

Example 1 with SessionFactoryImplementor

use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.

the class TypeFactorySerializationTest method testUnregisterSerializeRegisterDiffSessionFactory.

@Test
public void testUnregisterSerializeRegisterDiffSessionFactory() throws Exception {
    Configuration cfg = new Configuration().setProperty(AvailableSettings.SESSION_FACTORY_NAME, NAME).setProperty(AvailableSettings.SESSION_FACTORY_NAME_IS_JNDI, // default is true
    "false");
    SessionFactoryImplementor factory = (SessionFactoryImplementor) cfg.buildSessionFactory();
    assertSame(factory, SessionFactoryRegistry.INSTANCE.getNamedSessionFactory(NAME));
    // Remove the session factory from the registry
    SessionFactoryRegistry.INSTANCE.removeSessionFactory(factory.getUuid(), NAME, false, null);
    assertNull(SessionFactoryRegistry.INSTANCE.findSessionFactory(factory.getUuid(), NAME));
    TypeFactory typeFactory = factory.getTypeResolver().getTypeFactory();
    byte[] typeFactoryBytes = SerializationHelper.serialize(typeFactory);
    typeFactory = (TypeFactory) SerializationHelper.deserialize(typeFactoryBytes);
    try {
        typeFactory.resolveSessionFactory();
        fail("should have failed with HibernateException because session factory is not registered.");
    } catch (HibernateException ex) {
    // expected because the session factory is not registered.
    }
    // Now create a new session factory with the same name; it will have a different UUID.
    SessionFactoryImplementor factoryWithSameName = (SessionFactoryImplementor) cfg.buildSessionFactory();
    assertSame(factoryWithSameName, SessionFactoryRegistry.INSTANCE.getNamedSessionFactory(NAME));
    assertFalse(factory.getUuid().equals(factoryWithSameName.getUuid()));
    // Session factory resolved from typeFactory should be the new session factory
    // (because it is resolved from SessionFactoryRegistry.INSTANCE)
    assertSame(factoryWithSameName, typeFactory.resolveSessionFactory());
    factory.close();
    factoryWithSameName.close();
}
Also used : Configuration(org.hibernate.cfg.Configuration) HibernateException(org.hibernate.HibernateException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) TypeFactory(org.hibernate.type.TypeFactory) Test(org.junit.Test)

Example 2 with SessionFactoryImplementor

use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.

the class TypeFactorySerializationTest method testUnregisterSerializeRegisterDiffSessionFactoryNoName.

@Test
public void testUnregisterSerializeRegisterDiffSessionFactoryNoName() throws Exception {
    Configuration cfg = new Configuration();
    SessionFactoryImplementor factory = (SessionFactoryImplementor) cfg.buildSessionFactory();
    assertSame(factory, SessionFactoryRegistry.INSTANCE.getSessionFactory(factory.getUuid()));
    // Remove the session factory from the registry
    SessionFactoryRegistry.INSTANCE.removeSessionFactory(factory.getUuid(), null, false, null);
    assertNull(SessionFactoryRegistry.INSTANCE.getSessionFactory(factory.getUuid()));
    TypeFactory typeFactory = factory.getTypeResolver().getTypeFactory();
    byte[] typeFactoryBytes = SerializationHelper.serialize(typeFactory);
    typeFactory = (TypeFactory) SerializationHelper.deserialize(typeFactoryBytes);
    try {
        typeFactory.resolveSessionFactory();
        fail("should have failed with HibernateException because session factory is not registered.");
    } catch (HibernateException ex) {
    // expected because the session factory is not registered.
    }
    // Now create a new session factory with the same name; it will have a different UUID.
    SessionFactoryImplementor factoryWithDiffUuid = (SessionFactoryImplementor) cfg.buildSessionFactory();
    assertSame(factoryWithDiffUuid, SessionFactoryRegistry.INSTANCE.getSessionFactory(factoryWithDiffUuid.getUuid()));
    assertFalse(factory.getUuid().equals(factoryWithDiffUuid.getUuid()));
    // It should not be possible to resolve the session factory with no name configured.
    try {
        typeFactory.resolveSessionFactory();
        fail("should have failed with HibernateException because session factories were not registered with the same non-null name.");
    } catch (HibernateException ex) {
    // expected
    }
    factory.close();
    factoryWithDiffUuid.close();
}
Also used : Configuration(org.hibernate.cfg.Configuration) HibernateException(org.hibernate.HibernateException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) TypeFactory(org.hibernate.type.TypeFactory) Test(org.junit.Test)

Example 3 with SessionFactoryImplementor

use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.

the class CorrectnessTestCase method checkForEmptyPendingPuts.

protected void checkForEmptyPendingPuts() throws Exception {
    Field pp = PutFromLoadValidator.class.getDeclaredField("pendingPuts");
    pp.setAccessible(true);
    Method getInvalidators = null;
    List<DelayedInvalidators> delayed = new LinkedList<>();
    for (int i = 0; i < sessionFactories.length; i++) {
        SessionFactoryImplementor sfi = (SessionFactoryImplementor) sessionFactories[i];
        for (Object regionName : sfi.getCache().getSecondLevelCacheRegionNames()) {
            PutFromLoadValidator validator = getPutFromLoadValidator(sfi, (String) regionName);
            if (validator == null) {
                log.warn("No validator for " + regionName);
                continue;
            }
            ConcurrentMap<Object, Object> map = (ConcurrentMap) pp.get(validator);
            for (Iterator<Map.Entry<Object, Object>> iterator = map.entrySet().iterator(); iterator.hasNext(); ) {
                Map.Entry entry = iterator.next();
                if (getInvalidators == null) {
                    getInvalidators = entry.getValue().getClass().getMethod("getInvalidators");
                    getInvalidators.setAccessible(true);
                }
                java.util.Collection invalidators = (java.util.Collection) getInvalidators.invoke(entry.getValue());
                if (invalidators != null && !invalidators.isEmpty()) {
                    delayed.add(new DelayedInvalidators(map, entry.getKey()));
                }
            }
        }
    }
    // poll until all invalidations come
    long deadline = System.currentTimeMillis() + 30000;
    while (System.currentTimeMillis() < deadline) {
        iterateInvalidators(delayed, getInvalidators, (k, i) -> {
        });
        if (delayed.isEmpty()) {
            break;
        }
        Thread.sleep(1000);
    }
    if (!delayed.isEmpty()) {
        iterateInvalidators(delayed, getInvalidators, (k, i) -> log.warnf("Left invalidators on key %s: %s", k, i));
        throw new IllegalStateException("Invalidators were not cleared: " + delayed.size());
    }
}
Also used : PutFromLoadValidator(org.hibernate.cache.infinispan.access.PutFromLoadValidator) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ConcurrentMap(java.util.concurrent.ConcurrentMap) Method(java.lang.reflect.Method) LinkedList(java.util.LinkedList) Field(java.lang.reflect.Field) Collection(org.hibernate.mapping.Collection) Map(java.util.Map) TreeMap(java.util.TreeMap) NavigableMap(java.util.NavigableMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap)

Example 4 with SessionFactoryImplementor

use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.

the class CacheKeySerializationTest method getSessionFactory.

private SessionFactoryImplementor getSessionFactory(String cacheKeysFactory) {
    Configuration configuration = new Configuration().setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true").setProperty(Environment.CACHE_REGION_FACTORY, TestInfinispanRegionFactory.class.getName()).setProperty(Environment.DEFAULT_CACHE_CONCURRENCY_STRATEGY, "transactional").setProperty(AvailableSettings.SHARED_CACHE_MODE, "ALL").setProperty(Environment.HBM2DDL_AUTO, "create-drop");
    if (cacheKeysFactory != null) {
        configuration.setProperty(Environment.CACHE_KEYS_FACTORY, cacheKeysFactory);
    }
    configuration.addAnnotatedClass(WithSimpleId.class);
    configuration.addAnnotatedClass(WithEmbeddedId.class);
    return (SessionFactoryImplementor) configuration.buildSessionFactory();
}
Also used : Configuration(org.hibernate.cfg.Configuration) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor)

Example 5 with SessionFactoryImplementor

use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.

the class CacheKeySerializationTest method testId.

private void testId(CacheKeysFactory cacheKeysFactory, String entityName, Object id) throws Exception {
    final SessionFactoryImplementor sessionFactory = getSessionFactory(cacheKeysFactory.getClass().getName());
    final EntityPersister persister = sessionFactory.getEntityPersister(entityName);
    final Object key = cacheKeysFactory.createEntityKey(id, persister, sessionFactory, null);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(key);
    final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    final Object keyClone = ois.readObject();
    try {
        assertEquals(key, keyClone);
        assertEquals(keyClone, key);
        assertEquals(key.hashCode(), keyClone.hashCode());
        final Object idClone = cacheKeysFactory.getEntityId(keyClone);
        assertEquals(id.hashCode(), idClone.hashCode());
        assertEquals(id, idClone);
        assertEquals(idClone, id);
        assertTrue(persister.getIdentifierType().isEqual(id, idClone, sessionFactory));
        assertTrue(persister.getIdentifierType().isEqual(idClone, id, sessionFactory));
        sessionFactory.close();
    } finally {
        sessionFactory.close();
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ByteArrayInputStream(java.io.ByteArrayInputStream) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)201 Test (org.junit.Test)89 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)45 Configuration (org.hibernate.cfg.Configuration)44 MetadataSources (org.hibernate.boot.MetadataSources)37 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)27 ArrayList (java.util.ArrayList)23 RootClass (org.hibernate.mapping.RootClass)20 SimpleValue (org.hibernate.mapping.SimpleValue)20 ServiceRegistry (org.hibernate.service.ServiceRegistry)20 SQLException (java.sql.SQLException)18 EntityPersister (org.hibernate.persister.entity.EntityPersister)18 Metadata (org.hibernate.boot.Metadata)17 Type (org.hibernate.type.Type)16 Column (org.hibernate.mapping.Column)15 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)14 HibernateException (org.hibernate.HibernateException)13 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)13 EntityTuplizer (org.hibernate.tuple.entity.EntityTuplizer)13 Connection (java.sql.Connection)12