1818 */
1919package org .apache .fineract .infrastructure .core .service .database ;
2020
21+ import com .google .common .collect .Sets ;
2122import java .sql .Connection ;
2223import java .sql .SQLException ;
2324import java .util .List ;
2425import java .util .Map ;
26+ import java .util .Set ;
2527import java .util .concurrent .ConcurrentHashMap ;
2628import javax .sql .DataSource ;
2729import lombok .RequiredArgsConstructor ;
2830import lombok .extern .slf4j .Slf4j ;
31+ import org .apache .fineract .infrastructure .configuration .service .MoneyHelperInitializationService ;
2932import org .apache .fineract .infrastructure .core .domain .FineractPlatformTenant ;
3033import org .apache .fineract .infrastructure .core .domain .FineractPlatformTenantConnection ;
3134import org .apache .fineract .infrastructure .core .service .ThreadLocalContextUtil ;
@@ -53,6 +56,9 @@ public class TomcatJdbcDataSourcePerTenantService implements RoutingDataSourceSe
5356
5457 private final DataSourcePerTenantServiceFactory dataSourcePerTenantServiceFactory ;
5558
59+ private final MoneyHelperInitializationService moneyHelperInitializationService ;
60+ private final Set <Long > tenantMoneyInitializingSet = Sets .newConcurrentHashSet ();
61+
5662 @ Override
5763 public DataSource retrieveDataSource () {
5864 // default to tenant database datasource
@@ -66,7 +72,23 @@ public DataSource retrieveDataSource() {
6672 // appropriate datasource for that tenant.
6773 actualDataSource = TENANT_TO_DATA_SOURCE_MAP .computeIfAbsent (tenantConnectionKey ,
6874 (key ) -> dataSourcePerTenantServiceFactory .createNewDataSourceFor (tenant , tenantConnection ));
75+ }
6976
77+ // TODO: This is definitely not the optimal place to initialize the rounding modes
78+ // Preferably nothing should use a statically referenced context and the initialization
79+ // should happen within the rounding mode retrieval
80+ if (tenant != null ) {
81+ Long connectionId = tenant .getConnection ().getConnectionId ();
82+ if (!tenantMoneyInitializingSet .contains (connectionId ) && !moneyHelperInitializationService .isTenantInitialized (tenant )) {
83+ // Double check to prevent visibility and race-condition issues
84+ synchronized (tenantMoneyInitializingSet ) {
85+ if (!tenantMoneyInitializingSet .contains (connectionId )) {
86+ tenantMoneyInitializingSet .add (connectionId );
87+ moneyHelperInitializationService .initializeTenantRoundingMode (tenant );
88+ tenantMoneyInitializingSet .remove (connectionId );
89+ }
90+ }
91+ }
7092 }
7193
7294 return actualDataSource ;
0 commit comments