Search in sources :

Example 1 with MultiTenantConnectionProvider

use of org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider in project hibernate-orm by hibernate.

the class SchemaBasedMultiTenancyTest method setUp.

@Before
public void setUp() {
    AbstractMultiTenantConnectionProvider multiTenantConnectionProvider = buildMultiTenantConnectionProvider();
    Map settings = new HashMap();
    settings.put(Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA);
    settings.put(Environment.CACHE_REGION_FACTORY, CachingRegionFactory.class.getName());
    settings.put(Environment.GENERATE_STATISTICS, "true");
    serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder().applySettings(settings).addService(MultiTenantConnectionProvider.class, multiTenantConnectionProvider).build();
    MetadataSources ms = new MetadataSources(serviceRegistry);
    ms.addAnnotatedClass(Customer.class);
    ms.addAnnotatedClass(Invoice.class);
    Metadata metadata = ms.buildMetadata();
    ((RootClass) metadata.getEntityBinding(Customer.class.getName())).setCacheConcurrencyStrategy("read-write");
    HibernateSchemaManagementTool tool = new HibernateSchemaManagementTool();
    tool.injectServices(serviceRegistry);
    final GenerationTargetToDatabase acmeTarget = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, acmeProvider));
    final GenerationTargetToDatabase jbossTarget = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, jbossProvider));
    new SchemaDropperImpl(serviceRegistry).doDrop(metadata, serviceRegistry, settings, true, acmeTarget, jbossTarget);
    new SchemaCreatorImpl(serviceRegistry).doCreation(metadata, serviceRegistry, settings, true, acmeTarget, jbossTarget);
    final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
    configure(sfb);
    sessionFactory = (SessionFactoryImplementor) sfb.build();
}
Also used : RootClass(org.hibernate.mapping.RootClass) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) HashMap(java.util.HashMap) CachingRegionFactory(org.hibernate.testing.cache.CachingRegionFactory) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) AbstractMultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.AbstractMultiTenantConnectionProvider) HibernateSchemaManagementTool(org.hibernate.tool.schema.internal.HibernateSchemaManagementTool) DdlTransactionIsolatorTestingImpl(org.hibernate.test.util.DdlTransactionIsolatorTestingImpl) SchemaDropperImpl(org.hibernate.tool.schema.internal.SchemaDropperImpl) SchemaCreatorImpl(org.hibernate.tool.schema.internal.SchemaCreatorImpl) SessionFactoryBuilder(org.hibernate.boot.SessionFactoryBuilder) AbstractMultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.AbstractMultiTenantConnectionProvider) MultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider) GenerationTargetToDatabase(org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

Example 2 with MultiTenantConnectionProvider

use of org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider in project hibernate-orm by hibernate.

the class MultiTenantConnectionProviderInitiator method initiateService.

@Override
@SuppressWarnings({ "unchecked" })
public MultiTenantConnectionProvider initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
    final MultiTenancyStrategy strategy = MultiTenancyStrategy.determineMultiTenancyStrategy(configurationValues);
    if (strategy == MultiTenancyStrategy.NONE || strategy == MultiTenancyStrategy.DISCRIMINATOR) {
        // nothing to do, but given the separate hierarchies have to handle this here.
        return null;
    }
    final Object configValue = configurationValues.get(AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER);
    if (configValue == null) {
        // if they also specified the data source *name*, then lets assume they want
        // DataSourceBasedMultiTenantConnectionProviderImpl
        final Object dataSourceConfigValue = configurationValues.get(AvailableSettings.DATASOURCE);
        if (dataSourceConfigValue != null && String.class.isInstance(dataSourceConfigValue)) {
            return new DataSourceBasedMultiTenantConnectionProviderImpl();
        }
        return null;
    }
    if (MultiTenantConnectionProvider.class.isInstance(configValue)) {
        return (MultiTenantConnectionProvider) configValue;
    } else {
        final Class<MultiTenantConnectionProvider> implClass;
        if (Class.class.isInstance(configValue)) {
            implClass = (Class) configValue;
        } else {
            final String className = configValue.toString();
            final ClassLoaderService classLoaderService = registry.getService(ClassLoaderService.class);
            try {
                implClass = classLoaderService.classForName(className);
            } catch (ClassLoadingException cle) {
                log.warn("Unable to locate specified class [" + className + "]", cle);
                throw new ServiceException("Unable to locate specified multi-tenant connection provider [" + className + "]");
            }
        }
        try {
            return implClass.newInstance();
        } catch (Exception e) {
            log.warn("Unable to instantiate specified class [" + implClass.getName() + "]", e);
            throw new ServiceException("Unable to instantiate specified multi-tenant connection provider [" + implClass.getName() + "]");
        }
    }
}
Also used : MultiTenancyStrategy(org.hibernate.MultiTenancyStrategy) ServiceException(org.hibernate.service.spi.ServiceException) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) MultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider) DataSourceBasedMultiTenantConnectionProviderImpl(org.hibernate.engine.jdbc.connections.spi.DataSourceBasedMultiTenantConnectionProviderImpl) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) ServiceException(org.hibernate.service.spi.ServiceException) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Aggregations

MultiTenantConnectionProvider (org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MultiTenancyStrategy (org.hibernate.MultiTenancyStrategy)1 Metadata (org.hibernate.boot.Metadata)1 MetadataSources (org.hibernate.boot.MetadataSources)1 SessionFactoryBuilder (org.hibernate.boot.SessionFactoryBuilder)1 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)1 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)1 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)1 AbstractMultiTenantConnectionProvider (org.hibernate.engine.jdbc.connections.spi.AbstractMultiTenantConnectionProvider)1 DataSourceBasedMultiTenantConnectionProviderImpl (org.hibernate.engine.jdbc.connections.spi.DataSourceBasedMultiTenantConnectionProviderImpl)1 RootClass (org.hibernate.mapping.RootClass)1 ServiceException (org.hibernate.service.spi.ServiceException)1 DdlTransactionIsolatorTestingImpl (org.hibernate.test.util.DdlTransactionIsolatorTestingImpl)1 CachingRegionFactory (org.hibernate.testing.cache.CachingRegionFactory)1 HibernateSchemaManagementTool (org.hibernate.tool.schema.internal.HibernateSchemaManagementTool)1 SchemaCreatorImpl (org.hibernate.tool.schema.internal.SchemaCreatorImpl)1 SchemaDropperImpl (org.hibernate.tool.schema.internal.SchemaDropperImpl)1 GenerationTargetToDatabase (org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase)1