Skip to content

Commit 556b2a5

Browse files
feat: Multi tenant user pool (#57)
* changes storage layer to take json instead of config file path * adds new functions skeleton * adds checks for conflicting configs for user pools * changes to tests to make them pass * adds skeleton for multi tenancy functions * fixes bug * adds connection pool ID function * changes as per interface change * adds one test for multi tenany storage layer * adds more tests * fixes bugs * adds more tests and changes config parsing to prioritise connection uri input * fixes a few config parsing bugs * adds more tests * modifies testing to clear multiple user pools after each test * makes initlogging idempotent * fixes all tests * fixes tests * adds more placeholder functions * removes use of quiteprogramexception * small change * adds new function skeleton * adds more skeleton functions * updates exception import * adds skeleton for tenantIdentifier for emailpassword and useridmapping recipes * changes to incorporate tenantIndetifier for key value storage * changes to session receipe to add tenantIdentifier * introduces the concept of appIdentifier vs tenantIdentifier * fixes test compilation issues * changes as per plugin change * modifes user roles functions to add tenantidentifier and appidentifiers * modifies emailpassword functions * changes to a few functions * adds appidentifier to email verfication * makes tests pass * adds tenant identifier to third party * adds tenantidentifier to passwordless * function name changes * fix: Multitenancy schema updates (#59) * fix: few schema changes and multitenancy impl * fix: handling pkey constraint * fix: pr comments * fix: pr comments * fix: pr comments * fix: pr comments * fix: typo and logical mistakes * fix: null handling and new exceptions * fix: refactored provider SQLs * fix: refactored select all * fix: fix for concurrent test * fix: cleanup * fix: cleanup and handle null boolean * fix: Multitenant emailpassword recipe changes (#60) * fix: emailpassword schema * fix: ep, ev and pless schema * fix: prepare for ep review * fix: app_id_to_user_id table * fix: ep recipe impl * fix: removed todo * fix: updated as per plugin interface * fix: fixed index * fix: pr comments * fix: removed backward compatibility * fix: minor fix (#62) * fix: Multitenant schema changes (#64) * fix: ev and pless impl * fix: ev fixes * fix: pless and tp changes * fix: revert delete user * fix: pless impl * fix: cleanup and fixed deleteUser * fix: simplified queries and added fkey checks in ep * fix: fkey checks for pless * fix: fkey checks for thirdparty * fix: fkey checks for emailverification * fix: fixed test * fix: updated to join query for ep * fix: updated join queries * fix: constraints * fix: test fix * fix: pr comments * fix: to support PR comments on core (#65) * fix: from core pr comments * fix: updated tenant identifier conversion * fix: Multitenant userroles (#69) * fix: user roles impl * fix: handling fkey * fix: transaction fix * fix: transaction fix * fix: Multitenant usermetadata (#70) * fix: user roles impl * fix: handling fkey * fix: usermetadata impl * fix: transaction fix * fix: transaction fix * fix: ep storage (#71) * fix: thirdparty storage (#74) * fix: Multitenant thirdparty changes for update email (#75) * fix: thirdparty storage * fix: thirdparty changes * fix: pr comments * fix: Multitenant emailverification storage (#76) * fix: thirdparty storage * fix: emailverification storage * fix: tokens tenant specific (#77) * fix: Multitenant session (#78) * fix: session changes * fix: session changes * fix: session changes * comment modification * fix: Multitenant session changes (#80) * fix: key value changes * fix: pr comments * fix: adding tenant or app not found exceptions * Multi tenant merging with latest (#79) * merges with latest * fixes test compilation issue * increases threshold of deadlock retries * adds simple test for loading 50 storages * many fixes * fix: jwt changes (#82) * fix: Multitenant General Queries (#84) * fix: updated general queries * fix: fixed queries * fix: Multitenant dashboard (#85) * fix: updated general queries * fix: fixed queries * fix: dashboard queries * fix: added fk contstraint * fix: fixed index * fix: Multitenant totp (#86) * fix: totp queries * fix: handling fk * fix: pr comment * merges (#87) * adds new config * fix: multitenancy changes (#88) * fix: multitenancy changes * fix: multitenant queries * fix: add userid to tenant * fix: saas test * fix: remove DeletionInProgressException * fix: pr comments * fix: recipe id in appid_to_userid table * fix: pr comment * fix: query fixes * fix: fixed validation * fix: added comments * fix: Misc changes (#89) * fix: session expiry index * fix: active users * fix: Tenantid in userobjects (#90) * fix: adding tenant ids to user objects * fix: create user type * fix: test fixes * fix: transaction * fix: refactored ep and tp * fix: refactor pless * fix: pr comment * fix: pr comment * fix: test fix (#92) * fix: Startup log (#93) * fix: removed log to console * fix: tenant id in loadConfig * fix: Userpool test (#94) * fix: userpool test * fix: added test with server restart * fix: delete non auth user (#95) * fix: Delete nonauth user (#96) * fix: nonAuthRecipeuserData to take tenantIdentifier * fix: pr comments * fix: config validation (#97) * fix: config per tenant, per app annotations (#98) * fix: config annotation (#102) * fix: config annotation * fix: removed comments * Update src/main/java/io/supertokens/storage/postgresql/config/PostgreSQLConfig.java * Update src/main/java/io/supertokens/storage/postgresql/config/PostgreSQLConfig.java --------- Co-authored-by: Rishabh Poddar <[email protected]> * fix: added setLogLevels (#103) * fix: merge issue * fix: fkey names (#104) * fix: fixed fkey names on user tables * fix: catching fkey constraints * fix: added comments * fix: Postgres migration (#105) * fix: fixed fkey names on user tables * fix: catching fkey constraints * fix: added comments * fix: changelog * fix: changelog * fix: pr comment * fix: Fkey indexes (#109) * fix: fkey indexes * fix: fixes * fix: active users storage stuff * fix: active users storage stuff * fix: fixed index name * fix: updated migration script * fix: update version * fix: added date --------- Co-authored-by: Sattvik Chakravarthy <[email protected]> Co-authored-by: Sattvik Chakravarthy <[email protected]>
1 parent b6b1b55 commit 556b2a5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+7237
-1699
lines changed

CHANGELOG.md

+775
Large diffs are not rendered by default.

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ plugins {
22
id 'java-library'
33
}
44

5-
version = "3.0.0"
5+
version = "4.0.0"
66

77
repositories {
88
mavenCentral()

config.yaml

+28-25
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,69 @@
11
postgresql_config_version: 0
2-
# (OPTIONAL | Default: 10) integer value. Defines the connection pool size to PostgreSQL.
2+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: 10) integer value. Defines the connection pool size to PostgreSQL.
33
# Please see https://github.com/brettwooldridge/HikariCP/wiki/About-Pool-Sizing
44
# postgresql_connection_pool_size:
55

6-
# (OPTIONAL | Default: null) string value. Specify the PostgreSQL connection URI in the following
7-
# format: postgresql://[user[:[password]]@]host[:port][/dbname][?attr1=val1&attr2=val2...
6+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: null) string value. Specify the PostgreSQL connection URI in the
7+
# following format: postgresql://[user[:[password]]@]host[:port][/dbname][?attr1=val1&attr2=val2...
88
# Values provided via other configs will override values provided by this config.
99
# postgresql_connection_uri:
1010

11-
# (OPTIONAL | Default: "localhost") string value. Specify the postgresql host url here. For example:
11+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "localhost") string value. Specify the postgresql host url here.
12+
# For example:
1213
# - "localhost"
1314
# - "192.168.0.1"
1415
# - "<IP to cloud instance>"
1516
# - "example.com"
1617
# postgresql_host:
1718

