2019-08-13 15:10:23 +03:00
|
|
|
package pro.gravit.launchserver.auth;
|
|
|
|
|
2019-08-31 15:44:43 +03:00
|
|
|
import com.zaxxer.hikari.HikariDataSource;
|
2021-04-13 12:47:42 +03:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2019-10-19 19:46:04 +03:00
|
|
|
import org.postgresql.ds.PGSimpleDataSource;
|
2019-08-31 15:44:43 +03:00
|
|
|
import pro.gravit.utils.helper.LogHelper;
|
|
|
|
import pro.gravit.utils.helper.VerifyHelper;
|
2019-08-13 15:10:23 +03:00
|
|
|
|
2019-10-19 19:46:04 +03:00
|
|
|
import javax.sql.DataSource;
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
2019-08-13 15:10:23 +03:00
|
|
|
public final class PostgreSQLSourceConfig implements AutoCloseable {
|
|
|
|
public static final int TIMEOUT = VerifyHelper.verifyInt(
|
|
|
|
Integer.parseUnsignedInt(System.getProperty("launcher.postgresql.idleTimeout", Integer.toString(5000))),
|
|
|
|
VerifyHelper.POSITIVE, "launcher.postgresql.idleTimeout can't be <= 5000");
|
|
|
|
private static final int MAX_POOL_SIZE = VerifyHelper.verifyInt(
|
|
|
|
Integer.parseUnsignedInt(System.getProperty("launcher.postgresql.maxPoolSize", Integer.toString(3))),
|
|
|
|
VerifyHelper.POSITIVE, "launcher.postgresql.maxPoolSize can't be <= 0");
|
|
|
|
|
|
|
|
// Instance
|
|
|
|
private String poolName;
|
2021-04-13 12:47:42 +03:00
|
|
|
private transient final Logger logger = LogManager.getLogger();
|
2019-08-13 15:10:23 +03:00
|
|
|
|
|
|
|
// Config
|
2020-04-05 10:20:35 +03:00
|
|
|
private String[] addresses;
|
|
|
|
private int[] ports;
|
2019-08-13 15:10:23 +03:00
|
|
|
private String username;
|
|
|
|
private String password;
|
|
|
|
private String database;
|
|
|
|
|
|
|
|
// Cache
|
|
|
|
private DataSource source;
|
|
|
|
private boolean hikari;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized void close() {
|
|
|
|
if (hikari) { // Shutdown hikari pool
|
|
|
|
((HikariDataSource) source).close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized Connection getConnection() throws SQLException {
|
|
|
|
if (source == null) { // New data source
|
|
|
|
PGSimpleDataSource postgresqlSource = new PGSimpleDataSource();
|
|
|
|
|
|
|
|
// Set credentials
|
2020-04-05 10:20:35 +03:00
|
|
|
postgresqlSource.setServerNames(addresses);
|
|
|
|
postgresqlSource.setPortNumbers(ports);
|
2019-08-13 15:10:23 +03:00
|
|
|
postgresqlSource.setUser(username);
|
|
|
|
postgresqlSource.setPassword(password);
|
|
|
|
postgresqlSource.setDatabaseName(database);
|
|
|
|
|
|
|
|
// Try using HikariCP
|
|
|
|
source = postgresqlSource;
|
|
|
|
|
|
|
|
//noinspection Duplicates
|
|
|
|
try {
|
|
|
|
Class.forName("com.zaxxer.hikari.HikariDataSource");
|
|
|
|
hikari = true; // Used for shutdown. Not instanceof because of possible classpath error
|
|
|
|
|
|
|
|
// Set HikariCP pool
|
|
|
|
HikariDataSource hikariSource = new HikariDataSource();
|
|
|
|
hikariSource.setDataSource(source);
|
|
|
|
|
|
|
|
// Set pool settings
|
|
|
|
hikariSource.setPoolName(poolName);
|
|
|
|
hikariSource.setMinimumIdle(0);
|
|
|
|
hikariSource.setMaximumPoolSize(MAX_POOL_SIZE);
|
|
|
|
hikariSource.setIdleTimeout(TIMEOUT * 1000L);
|
|
|
|
|
|
|
|
// Replace source with hds
|
|
|
|
source = hikariSource;
|
2021-04-13 12:47:42 +03:00
|
|
|
logger.info("HikariCP pooling enabled for '{}'", poolName);
|
2019-08-13 15:10:23 +03:00
|
|
|
} catch (ClassNotFoundException ignored) {
|
2021-04-13 12:47:42 +03:00
|
|
|
logger.warn("HikariCP isn't in classpath for '{}'", poolName);
|
2019-08-13 15:10:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return source.getConnection();
|
|
|
|
}
|
|
|
|
}
|