Skip to content

Commit

Permalink
・Database Connectionが必ずNullになるバグを修正
Browse files Browse the repository at this point in the history
・Initialize処理とDBチェックを分割
  • Loading branch information
waxsd100 committed Aug 22, 2020
1 parent 30c3c83 commit 904ca12
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/main/java/jp/skyblock/Command/PingCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class PingCommand implements CommandExecIf {
final Received received = new Received();

@Override
public void execute() {
public void execute() throws Exception {
MessageReceivedEvent event = Received.getEvent();
String[] cmdParam = Received.getCmdParam();
Guild guild = event.getGuild();
Expand All @@ -53,7 +53,7 @@ public void execute() {
eb.clear();

} catch (Exception e) {

throw sendError(e, "ERROR", event);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/jp/skyblock/Core/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static String getDir() {
*
* @return Properties
*/
public static Properties loadProperties(final String basePath) {
public Properties loadProperties(final String basePath) {
try {
try (InputStream inputStream = new FileInputStream(basePath)) {
Properties properties = new Properties();
Expand Down
62 changes: 44 additions & 18 deletions src/main/java/jp/skyblock/TusbChangRoleBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,42 @@ public class TusbChangRoleBot {
private static final String DISCORD_CONFIG_FILE = File.separator + "Discord.properties";
private static final String DATABASE_CONFIG_FILE = File.separator + "Database.properties";

/**
* main メソッド
* @param args
*/
public static void main(String[] args) {
loadConf();
logger.info("Config Load");
initialize();

try (Connection con = ConnectionPool.getConnection()) {

Statement statement = con.createStatement();
String sql = "SELECT CURRENT_TIMESTAMP;";
statement.executeQuery(sql);
ResultSet rs = statement.executeQuery(sql);
if (rs.next()) {
String now = rs.getString("CURRENT_TIMESTAMP");
System.out.println("現在時間: " + now);
}
} catch (SQLException e) {
e.printStackTrace();
}

checkDatabase();
}

/**
* Config File Load
*/
private static void loadConf() {
try {
PropertyConfigurator.configure("config/log4j.properties");
DISCORD_PROP_FILE = CONFIG_DIR + DISCORD_CONFIG_FILE;
DATABASE_PROP_FILE = CONFIG_DIR + DATABASE_CONFIG_FILE;
logger.info("Config Load");
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* 各種インスタンスをイニシャライズ
*/
private static void initialize() {
// Discord 用ConfigファイルをLoad
databaseInitialize();
discordInitialize();
}

/**
* Discord用インスタンスを初期化
*/
private static void discordInitialize(){
try {
String DISCORD_TOKEN = new Config().getValue(DISCORD_PROP_FILE, "Token");
JDA jda = createLight(DISCORD_TOKEN)
Expand All @@ -84,11 +86,35 @@ private static void initialize() {
e.printStackTrace();
System.exit(1);
}
}


/**
* Databaseインスタンスを作成する。
*/
private static void databaseInitialize(){
try {
ConnectionPool.initialize(DATABASE_PROP_FILE);
} catch (Exception e) {
throw new ServiceException(DatabaseError.DatabaseInit, e);
}
}

/**
* Database の接続確認をする。
*/
private static void checkDatabase(){
try (Connection con = ConnectionPool.getConnection()) {
Statement statement = con.createStatement();
String sql = "SELECT CURRENT_TIMESTAMP;";
statement.executeQuery(sql);
ResultSet rs = statement.executeQuery(sql);
if (rs.next()) {
String now = rs.getString("CURRENT_TIMESTAMP");
System.out.println("現在時間: " + now);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
10 changes: 5 additions & 5 deletions src/main/java/jp/skyblock/Utility/db/ConnectionPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package jp.skyblock.Utility.db;

import com.zaxxer.hikari.HikariDataSource;
import jp.skyblock.Core.Config;
import jp.skyblock.Core.Const.Enums.DatabaseError;
import jp.skyblock.Utility.Exception.ServiceException;

Expand All @@ -21,8 +22,6 @@
import java.util.Timer;
import java.util.TimerTask;

import static jp.skyblock.Core.Config.loadProperties;

public abstract class ConnectionPool {

/**
Expand Down Expand Up @@ -92,9 +91,10 @@ public static Connection getConnection(final Boolean retry) throws SQLException
try {
if (getInstance().dataSource != null) {
con = getInstance().dataSource.getConnection();
}else{
throw new ServiceException(DatabaseError.DatabaseState,
"Server : " + getDBParameter().getServers()[serverIndex]);
}
throw new ServiceException(DatabaseError.DatabaseState,
"Server : " + getDBParameter().getServers()[serverIndex]);
} catch (Exception e) {
if (retry) con = getReConnection();
else throw e;
Expand Down Expand Up @@ -158,7 +158,7 @@ private static ConnectionPool createInstance(final Properties prop) {
* @param prop Database Properties File
*/
public static void initialize(final String prop) {
Properties p = loadProperties(prop);
Properties p = new Config().loadProperties(prop);
if (instance == null) {
instance = createInstance(p);
}
Expand Down

0 comments on commit 904ca12

Please sign in to comment.