18-
# (OPTIONAL | Default: 5432) integer value. Specify the port to use when connecting to PostgreSQL instance.
19+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: 5432) integer value. Specify the port to use when connecting to
20+
# PostgreSQL instance.
1921
# postgresql_port:
2022

21-
# (COMPULSORY) string value. The PostgreSQL user to use to query the database.
23+
# (DIFFERENT_ACROSS_TENANTS | COMPULSORY) string value. The PostgreSQL user to use to query the database.
2224
# If the relevant tables are not already created by you, this user should have the
2325
# ability to create new tables. To see the tables needed, visit: https://supertokens.io/docs/community/getting-started/database-setup/postgresql
2426
# postgresql_user:
2527

26-
# (COMPULSORY) string value. Password for the PostgreSQL user. If you have not set a password
28+
# (DIFFERENT_ACROSS_TENANTS | COMPULSORY) string value. Password for the PostgreSQL user. If you have not set a password
2729
# make this an empty string.
2830
# postgresql_password:
2931

30-
# (OPTIONAL | Default: "supertokens") string value. The database name to store SuperTokens related data.
32+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "supertokens") string value. The database name to store SuperTokens
33+
# related data.
3134
# postgresql_database_name:
3235

33-
# (OPTIONAL | Default: "public") string value. The schema for tables.
36+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "public") string value. The schema for tables.
3437
# postgresql_table_schema:
3538

