forked from bootswithdefer/LogBlock
/
MySQLConnectionPool.java
51 lines (39 loc) · 1.64 KB
/
MySQLConnectionPool.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package de.diddiz.util;
import com.zaxxer.hikari.HikariDataSource;
import de.diddiz.LogBlock.config.Config;
import java.io.Closeable;
import java.sql.Connection;
import java.sql.SQLException;
public class MySQLConnectionPool implements Closeable {
private final HikariDataSource ds;
public MySQLConnectionPool(String url, String user, String password, boolean requireSSL) {
this.ds = new HikariDataSource();
ds.setJdbcUrl(url);
ds.setUsername(user);
ds.setPassword(password);
ds.setMinimumIdle(2);
ds.setMaximumPoolSize(15);
ds.setPoolName("LogBlock-Connection-Pool");
ds.addDataSourceProperty("useUnicode", "true");
ds.addDataSourceProperty("characterEncoding", "utf-8");
ds.addDataSourceProperty("rewriteBatchedStatements", "true");
ds.addDataSourceProperty("cachePrepStmts", "true");
ds.addDataSourceProperty("prepStmtCacheSize", "250");
ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
ds.addDataSourceProperty("useServerPrepStmts", "true");
ds.addDataSourceProperty("useSSL", "true");
ds.addDataSourceProperty("requireSSL", Boolean.toString(requireSSL));
ds.addDataSourceProperty("verifyServerCertificate", "false");
}
@Override
public void close() {
ds.close();
}
public Connection getConnection() throws SQLException {
Connection connection = ds.getConnection();
if (Config.mb4) {
connection.createStatement().executeQuery("SET NAMES utf8mb4");
}
return connection;
}
}