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();
}
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();
}
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());
}
}
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();
}
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();
}
}
Aggregations