@PostConstruct
 public void init() {
   Collection<ICustomerContext> customers = customerContextHolder.getCustomerContexts();
   for (ICustomerContext customerContext : customers) {
     try {
       DataSource dataSource = configurableDataSourceFactory.createDataSource(customerContext);
       resolvedDataSources.put(customerContext.getCustomerId(), dataSource);
     } catch (Exception e) {
       logger.error(
           "Unable to create DataSource for customer [" + customerContext.getCustomerId() + "]",
           e);
     }
   }
 }
  protected DataSource resolveSpecifiedDataSource(ICustomerContext customerContext)
      throws IllegalArgumentException {
    try {
      DataSource dataSource = null;
      synchronized (customerContext) {
        dataSource = configurableDataSourceFactory.createDataSource(customerContext);
        this.resolvedDataSources.put(customerContext.getCustomerId(), dataSource);
      }
      return dataSource;

    } catch (Exception e) {
      String msg =
          "Unable to create DataSource for customer [" + customerContext.getCustomerId() + "]";
      logger.error(msg, e);
      throw new IllegalArgumentException(msg, e);
    }
  }
  protected DataSource determineTargetDataSource() {
    ICustomerContext customerContext = determineCurrentLookupKey();
    Long lookupKey = customerContext.getCustomerId();

    DataSource dataSource = this.resolvedDataSources.get(lookupKey);

    if (dataSource == null) {
      dataSource = resolveSpecifiedDataSource(customerContext);
    }

    if (dataSource == null) {
      String msg = "Cannot determine target DataSource for customer [" + lookupKey + "]";
      logger.error(msg);
      throw new IllegalStateException(msg);
    }
    return dataSource;
  }