36-
# (OPTIONAL | Default: "") string value. A prefix to add to all table names managed by SuperTokens. An "_" will be
37-
# added between this prefix and the actual table name if the prefix is defined
39+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "") string value. A prefix to add to all table names managed by
40+
# SuperTokens. An "_" will be added between this prefix and the actual table name if the prefix is defined
3841
# postgresql_table_names_prefix:
3942

40-
# (OPTIONAL | Default: "key_value") string value. Specify the name of the table that will store secret keys
41-
# and app info necessary for the functioning sessions.
43+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "key_value") string value. Specify the name of the table that will
44+
# store secret keys and app info necessary for the functioning sessions.
4245
# postgresql_key_value_table_name:
4346

44-
# (OPTIONAL | Default: "session_info") string value. Specify the name of the table that will store the
45-
# session info for users.
47+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "session_info") string value. Specify the name of the table that
48+
# will store the session info for users.
4649
# postgresql_session_info_table_name:
4750

48-
# (OPTIONAL | Default: "emailpassword_users") string value. Specify the name of the table that will store the
49-
# user information, along with their email and hashed password.
51+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "emailpassword_users") string value. Specify the name of the table
52+
# that will store the user information, along with their email and hashed password.
5053
# postgresql_emailpassword_users_table_name:
5154

52-
# (OPTIONAL | Default: "emailpassword_pswd_reset_tokens") string value. Specify the name of the table that will
53-
# store the password reset tokens for users.
55+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "emailpassword_pswd_reset_tokens") string value. Specify the name
56+
# of the table that will store the password reset tokens for users.
5457
# postgresql_emailpassword_pswd_reset_tokens_table_name:
5558

56-
# (OPTIONAL | Default: "emailverification_tokens") string value. Specify the name of the table that will
57-
# store the email verification tokens for users.
59+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "emailverification_tokens") string value. Specify the name of the
60+
# table that will store the email verification tokens for users.
5861
# postgresql_emailverification_tokens_table_name:
5962

60-
# (OPTIONAL | Default: "emailverification_verified_emails") string value. Specify the name of the table that will
61-
# store the verified email addresses.
63+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "emailverification_verified_emails") string value. Specify the name
64+
# of the table that will store the verified email addresses.
6265
# postgresql_emailverification_verified_emails_table_name:
6366

64-
# (OPTIONAL | Default: "thirdparty_users") string value. Specify the name of the table that will
65-
# store the thirdparty recipe users.
67+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "thirdparty_users") string value. Specify the name of the table
68+
# that will store the thirdparty recipe users.
6669
# postgresql_thirdparty_users_table_name

devConfig.yaml

+30-25
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,71 @@
11
postgresql_config_version: 0
22

3-
# (OPTIONAL | Default: 10) integer value. Defines the connection pool size to PostgreSQL.
3+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: 10) integer value. Defines the connection pool size to PostgreSQL.
44
# Please see https://github.com/brettwooldridge/HikariCP/wiki/About-Pool-Sizing
55
# postgresql_connection_pool_size:
66

