Skip to content

Instantly share code, notes, and snippets.

@scottmarlow
Created September 20, 2018 16:04
Show Gist options
  • Save scottmarlow/63241549820243923aab16e664c3c6c3 to your computer and use it in GitHub Desktop.
Save scottmarlow/63241549820243923aab16e664c3c6c3 to your computer and use it in GitHub Desktop.
MultipleInitiatorTest
package org.hibernate.service;
import java.util.Map;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cache.internal.RegionFactoryInitiator;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.spi.ServiceContributor;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.testing.cache.CachingRegionFactory;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MultipleInitiatorTest extends BaseUnitTestCase {
@Test
public void testTwoSessionFactories() {
SessionFactory sf1 = null, sf2 = null;
try {
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();
final MyRegionFactoryInitiator initiator = new MyRegionFactoryInitiator();
ssrb.addInitiator( initiator );
ssrb.addService( SomeService.class, initiator );
final ServiceRegistryImplementor registry = (ServiceRegistryImplementor) ssrb.build();
Configuration config = new Configuration();
sf1 = config.buildSessionFactory( registry );
assertEquals( 1, initiator.calledCount );
sf2 = config.buildSessionFactory( registry );
assertEquals( 2, initiator.calledCount );
}
finally {
if ( sf1 != null ) {
sf1.close();
}
if ( sf2 != null ) {
sf2.close();
}
}
}
class MyRegionFactoryInitiator extends RegionFactoryInitiator implements ServiceContributor, Service {
private int calledCount = 0;
@Override
protected RegionFactory getFallback(
Map configurationValues,
ServiceRegistryImplementor registry) {
calledCount++;
return new MyRegionFactory();
}
@Override
public void contribute(StandardServiceRegistryBuilder serviceRegistryBuilder) {
calledCount++;
}
}
class MyRegionFactory extends CachingRegionFactory {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment