protected DataSource determineTargetDataSource()

in src/main/java/org/apache/fineract/cn/postgresql/domain/ContextAwareRoutingDataSource.java [63:98]


  protected DataSource determineTargetDataSource() {
    if (!TenantContextHolder.identifier().isPresent()) {
      this.logger.warn("Tenant context not available.");
      return super.determineTargetDataSource();
    }

    final String currentLookupKey = this.determineCurrentLookupKey().toString();

    this.dynamicDataSources.computeIfAbsent(currentLookupKey, (key) -> {
      this.logger.info("Creating new dynamic data source for {}.", key);
      final Tenant tenant = new Tenant(key);
      this.readAdditionalTenantInformation(tenant);
      final BoneCPDataSource tenantDataSource = new BoneCPDataSource();
      tenantDataSource.setDriverClass(tenant.getDriverClass());
      tenantDataSource.setJdbcUrl(JdbcUrlBuilder
          .create(this.type)
          .host(tenant.getHost())
          .port(tenant.getPort())
          .instanceName(tenant.getDatabaseName())
          .build());
      tenantDataSource.setUsername(tenant.getUser());
      tenantDataSource.setPassword(tenant.getPassword());

      final BoneCPDataSource boneCpMetaDataSource = (BoneCPDataSource) this.metaDataSource;
      tenantDataSource.setIdleConnectionTestPeriodInMinutes(boneCpMetaDataSource.getIdleConnectionTestPeriodInMinutes());
      tenantDataSource.setIdleMaxAgeInMinutes(boneCpMetaDataSource.getIdleMaxAgeInMinutes());
      tenantDataSource.setMaxConnectionsPerPartition(boneCpMetaDataSource.getMaxConnectionsPerPartition());
      tenantDataSource.setMinConnectionsPerPartition(boneCpMetaDataSource.getMinConnectionsPerPartition());
      tenantDataSource.setPartitionCount(boneCpMetaDataSource.getPartitionCount());
      tenantDataSource.setAcquireIncrement(boneCpMetaDataSource.getAcquireIncrement());
      tenantDataSource.setStatementsCacheSize(boneCpMetaDataSource.getStatementsCacheSize());
      return tenantDataSource;
    });

    return this.dynamicDataSources.get(currentLookupKey);
  }