7-
# (OPTIONAL | Default: null) string value. Specify the PostgreSQL connection URI in the following
8-
# format: postgresql://[user[:[password]]@]host[:port][/dbname][?attr1=val1&attr2=val2...
7+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: null) string value. Specify the PostgreSQL connection URI in the
8+
# following format: postgresql://[user[:[password]]@]host[:port][/dbname][?attr1=val1&attr2=val2...
99
# Values provided via other configs will override values provided by this config.
1010
# postgresql_connection_uri:
1111

12-
# (OPTIONAL | Default: "localhost") string value. Specify the postgresql host url here. For example:
12+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "localhost") string value. Specify the postgresql host url here.
13+
# For example:
1314
# - "localhost"
1415
# - "192.168.0.1"
1516
# - "<IP to cloud instance>"
1617
# - "example.com"
1718
# postgresql_host:
1819

19-
# (OPTIONAL | Default: 5432) integer value. Specify the port to use when connecting to PostgreSQL instance.
20+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: 5432) integer value. Specify the port to use when connecting to
21+
# PostgreSQL instance.
2022
# postgresql_port:
2123

22-
# (COMPULSORY) string value. The PostgreSQL user to use to query the database.
24+
# (DIFFERENT_ACROSS_TENANTS | COMPULSORY) string value. The PostgreSQL user to use to query the database.
2325
# If the relevant tables are not already created by you, this user should have the
2426
# ability to create new tables. To see the tables needed, visit: TODO
2527
postgresql_user: "root"
2628

27-
# (COMPULSORY) string value. Password for the PostgreSQL instance. If you do not have a password
29+
# (DIFFERENT_ACROSS_TENANTS | COMPULSORY) string value. Password for the PostgreSQL instance. If you do not have a
30+
# password
2831
# make this an empty string.
2932
postgresql_password: "root"
30-
# (OPTIONAL | Default: "supertokens") string value. The database name to store SuperTokens related data.
33+
34+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "supertokens") string value. The database name to store SuperTokens
35+
# related data.
3136
# postgresql_database_name:
3237

33-
# (OPTIONAL | Default: "public") string value. The schema for tables.
38+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "public") string value. The schema for tables.
3439
# postgresql_table_schema:
3540

36-
# (OPTIONAL | Default: "") string value. A prefix to add to all table names managed by SuperTokens. An "_" will be
37-
# added between this prefix and the actual table name if the prefix is defined
41+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "") string value. A prefix to add to all table names managed by
42+
# SuperTokens. An "_" will be added between this prefix and the actual table name if the prefix is defined
3843
# postgresql_table_names_prefix:
3944

40-
# (OPTIONAL | Default: "key_value") string value. Specify the name of the table that will store secret keys
41-
# and app info necessary for the functioning sessions.
45+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "key_value") string value. Specify the name of the table that will
46+
# store secret keys and app info necessary for the functioning sessions.
4247
# postgresql_key_value_table_name:
4348

44-
# (OPTIONAL | Default: "session_info") string value. Specify the name of the table that will store the
45-
# session info for users.
49+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "session_info") string value. Specify the name of the table that
50+
# will store the session info for users.
4651
# postgresql_session_info_table_name:
4752

48-
# (OPTIONAL | Default: "emailpassword_users") string value. Specify the name of the table that will store the
49-
# user information, along with their email and hashed password.
53+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "emailpassword_users") string value. Specify the name of the table
54+
# that will store the user information, along with their email and hashed password.
5055
# postgresql_emailpassword_users_table_name:
5156

52-
# (OPTIONAL | Default: "emailpassword_pswd_reset_tokens") string value. Specify the name of the table that will
53-
# store the password reset tokens for users.
57+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "emailpassword_pswd_reset_tokens") string value. Specify the name
58+
# of the table that will store the password reset tokens for users.
5459
# postgresql_emailpassword_pswd_reset_tokens_table_name:
5560

56-
# (OPTIONAL | Default: "emailverification_tokens") string value. Specify the name of the table that will
57-
# store the email verification tokens for users.
61+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "emailverification_tokens") string value. Specify the name of the
62+
# table that will store the email verification tokens for users.
5863
# postgresql_emailverification_tokens_table_name:
5964

