-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from AzisabaNetwork/feat/hikaricp-support
Use hikariCP for connect to database
- Loading branch information
Showing
10 changed files
with
507 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/jp/azisaba/lgw/kdstatus/sql/DBAuthConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package jp.azisaba.lgw.kdstatus.sql; | ||
|
||
import jp.azisaba.lgw.kdstatus.KDStatusReloaded; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
|
||
import java.util.logging.Logger; | ||
|
||
/** | ||
* Safe auth config loader | ||
*/ | ||
public class DBAuthConfig { | ||
@Getter(AccessLevel.PROTECTED) | ||
private static String host; | ||
@Getter(AccessLevel.PROTECTED) | ||
private static String port; | ||
@Getter(AccessLevel.PROTECTED) | ||
private static String database; | ||
@Getter(AccessLevel.PROTECTED) | ||
private static String user; | ||
@Getter(AccessLevel.PROTECTED) | ||
private static String password; | ||
|
||
public static void loadAuthConfig() { | ||
host = getConfigAsString("host"); | ||
port = getConfigAsString("port"); | ||
database = getConfigAsString("database"); | ||
user = getConfigAsString("username"); | ||
password = getConfigAsString("password"); | ||
} | ||
|
||
public static HikariMySQLDatabase getDatabase(Logger logger, int maxPoolSize) { | ||
return new HikariMySQLDatabase( | ||
logger, | ||
maxPoolSize, | ||
host, | ||
port, | ||
database, | ||
user, | ||
password | ||
); | ||
} | ||
|
||
private static String getConfigAsString(String path) { | ||
return KDStatusReloaded.getPlugin().getConfig().getString(path); | ||
} | ||
} |
167 changes: 167 additions & 0 deletions
167
src/main/java/jp/azisaba/lgw/kdstatus/sql/HikariMySQLDatabase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package jp.azisaba.lgw.kdstatus.sql; | ||
|
||
|
||
import com.zaxxer.hikari.HikariConfig; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.function.Consumer; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* Wrapper class of HikariDataSource for MySQL | ||
*/ | ||
@RequiredArgsConstructor | ||
public class HikariMySQLDatabase { | ||
private final Logger logger; | ||
private final int maxPoolSize; | ||
private final String host, port, databaseName, user, password; | ||
|
||
@Getter | ||
private boolean initialized; | ||
private HikariDataSource hikari; | ||
|
||
public boolean isConnected() { | ||
if(hikari == null) return false; | ||
return hikari.isRunning(); | ||
} | ||
|
||
/** | ||
* connect to database. | ||
*/ | ||
public void connect() { | ||
if (initialized) { | ||
logger.warning("Database is already initialized!"); | ||
return; | ||
} | ||
String jdbcUrl = String.format( | ||
"jdbc:mysql://%s:%s/%s?useSSL=false", | ||
host, | ||
port, | ||
databaseName | ||
); | ||
|
||
HikariConfig config = new HikariConfig(); | ||
config.setUsername(user); | ||
config.setPassword(password); | ||
config.setJdbcUrl(jdbcUrl); | ||
config.setMaximumPoolSize(maxPoolSize); | ||
// config.setConnectionInitSql("SELECT 1"); | ||
config.setAutoCommit(true); | ||
config.setConnectionTimeout(1500); // TODO change this | ||
hikari = new HikariDataSource(config); | ||
} | ||
|
||
/** | ||
* close database connection | ||
*/ | ||
public void close() { | ||
hikari.close(); | ||
initialized = false; | ||
} | ||
|
||
/** | ||
* reconnect database | ||
*/ | ||
public void reconnect() { | ||
close(); | ||
connect(); | ||
} | ||
|
||
/** | ||
* CAUTION: this method throws {@link SQLException} so need to catch it | ||
* | ||
* @return a connection of database | ||
* @throws SQLException from {@link HikariDataSource#getConnection()} | ||
*/ | ||
public Connection getConnection() throws SQLException { | ||
return hikari.getConnection(); | ||
} | ||
|
||
/** | ||
* get a connection (Safer than {@link #getConnection()}) | ||
* | ||
* @return database connection. If failed, return null. | ||
*/ | ||
|
||
public Connection getConnectionOrNull() { | ||
try { | ||
return hikari.getConnection(); | ||
} catch (SQLException e) { | ||
logger.log(Level.SEVERE, "Failed to get connection", e); | ||
return null; | ||
} | ||
} | ||
|
||
|
||
public PreparedStatement preparedStatement(@NonNull String sql) { | ||
Connection conn = getConnectionOrNull(); | ||
if (conn == null) { | ||
logger.log(Level.SEVERE, "Failed to create preparedStatement: connection is null"); | ||
return null; | ||
} | ||
try { | ||
return conn.prepareStatement(sql); | ||
} catch (SQLException e) { | ||
logger.log(Level.SEVERE, "Failed to create preparedStatement", e); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* execute a SQL statement | ||
* | ||
* @param sql SQL statement | ||
* @param pstmtConsumer to process PreparedStatement (ex. {@link PreparedStatement#setString(int, String)}) | ||
* @return result of execution. If failed, return null | ||
*/ | ||
|
||
public ResultSet executeQuery(@NonNull String sql, Consumer<PreparedStatement> pstmtConsumer) { | ||
// get a connection | ||
Connection conn = getConnectionOrNull(); | ||
if (conn == null) { | ||
logger.warning("Failed to execute query: connection is null"); | ||
return null; | ||
} | ||
|
||
// execute query | ||
try { | ||
PreparedStatement pstmt = conn.prepareStatement(sql); | ||
if (pstmtConsumer != null) { | ||
pstmtConsumer.accept(pstmt); | ||
} | ||
return pstmt.executeQuery(); | ||
} catch (SQLException e) { | ||
logger.log(Level.SEVERE, "Failed to execute query", e); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* @param sql SQL statement | ||
* @return is succeeded | ||
*/ | ||
public boolean executeUpdate(String sql) { | ||
Connection conn = getConnectionOrNull(); | ||
if (conn == null) { | ||
logger.severe("Failed to execute update: connection is null"); | ||
return false; | ||
} | ||
|
||
try { | ||
PreparedStatement pstmt = conn.prepareStatement(sql); | ||
pstmt.executeUpdate(); | ||
return true; | ||
} catch (SQLException e) { | ||
logger.log(Level.SEVERE, "Failed to execute update", e); | ||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.