This is an automated email from the ASF dual-hosted git repository. juhan pushed a commit to branch spring_boot_2 in repository https://gitbox.apache.org/repos/asf/fineract-cn-postgresql.git
commit 41bd3b24ba886a11005ee28bb28c147c7a1a50ac Author: Juhan Aasaru <[email protected]> AuthorDate: Sat Oct 12 15:52:41 2019 +0300 FINCN-180 restore tenant-specific data sources functionality --- README.md | 5 ----- .../config/EclipseLinkJpaConfiguration.java | 10 ++++++--- .../PostgreSQLTenantBasedJavaConfiguration.java | 12 +++++----- .../domain/ContextAwareRoutingDataSource.java | 4 ++-- .../cn/postgresql/util/EclipseLinkConstants.java | 26 ++++++++++++++++++++++ 5 files changed, 42 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 341a893..b8ccbac 100644 --- a/README.md +++ b/README.md @@ -11,11 +11,6 @@ Install Java 8 as described at https://docs.oracle.com/javase/8/docs/technotes/g Install PostgreSQL as described at https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-16-04. -After installation you need to create the meta database: - - sudo -i -u postgres psql - CREATE DATABASE system_console; - ## Multi-tenancy Multi-tenancy is reached by providing separate data storage on a per tenant basis. diff --git a/src/main/java/org/apache/fineract/cn/postgresql/config/EclipseLinkJpaConfiguration.java b/src/main/java/org/apache/fineract/cn/postgresql/config/EclipseLinkJpaConfiguration.java index 0ca5428..135d263 100644 --- a/src/main/java/org/apache/fineract/cn/postgresql/config/EclipseLinkJpaConfiguration.java +++ b/src/main/java/org/apache/fineract/cn/postgresql/config/EclipseLinkJpaConfiguration.java @@ -18,11 +18,13 @@ */ package org.apache.fineract.cn.postgresql.config; +import org.apache.fineract.cn.postgresql.util.EclipseLinkConstants; import org.eclipse.persistence.config.BatchWriting; import org.eclipse.persistence.config.PersistenceUnitProperties; import org.eclipse.persistence.config.TargetDatabase; import org.eclipse.persistence.logging.SessionLog; import org.springframework.beans.factory.ObjectProvider; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; @@ -53,8 +55,10 @@ import java.util.Map; "org.apache.fineract.cn.postgresql.util" }) public class EclipseLinkJpaConfiguration extends JpaBaseConfiguration { - - + + @Value(EclipseLinkConstants.ECLIPSE_LINK_SHOW_SQL + ":" + EclipseLinkConstants.ECLIPSE_LINK_SHOW_SQL_DEFAULT) + private Boolean eclipseLinkShowSql; + protected EclipseLinkJpaConfiguration(DataSource dataSource, JpaProperties properties, ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider) { super(dataSource, properties, jtaTransactionManagerProvider); } @@ -75,7 +79,7 @@ public class EclipseLinkJpaConfiguration extends JpaBaseConfiguration { protected AbstractJpaVendorAdapter createJpaVendorAdapter() { EclipseLinkJpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter(); vendorAdapter.setDatabasePlatform("org.eclipse.persistence.platform.database.PostgreSQLPlatform"); - vendorAdapter.setShowSql(true); // Todo: remove sql log + vendorAdapter.setShowSql(eclipseLinkShowSql); vendorAdapter.setDatabase(Database.POSTGRESQL); vendorAdapter.setGenerateDdl(false); return vendorAdapter; diff --git a/src/main/java/org/apache/fineract/cn/postgresql/config/PostgreSQLTenantBasedJavaConfiguration.java b/src/main/java/org/apache/fineract/cn/postgresql/config/PostgreSQLTenantBasedJavaConfiguration.java index a11cd35..2848b28 100644 --- a/src/main/java/org/apache/fineract/cn/postgresql/config/PostgreSQLTenantBasedJavaConfiguration.java +++ b/src/main/java/org/apache/fineract/cn/postgresql/config/PostgreSQLTenantBasedJavaConfiguration.java @@ -24,6 +24,7 @@ import org.apache.fineract.cn.postgresql.util.PostgreSQLConstants; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @@ -33,13 +34,14 @@ import java.util.HashMap; @Configuration @ConditionalOnProperty(prefix = "postgresql", name = "enabled", matchIfMissing = true) public class PostgreSQLTenantBasedJavaConfiguration { + @Bean public DataSource dataSource(@Qualifier(PostgreSQLConstants.LOGGER_NAME) final Logger logger, final MetaDataSourceWrapper metaDataSource) { - final ContextAwareRoutingDataSource dataSources = new ContextAwareRoutingDataSource(logger, JdbcUrlBuilder.DatabaseType.POSTGRESQL); - dataSources.setMetaDataSource(metaDataSource.getMetaDataSource()); - final HashMap<Object, Object> targetDataSources = new HashMap<>(); - dataSources.setTargetDataSources(targetDataSources); - return dataSources; + final ContextAwareRoutingDataSource dataSource = new ContextAwareRoutingDataSource(logger, JdbcUrlBuilder.DatabaseType.POSTGRESQL); + dataSource.setMetaDataSource(metaDataSource.getMetaDataSource()); + final HashMap<Object, Object> tenantDataSources = new HashMap<>(); + dataSource.setTargetDataSources(tenantDataSources); + return dataSource; } } diff --git a/src/main/java/org/apache/fineract/cn/postgresql/domain/ContextAwareRoutingDataSource.java b/src/main/java/org/apache/fineract/cn/postgresql/domain/ContextAwareRoutingDataSource.java index 72b310a..a14f98d 100644 --- a/src/main/java/org/apache/fineract/cn/postgresql/domain/ContextAwareRoutingDataSource.java +++ b/src/main/java/org/apache/fineract/cn/postgresql/domain/ContextAwareRoutingDataSource.java @@ -56,7 +56,7 @@ public final class ContextAwareRoutingDataSource extends AbstractRoutingDataSour @Override protected Object determineCurrentLookupKey() { - return TenantContextHolder.checkedGetIdentifier(); + return TenantContextHolder.identifier().orElse(null); } @Override @@ -118,4 +118,4 @@ public final class ContextAwareRoutingDataSource extends AbstractRoutingDataSour throw new IllegalArgumentException("Could not fetch information for tenant '" + tenant.getIdentifier() + "'", ex); } } -} \ No newline at end of file +} diff --git a/src/main/java/org/apache/fineract/cn/postgresql/util/EclipseLinkConstants.java b/src/main/java/org/apache/fineract/cn/postgresql/util/EclipseLinkConstants.java new file mode 100644 index 0000000..4719a70 --- /dev/null +++ b/src/main/java/org/apache/fineract/cn/postgresql/util/EclipseLinkConstants.java @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.fineract.cn.postgresql.util; + +public interface EclipseLinkConstants { + + String ECLIPSE_LINK_SHOW_SQL = "eclipseLink.showSql"; + + String ECLIPSE_LINK_SHOW_SQL_DEFAULT = "true"; +}
