Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright 2018 onwards - Sunit Katkar (sunitkatkar@gmail.com)
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.sunitkatkar.blogspot.tenant.config;
- import java.util.HashMap;
- import java.util.Map;
- import javax.persistence.EntityManagerFactory;
- import org.hibernate.MultiTenancyStrategy;
- import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
- import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
- import org.springframework.orm.jpa.JpaTransactionManager;
- import org.springframework.orm.jpa.JpaVendorAdapter;
- import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
- import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
- import com.sunitkatkar.blogspot.tenant.model.User;
- import com.sunitkatkar.blogspot.tenant.repository.UserRepository;
- import com.sunitkatkar.blogspot.tenant.service.UserService;
- /**
- * This is the tenant data sources configuration which sets up the multitenancy.
- *
- * @author Sunit Katkar, sunitkatkar@gmail.com
- * (https://sunitkatkar.blogspot.com/)
- * @since ver 1.0 (May 2018)
- * @version 1.0
- */
- @Configuration
- @EnableTransactionManagement
- @ComponentScan(basePackages = { "com.sunitkatkar.blogspot.tenant.repository",
- "com.sunitkatkar.blogspot.tenant.model" })
- @EnableJpaRepositories(basePackages = {
- "com.sunitkatkar.blogspot.tenant.repository",
- "com.sunitkatkar.blogspot.tenant.service" },
- entityManagerFactoryRef = "tenantEntityManagerFactory",
- transactionManagerRef = "tenantTransactionManager")
- public class TenantDatabaseConfig {
- private static final Logger LOG = LoggerFactory
- .getLogger(TenantDatabaseConfig.class);
- @Bean(name = "tenantJpaVendorAdapter")
- public JpaVendorAdapter jpaVendorAdapter() {
- return new HibernateJpaVendorAdapter();
- }
- @Bean(name = "tenantTransactionManager")
- public JpaTransactionManager transactionManager(
- EntityManagerFactory tenantEntityManager) {
- JpaTransactionManager transactionManager = new JpaTransactionManager();
- transactionManager.setEntityManagerFactory(tenantEntityManager);
- return transactionManager;
- }
- /**
- * The multi tenant connection provider
- *
- * @return
- */
- @Bean(name = "datasourceBasedMultitenantConnectionProvider")
- @ConditionalOnBean(name = "masterEntityManagerFactory")
- public MultiTenantConnectionProvider multiTenantConnectionProvider() {
- // Autowires the multi connection provider
- return new DataSourceBasedMultiTenantConnectionProviderImpl();
- }
- /**
- * The current tenant identifier resolver
- *
- * @return
- */
- @Bean(name = "currentTenantIdentifierResolver")
- public CurrentTenantIdentifierResolver currentTenantIdentifierResolver() {
- return new CurrentTenantIdentifierResolverImpl();
- }
- /**
- * Creates the entity manager factory bean which is required to access the
- * JPA functionalities provided by the JPA persistence provider, i.e.
- * Hibernate in this case.
- *
- * @param connectionProvider
- * @param tenantResolver
- * @return
- */
- @Bean(name = "tenantEntityManagerFactory")
- @ConditionalOnBean(name = "datasourceBasedMultitenantConnectionProvider")
- public LocalContainerEntityManagerFactoryBean entityManagerFactory(
- @Qualifier("datasourceBasedMultitenantConnectionProvider")
- MultiTenantConnectionProvider connectionProvider,
- @Qualifier("currentTenantIdentifierResolver")
- CurrentTenantIdentifierResolver tenantResolver) {
- LocalContainerEntityManagerFactoryBean emfBean = new LocalContainerEntityManagerFactoryBean();
- //All tenant related entities, repositories and service classes must be scanned
- emfBean.setPackagesToScan(
- new String[] { User.class.getPackage().getName(),
- UserRepository.class.getPackage().getName(),
- UserService.class.getPackage().getName() });
- emfBean.setJpaVendorAdapter(jpaVendorAdapter());
- emfBean.setPersistenceUnitName("tenantdb-persistence-unit");
- Map<String, Object> properties = new HashMap<>();
- properties.put(org.hibernate.cfg.Environment.MULTI_TENANT,
- MultiTenancyStrategy.SCHEMA);
- properties.put(
- org.hibernate.cfg.Environment.MULTI_TENANT_CONNECTION_PROVIDER,
- connectionProvider);
- properties.put(
- org.hibernate.cfg.Environment.MULTI_TENANT_IDENTIFIER_RESOLVER,
- tenantResolver);
- // ImprovedNamingStrategy is deprecated and unsupported in Hibernate 5
- // properties.put("hibernate.ejb.naming_strategy",
- // "org.hibernate.cfg.ImprovedNamingStrategy");
- properties.put(org.hibernate.cfg.Environment.DIALECT,
- "org.hibernate.dialect.MySQL5Dialect");
- properties.put(org.hibernate.cfg.Environment.SHOW_SQL, true);
- properties.put(org.hibernate.cfg.Environment.FORMAT_SQL, true);
- properties.put(org.hibernate.cfg.Environment.HBM2DDL_AUTO, "update");
- emfBean.setJpaPropertyMap(properties);
- LOG.info("tenantEntityManagerFactory set up successfully!");
- return emfBean;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement