@Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory( DataSource dataSource, MultiTenantConnectionProvider multiTenantConnectionProviderImpl, CurrentTenantIdentifierResolver currentTenantIdentifierResolverImpl) { Map<String, Object> properties = new HashMap<>(); properties.putAll(jpaProperties.getHibernateProperties(dataSource)); properties.put(org.hibernate.cfg.Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA); properties .put(org.hibernate.cfg.Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProviderImpl); properties .put(org.hibernate.cfg.Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolverImpl); LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource); em.setPackagesToScan(JPA_PACKAGES); em.setJpaVendorAdapter(jpaVendorAdapter()); em.setJpaPropertyMap(properties); return em; }
@Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory( DataSource dataSource, MultiTenantConnectionProvider multiTenantConnectionProviderImpl, CurrentTenantIdentifierResolver currentTenantIdentifierResolverImpl, LocalValidatorFactoryBean localValidatorFactoryBean) { Map<String, Object> properties = new HashMap<>(); properties.putAll(jpaProperties.getHibernateProperties(dataSource)); properties.put(MULTI_TENANT, MultiTenancyStrategy.SCHEMA); properties.put(MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProviderImpl); properties.put(MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolverImpl); properties.put(JPA_VALIDATION_FACTORY, localValidatorFactoryBean); LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource); em.setPackagesToScan(JPA_PACKAGES); em.setJpaVendorAdapter(jpaVendorAdapter()); em.setJpaPropertyMap(properties); return em; }
@Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, MultiTenantConnectionProvider multiTenantConnectionProviderImpl, CurrentTenantIdentifierResolver currentTenantIdentifierResolverImpl) { Map<String, Object> properties = new HashMap<>(); properties.putAll(jpaProperties.getHibernateProperties(dataSource)); properties.put(org.hibernate.cfg.Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA); properties .put(org.hibernate.cfg.Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProviderImpl); properties .put(org.hibernate.cfg.Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolverImpl); LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource); em.setPackagesToScan(JPA_PACKAGES); em.setJpaVendorAdapter(jpaVendorAdapter()); em.setJpaPropertyMap(properties); return em; }
@Override public JdbcConnectionAccess getJdbcConnectionAccess() { if ( jdbcConnectionAccess == null ) { if ( MultiTenancyStrategy.NONE == factory.getSettings().getMultiTenancyStrategy() ) { jdbcConnectionAccess = new NonContextualJdbcConnectionAccess( getEventListenerManager(), factory.getServiceRegistry().getService( ConnectionProvider.class ) ); } else { jdbcConnectionAccess = new ContextualJdbcConnectionAccess( getEventListenerManager(), factory.getServiceRegistry().getService( MultiTenantConnectionProvider.class ) ); } } return jdbcConnectionAccess; }
private JdbcConnectionAccess buildLocalConnectionAccess() { return new JdbcConnectionAccess() { @Override public Connection obtainConnection() throws SQLException { return settings.getMultiTenancyStrategy() == MultiTenancyStrategy.NONE ? serviceRegistry.getService( ConnectionProvider.class ).getConnection() : serviceRegistry.getService( MultiTenantConnectionProvider.class ).getAnyConnection(); } @Override public void releaseConnection(Connection connection) throws SQLException { if ( settings.getMultiTenancyStrategy() == MultiTenancyStrategy.NONE ) { serviceRegistry.getService( ConnectionProvider.class ).closeConnection( connection ); } else { serviceRegistry.getService( MultiTenantConnectionProvider.class ).releaseAnyConnection( connection ); } } @Override public boolean supportsAggressiveRelease() { return false; } }; }
@Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, MultiTenantConnectionProvider multiTenantConnectionProvider, CurrentTenantIdentifierResolver tenantIdentifierResolver) { final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource); em.setPackagesToScan("com.mycompany.models"); em.setJpaVendorAdapter(this.jpaVendorAdapter()); final Map<String, Object> jpaProperties = new HashMap<>(); jpaProperties.put(Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA); jpaProperties.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProvider); jpaProperties.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, tenantIdentifierResolver); jpaProperties.put(Environment.FORMAT_SQL, true); em.setJpaPropertyMap(jpaProperties); return em; }
@Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, MultiTenantConnectionProvider multiTenantConnectionProviderImpl, CurrentTenantIdentifierResolver currentTenantIdentifierResolverImpl) { Map<String, Object> properties = new HashMap<>(); properties.putAll(jpaProperties.getHibernateProperties(dataSource)); properties.put(Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA); properties.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProviderImpl); properties.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolverImpl); LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource); em.setPackagesToScan("com.srai"); em.setJpaVendorAdapter(jpaVendorAdapter()); em.setJpaPropertyMap(properties); return em; }
@Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, MultiTenantConnectionProvider multiTenantConnectionProviderImpl, CurrentTenantIdentifierResolver currentTenantIdentifierResolverImpl, LocalValidatorFactoryBean localValidatorFactoryBean) { Map<String, Object> properties = new HashMap<>(); properties.putAll(jpaProperties.getHibernateProperties(dataSource)); properties.put(org.hibernate.cfg.Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA); properties .put(org.hibernate.cfg.Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProviderImpl); properties .put(org.hibernate.cfg.Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolverImpl); properties.put(JPA_VALIDATION_FACTORY, localValidatorFactoryBean); LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource); em.setPackagesToScan(JPA_PACKAGES); em.setJpaVendorAdapter(jpaVendorAdapter()); em.setJpaPropertyMap(properties); return em; }
private JdbcConnectionAccess buildJdbcConnectionAccess(Map configValues) { final MultiTenancyStrategy multiTenancyStrategy = MultiTenancyStrategy.determineMultiTenancyStrategy( configValues ); if ( MultiTenancyStrategy.NONE == multiTenancyStrategy ) { connectionProvider = serviceRegistry.getService( ConnectionProvider.class ); return new ConnectionProviderJdbcConnectionAccess( connectionProvider ); } else { connectionProvider = null; final MultiTenantConnectionProvider multiTenantConnectionProvider = serviceRegistry.getService( MultiTenantConnectionProvider.class ); return new MultiTenantConnectionProviderJdbcConnectionAccess( multiTenantConnectionProvider ); } }
@Override public Class<MultiTenantConnectionProvider> getServiceInitiated() { return MultiTenantConnectionProvider.class; }
@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() + "]" ); } } }
public MultiTenantConnectionProviderJdbcConnectionAccess(MultiTenantConnectionProvider connectionProvider) { this.connectionProvider = connectionProvider; }
private ContextualJdbcConnectionAccess( SessionEventListener listener, MultiTenantConnectionProvider connectionProvider) { this.listener = listener; this.connectionProvider = connectionProvider; }
/** * Set a {@link MultiTenantConnectionProvider} to be passed on to the * SessionFactory. * * @see HibernateSpringSessionFactoryBuilder#setMultiTenantConnectionProvider * @since 4.3 */ public void setMultiTenantConnectionProvider(MultiTenantConnectionProvider multiTenantConnectionProvider) { this.multiTenantConnectionProvider = multiTenantConnectionProvider; }
/** * Set a {@link MultiTenantConnectionProvider} to be passed on to the SessionFactory. * * @see AvailableSettings#MULTI_TENANT_CONNECTION_PROVIDER * @since 4.3 */ public HibernateSpringSessionFactoryBuilder setMultiTenantConnectionProvider(MultiTenantConnectionProvider multiTenantConnectionProvider) { getProperties().put(AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProvider); return this; }