Skip to content

Commit

Permalink
Share tenant data sources across requests
Browse files Browse the repository at this point in the history
  • Loading branch information
smailliwcs committed Jan 29, 2024
1 parent 9496d2a commit 1b8793f
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions core/src/main/java/com/lantanagroup/link/db/TenantService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

public class TenantService {
private static final Logger logger = LoggerFactory.getLogger(TenantService.class);
private static final Map<String, ComboPooledDataSource> dataSourcesByJdbcUrl = new ConcurrentHashMap<>();

@Getter
private final Tenant config;
Expand All @@ -48,10 +47,8 @@ public class TenantService {
private final ValidationRepository validations;
private final ValidationCategoryRepository validationCategories;

protected TenantService(Tenant config) {
protected TenantService(Tenant config, ComboPooledDataSource dataSource) {
this.config = config;
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl(config.getConnectionString());
this.dataSource = dataSource;
PlatformTransactionManager txManager = new DataSourceTransactionManager(this.dataSource);
this.conceptMaps = new ConceptMapRepository(this.dataSource, txManager);
Expand All @@ -69,7 +66,11 @@ protected TenantService(Tenant config) {
}

public static TenantService create(Tenant tenant) {
return new TenantService(tenant);
return new TenantService(tenant, dataSourcesByJdbcUrl.computeIfAbsent(tenant.getConnectionString(), jdbcUrl -> {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl(jdbcUrl);
return dataSource;
}));
}

public void testConnection() throws SQLException {
Expand Down Expand Up @@ -98,7 +99,7 @@ public static TenantService create(SharedService sharedService, String tenantId)
return null;
}

return new TenantService(tenant);
return TenantService.create(tenant);
}

public List<PatientList> getPatientLists(String reportId) {
Expand Down

0 comments on commit 1b8793f

Please sign in to comment.