60-
# (OPTIONAL | Default: "emailverification_verified_emails") string value. Specify the name of the table that will
61-
# store the verified email addresses.
65+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "emailverification_verified_emails") string value. Specify the name
66+
# of the table that will store the verified email addresses.
6267
# postgresql_emailverification_verified_emails_table_name:
6368

64-
# (OPTIONAL | Default: "thirdparty_users") string value. Specify the name of the table that will
65-
# store the thirdparty recipe users.
69+
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: "thirdparty_users") string value. Specify the name of the table
70+
# that will store the thirdparty recipe users.
6671
# postgresql_thirdparty_users_table_name

jar/postgresql-plugin-3.0.0.jar

-131 KB
Binary file not shown.

jar/postgresql-plugin-4.0.0.jar

182 KB
Binary file not shown.

pluginInterfaceSupported.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"_comment": "contains a list of plugin interfaces branch names that this core supports",
33
"versions": [
4-
"2.23"
4+
"3.0"
55
]
66
}

src/main/java/io/supertokens/storage/postgresql/ConnectionPool.java

+50-29
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import com.zaxxer.hikari.HikariConfig;
2121
import com.zaxxer.hikari.HikariDataSource;
22-
import io.supertokens.pluginInterface.exceptions.QuitProgramFromPluginException;
22+
import io.supertokens.pluginInterface.exceptions.DbInitException;
2323
import io.supertokens.storage.postgresql.config.Config;
2424
import io.supertokens.storage.postgresql.config.PostgreSQLConfig;
2525
import io.supertokens.storage.postgresql.output.Logging;
@@ -33,23 +33,20 @@
3333
public class ConnectionPool extends ResourceDistributor.SingletonResource {
3434

3535
private static final String RESOURCE_KEY = "io.supertokens.storage.postgresql.ConnectionPool";
36-
private static HikariDataSource hikariDataSource = null;
36+
private HikariDataSource hikariDataSource;
37+
private final Start start;
3738

3839
private ConnectionPool(Start start) {
39-
if (!start.enabled) {
40-
throw new RuntimeException("Connection to refused"); // emulates exception thrown by Hikari
41-
}
42-
43-
if (ConnectionPool.hikariDataSource != null) {
44-
// This implies that it was already created before and that
45-
// there is no need to create Hikari again.
40+
this.start = start;
41+
}
4642

47-
// If ConnectionPool.hikariDataSource == null, it implies that
48-
// either the config file had changed somehow (which means the plugin JAR was reloaded, resulting in static
49-
// variables to be set to null), or it means that this is the first time we are trying to connect to a db
50-
// (applicable only for testing).
43+
private synchronized void initialiseHikariDataSource() throws SQLException {
44+
if (this.hikariDataSource != null) {
5145
return;
5246
}
47+
if (!start.enabled) {
48+
throw new RuntimeException("Connection to refused"); // emulates exception thrown by Hikari
49+
}
5350

5451
HikariConfig config = new HikariConfig();
5552
PostgreSQLConfig userConfig = Config.getConfig(start);
@@ -92,8 +89,12 @@ private ConnectionPool(Start start) {
9289
// SuperTokens
9390
// - Failed to validate connection org.mariadb.jdbc.MariaDbConnection@79af83ae (Connection.setNetworkTimeout
9491
// cannot be called on a closed connection). Possibly consider using a shorter maxLifetime value.
95-
config.setPoolName("SuperTokens");
96-
hikariDataSource = new HikariDataSource(config);
92+
config.setPoolName(start.getUserPoolId() + "~" + start.getConnectionPoolId());
93+
try {
94+
hikariDataSource = new HikariDataSource(config);
95+
} catch (Exception e) {
96+
throw new SQLException(e);
97+
}
9798
}
9899

99100
private static int getTimeToWaitToInit(Start start) {
@@ -120,30 +121,41 @@ private static ConnectionPool getInstance(Start start) {
120121
return (ConnectionPool) start.getResourceDistributor().getResource(RESOURCE_KEY);
121122
}
122123

123-
static void initPool(Start start) {
124-
if (getInstance(start) != null) {
124+
static boolean isAlreadyInitialised(Start start) {
125+
return getInstance(start) != null && getInstance(start).hikariDataSource != null;
126+
}
127+
128+
static void initPool(Start start, boolean shouldWait) throws DbInitException {
129+
if (isAlreadyInitialised(start)) {
125130
return;
126131
}
127132
if (Thread.currentThread() != start.mainThread) {
128-
throw new QuitProgramFromPluginException("Should not come here");
133+
throw new DbInitException("Should not come here");
129134
}
130135
Logging.info(start, "Setting up PostgreSQL connection pool.", true);
131136
boolean longMessagePrinted = false;
132137
long maxTryTime = System.currentTimeMillis() + getTimeToWaitToInit(start);
133-
String errorMessage = "Error connecting to PostgreSQL instance. Please make sure that PostgreSQL is running and that "
134-
+ "you have" + " specified the correct values for ('postgresql_host' and 'postgresql_port') or for "
135-
+ "'postgresql_connection_uri'";
138+
String errorMessage =
139+
"Error connecting to PostgreSQL instance. Please make sure that PostgreSQL is running and that "
140+
+ "you have" +
141+
" specified the correct values for ('postgresql_host' and 'postgresql_port') or for "
142+
+ "'postgresql_connection_uri'";
136143
try {
144+
ConnectionPool con = new ConnectionPool(start);
145+
start.getResourceDistributor().setResource(RESOURCE_KEY, con);
137146
while (true) {
138147
try {
139-
start.getResourceDistributor().setResource(RESOURCE_KEY, new ConnectionPool(start));
148+
con.initialiseHikariDataSource();
140149
break;
141150
} catch (Exception e) {
151+
if (!shouldWait) {
152+
throw new DbInitException(e);
153+
}
142154
if (e.getMessage().contains("Connection to") && e.getMessage().contains("refused")
143155
|| e.getMessage().contains("the database system is starting up")) {
144156
start.handleKillSignalForWhenItHappens();
145157
if (System.currentTimeMillis() > maxTryTime) {
146-
throw new QuitProgramFromPluginException(errorMessage);
158+
throw new DbInitException(errorMessage);
147159
}
148160
if (!longMessagePrinted) {
149161
longMessagePrinted = true;
@@ -160,10 +172,10 @@ static void initPool(Start start) {
160172
}
161173
Thread.sleep(getRetryIntervalIfInitFails(start));
162174
} catch (InterruptedException ex) {
163-
throw new QuitProgramFromPluginException(errorMessage);
175+
throw new DbInitException(errorMessage);
164176
}
165177
} else {
166-
throw e;
178+
throw new DbInitException(e);
167179
}
168180
}
169181
}
@@ -174,19 +186,28 @@ static void initPool(Start start) {
174186

175187
public static Connection getConnection(Start start) throws SQLException {
176188
if (getInstance(start) == null) {
177-
throw new QuitProgramFromPluginException("Please call initPool before getConnection");
189+
throw new IllegalStateException("Please call initPool before getConnection");
178190
}
179191
if (!start.enabled) {
180192
throw new SQLException("Storage layer disabled");
181193
}
182-
return ConnectionPool.hikariDataSource.getConnection();
194+
if (getInstance(start).hikariDataSource == null) {
195+
getInstance(start).initialiseHikariDataSource();
196+
}
197+
return getInstance(start).hikariDataSource.getConnection();
183198
}
184199

185200
static void close(Start start) {
186201
if (getInstance(start) == null) {
187202
return;
188203
}
189-
ConnectionPool.hikariDataSource.close();
190-
ConnectionPool.hikariDataSource = null;
204+
if (getInstance(start).hikariDataSource != null) {
205+
try {
206+
getInstance(start).hikariDataSource.close();
207+
} finally {
208+
// we mark it as null so that next time it's being initialised, it will be initialised again
209+
getInstance(start).hikariDataSource = null;
210+
}
211+
}
191212
}
192213
}

0 commit comments

Comments
 (0)