2019-06-02 05:03:08 +03:00
|
|
|
package pro.gravit.launchserver.auth;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
|
|
|
import com.mysql.cj.jdbc.MysqlDataSource;
|
|
|
|
import com.zaxxer.hikari.HikariConfig;
|
|
|
|
import com.zaxxer.hikari.HikariDataSource;
|
2019-06-02 05:03:08 +03:00
|
|
|
import pro.gravit.utils.helper.LogHelper;
|
|
|
|
import pro.gravit.utils.helper.VerifyHelper;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
2019-10-19 19:46:04 +03:00
|
|
|
import javax.sql.DataSource;
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
2018-12-23 19:22:19 +03:00
|
|
|
public final class MySQLSourceConfig implements AutoCloseable {
|
2018-10-13 11:01:10 +03:00
|
|
|
|
2018-09-17 10:07:32 +03:00
|
|
|
public static final int TIMEOUT = VerifyHelper.verifyInt(
|
|
|
|
Integer.parseUnsignedInt(System.getProperty("launcher.mysql.idleTimeout", Integer.toString(5000))),
|
|
|
|
VerifyHelper.POSITIVE, "launcher.mysql.idleTimeout can't be <= 5000");
|
|
|
|
private static final int MAX_POOL_SIZE = VerifyHelper.verifyInt(
|
|
|
|
Integer.parseUnsignedInt(System.getProperty("launcher.mysql.maxPoolSize", Integer.toString(3))),
|
|
|
|
VerifyHelper.POSITIVE, "launcher.mysql.maxPoolSize can't be <= 0");
|
|
|
|
|
|
|
|
// Instance
|
2019-01-15 06:35:39 +03:00
|
|
|
private transient final String poolName;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
|
|
|
// Config
|
2018-12-23 19:22:19 +03:00
|
|
|
private String address;
|
|
|
|
private int port;
|
|
|
|
private boolean useSSL;
|
|
|
|
private boolean verifyCertificates;
|
|
|
|
private String username;
|
|
|
|
private String password;
|
|
|
|
private String database;
|
2018-09-17 10:07:32 +03:00
|
|
|
private String timeZone;
|
2019-03-12 11:08:51 +03:00
|
|
|
private boolean enableHikari;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
|
|
|
// Cache
|
2018-12-24 12:23:00 +03:00
|
|
|
private transient DataSource source;
|
|
|
|
private transient boolean hikari;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
2018-10-13 11:01:10 +03:00
|
|
|
|
2018-12-23 19:22:19 +03:00
|
|
|
public MySQLSourceConfig(String poolName) {
|
2018-09-17 10:07:32 +03:00
|
|
|
this.poolName = poolName;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized void close() {
|
|
|
|
if (hikari)
|
2018-09-22 17:33:00 +03:00
|
|
|
((HikariDataSource) source).close();
|
2018-09-17 10:07:32 +03:00
|
|
|
}
|
|
|
|
|
2018-10-13 11:01:10 +03:00
|
|
|
|
2018-09-17 10:07:32 +03:00
|
|
|
public synchronized Connection getConnection() throws SQLException {
|
|
|
|
if (source == null) { // New data source
|
|
|
|
MysqlDataSource mysqlSource = new MysqlDataSource();
|
|
|
|
mysqlSource.setCharacterEncoding("UTF-8");
|
|
|
|
|
|
|
|
// Prep statements cache
|
|
|
|
mysqlSource.setPrepStmtCacheSize(250);
|
|
|
|
mysqlSource.setPrepStmtCacheSqlLimit(2048);
|
|
|
|
mysqlSource.setCachePrepStmts(true);
|
|
|
|
mysqlSource.setUseServerPrepStmts(true);
|
|
|
|
|
|
|
|
// General optimizations
|
|
|
|
mysqlSource.setCacheServerConfiguration(true);
|
|
|
|
mysqlSource.setUseLocalSessionState(true);
|
|
|
|
mysqlSource.setRewriteBatchedStatements(true);
|
|
|
|
mysqlSource.setMaintainTimeStats(false);
|
|
|
|
mysqlSource.setUseUnbufferedInput(false);
|
|
|
|
mysqlSource.setUseReadAheadInput(false);
|
|
|
|
mysqlSource.setUseSSL(useSSL);
|
|
|
|
mysqlSource.setVerifyServerCertificate(verifyCertificates);
|
|
|
|
// Set credentials
|
|
|
|
mysqlSource.setServerName(address);
|
|
|
|
mysqlSource.setPortNumber(port);
|
|
|
|
mysqlSource.setUser(username);
|
|
|
|
mysqlSource.setPassword(password);
|
|
|
|
mysqlSource.setDatabaseName(database);
|
|
|
|
mysqlSource.setTcpNoDelay(true);
|
|
|
|
if (timeZone != null) mysqlSource.setServerTimezone(timeZone);
|
|
|
|
hikari = false;
|
|
|
|
// Try using HikariCP
|
|
|
|
source = mysqlSource;
|
2019-04-03 16:27:40 +03:00
|
|
|
if (enableHikari) {
|
2019-03-12 11:08:51 +03:00
|
|
|
try {
|
|
|
|
Class.forName("com.zaxxer.hikari.HikariDataSource");
|
|
|
|
hikari = true; // Used for shutdown. Not instanceof because of possible classpath error
|
|
|
|
HikariConfig cfg = new HikariConfig();
|
|
|
|
cfg.setDataSource(mysqlSource);
|
|
|
|
cfg.setPoolName(poolName);
|
|
|
|
cfg.setMaximumPoolSize(MAX_POOL_SIZE);
|
|
|
|
// Set HikariCP pool
|
|
|
|
// Replace source with hds
|
|
|
|
source = new HikariDataSource(cfg);
|
|
|
|
LogHelper.warning("HikariCP pooling enabled for '%s'", poolName);
|
|
|
|
} catch (ClassNotFoundException ignored) {
|
|
|
|
LogHelper.debug("HikariCP isn't in classpath for '%s'", poolName);
|
|
|
|
}
|
2018-09-17 10:07:32 +03:00
|
|
|
}
|
2019-03-12 11:08:51 +03:00
|
|
|
|
2018-09-17 10:07:32 +03:00
|
|
|
}
|
|
|
|
return source.getConnection();
|
|
|
|
}
|
|
|
|
}
|