-
Notifications
You must be signed in to change notification settings - Fork 0
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 #10 from tamilpp25/feature-mysql-interface
Feature : MySQL Interfacing
- Loading branch information
Showing
11 changed files
with
613 additions
and
4 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
93 changes: 93 additions & 0 deletions
93
src/main/java/com/grubnest/game/core/DatabaseHandler/ConnectionPoolManager.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,93 @@ | ||
package com.grubnest.game.core.DatabaseHandler; | ||
|
||
import com.grubnest.game.core.DatabaseHandler.Utils.Deactivated; | ||
import com.grubnest.game.core.DatabaseHandler.Utils.Disabler; | ||
import com.zaxxer.hikari.HikariConfig; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
/** Handles connection from mysql and uses HikariCP | ||
* @author tamilpp25 | ||
* @version 1.0 at 15-5-2022 | ||
*/ | ||
public class ConnectionPoolManager implements Deactivated { | ||
private final MySQLData data; | ||
private HikariDataSource dataSource; | ||
|
||
|
||
/** | ||
* Construct mysql HikariCP | ||
* @param data MysqlDATA | ||
*/ | ||
public ConnectionPoolManager(MySQLData data) { | ||
this.data = data; | ||
setupPool(); | ||
Disabler.getInstance().registerDeactivated(this); | ||
} | ||
|
||
/** | ||
* Setup mysql connection Pool with HikariCP to get multiple connections. | ||
*/ | ||
private void setupPool() { | ||
HikariConfig config = new HikariConfig(); | ||
config.setJdbcUrl( | ||
"jdbc:mysql://" + | ||
data.HOST + | ||
":" + | ||
data.PORT+ | ||
"/" + | ||
data.DATABASE | ||
); | ||
config.setDriverClassName("com.mysql.jdbc.Driver"); | ||
config.setUsername(data.USERNAME); | ||
config.setPassword(data.PASSWORD); | ||
config.setMinimumIdle(data.minimumConnections); | ||
config.setMaximumPoolSize(data.maximumConnections); | ||
config.setConnectionTimeout(data.connectionTimeout); | ||
config.setConnectionTestQuery("SELECT 1"); | ||
dataSource = new HikariDataSource(config); | ||
} | ||
|
||
/** | ||
* Get MySQL Pool connection | ||
* @return Sql Connection | ||
* @throws SQLException if some error.. | ||
*/ | ||
public Connection getConnection() throws SQLException { | ||
return dataSource.getConnection(); | ||
} | ||
|
||
/** | ||
* Temp close method can close using try-with statement too | ||
* Recommending using try-with statement to close automatically! | ||
* @param conn Connection | ||
* @param ps PreparedStatement | ||
* @param res ResultSet | ||
*/ | ||
public void close(Connection conn, PreparedStatement ps, ResultSet res) { | ||
if (conn != null) try { conn.close(); } catch (SQLException ignored) {} | ||
if (ps != null) try { ps.close(); } catch (SQLException ignored) {} | ||
if (res != null) try { res.close(); } catch (SQLException ignored) {} | ||
} | ||
|
||
/** | ||
* Close pool when everything done... | ||
*/ | ||
public void closePool() { | ||
if (dataSource != null && !dataSource.isClosed()) { | ||
dataSource.close(); | ||
} | ||
} | ||
|
||
/** | ||
* Auto disable classes on disable if multiple instance are there too | ||
*/ | ||
@Override | ||
public void onDisable() { | ||
closePool(); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/grubnest/game/core/DatabaseHandler/MySQL.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,55 @@ | ||
package com.grubnest.game.core.DatabaseHandler; | ||
|
||
import com.grubnest.game.core.GrubnestCorePlugin; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
/** | ||
* Main class for handling mysql data | ||
* make your methods for getting / setting here in async! | ||
* new MySQL class can be initialized outside main class too! | ||
* | ||
* Recommend using getMySQL method from main class as it properly does the work and closes connection on disable! | ||
* make a new instance of this if you really know what you are doing! | ||
* | ||
* @author tamilpp25 | ||
* @version 1.0 at 15-5-2022 | ||
*/ | ||
public class MySQL extends ConnectionPoolManager { | ||
public MySQL(MySQLData data) { | ||
super(data); | ||
} | ||
|
||
/** | ||
* This is an example table query for creating queries make a similar method like this! | ||
* MAKE SURE that all queries are run async so that it doesn't freeze the main thread | ||
* you don't have to worry about closing a connection since its auto closed by | ||
*/ | ||
public void testTableQuery() { | ||
GrubnestCorePlugin.getInstance().getServer().getScheduler().runTaskAsynchronously(GrubnestCorePlugin.getInstance(),()->{ | ||
try (Connection conn = getConnection()){ | ||
PreparedStatement statement = conn.prepareStatement( | ||
"CREATE TABLE IF NOT EXISTS `Test` " + | ||
"(" + | ||
"UUID varchar(30)" + | ||
")" | ||
); | ||
statement.executeUpdate(); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
} | ||
|
||
//You make method to fetch / add data in this class | ||
|
||
/** | ||
* Close pool on plugin Disable | ||
*/ | ||
public void onDisable(){ | ||
closePool(); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/grubnest/game/core/DatabaseHandler/MySQLData.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,28 @@ | ||
package com.grubnest.game.core.DatabaseHandler; | ||
|
||
/** MysqlData object to store credentials etc | ||
* @author tamilpp25 | ||
* @version 1.0 at 15-5-2022 | ||
*/ | ||
public class MySQLData { | ||
public final String HOST; | ||
public final String USERNAME; | ||
public final String PASSWORD; | ||
public final String PORT; | ||
public final String DATABASE; | ||
|
||
public final int minimumConnections; | ||
public final int maximumConnections; | ||
public final long connectionTimeout; | ||
|
||
public MySQLData(String host, String username, String password, String port, String database, int minimumConnections, int maximumConnections, long connectionTimeout) { | ||
HOST = host; | ||
USERNAME = username; | ||
PASSWORD = password; | ||
PORT = port; | ||
DATABASE = database; | ||
this.minimumConnections = minimumConnections; | ||
this.maximumConnections = maximumConnections; | ||
this.connectionTimeout = connectionTimeout; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/grubnest/game/core/DatabaseHandler/Utils/Deactivated.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,12 @@ | ||
package com.grubnest.game.core.DatabaseHandler.Utils; | ||
|
||
/** | ||
* Utility class to prevent Memory leak. | ||
* @author tamilpp25 | ||
* @version 1.0 at 15-5-2022 | ||
*/ | ||
public interface Deactivated { | ||
|
||
void onDisable(); | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/grubnest/game/core/DatabaseHandler/Utils/Disabler.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,44 @@ | ||
package com.grubnest.game.core.DatabaseHandler.Utils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Disabler instance to auto disable any used instance of sql | ||
* @author tamilpp25 | ||
* @version 1.0 at 15-5-2022 | ||
*/ | ||
public class Disabler { | ||
private static Disabler instance = null; | ||
private final List<Deactivated> toDisable = new ArrayList<>(); | ||
|
||
public static Disabler getInstance() { | ||
if (instance != null) return instance; | ||
return instance = new Disabler(); | ||
} | ||
|
||
/** | ||
* close all active mysql connections | ||
*/ | ||
public void disableAll() { | ||
for (Deactivated toDeactivated : toDisable) | ||
toDeactivated.onDisable(); | ||
} | ||
|
||
/** | ||
* Disable specific class with mysql connection | ||
* @param sqlinstance SQL instance | ||
*/ | ||
public void disable(Deactivated sqlinstance) { | ||
sqlinstance.onDisable(); | ||
} | ||
|
||
/** | ||
* Register a class as mysql connection pool | ||
* @param pDeactivated Class to be registered | ||
*/ | ||
public void registerDeactivated(Deactivated pDeactivated) { | ||
toDisable.add(pDeactivated); | ||
} | ||
|
||
} |
50 changes: 49 additions & 1 deletion
50
src/main/java/com/grubnest/game/core/GrubnestCorePlugin.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 |
---|---|---|
@@ -1,22 +1,70 @@ | ||
package com.grubnest.game.core; | ||
|
||
import com.grubnest.game.core.DatabaseHandler.MySQL; | ||
import com.grubnest.game.core.DatabaseHandler.MySQLData; | ||
import com.grubnest.game.core.DatabaseHandler.Utils.Disabler; | ||
import org.bukkit.plugin.java.JavaPlugin; | ||
|
||
public class GrubnestCorePlugin extends JavaPlugin { | ||
|
||
private MySQL sql; | ||
private static GrubnestCorePlugin instance; | ||
/** | ||
* Runs when plugin is enabled | ||
*/ | ||
@Override | ||
public void onEnable() { | ||
instance = this; | ||
|
||
//Register Plugin messaging channels on enable | ||
this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord"); | ||
this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new PluginMessage()); | ||
|
||
getConfig().options().copyDefaults(true); | ||
saveConfig(); | ||
sql = new MySQL(dataInitializer()); | ||
} | ||
|
||
/** | ||
* Runs when plugin is disabled | ||
*/ | ||
@Override | ||
public void onDisable() { | ||
Disabler.getInstance().disableAll(); | ||
//Unregister channels on disable | ||
this.getServer().getMessenger().unregisterOutgoingPluginChannel(this); | ||
this.getServer().getMessenger().unregisterIncomingPluginChannel(this); | ||
} | ||
|
||
/** | ||
* Initialize data from config.yml | ||
* @return MySQLData | ||
*/ | ||
private MySQLData dataInitializer(){ | ||
String host = getConfig().getString("Database.hostname"); | ||
String port = getConfig().getString("Database.port"); | ||
String database = getConfig().getString("Database.database"); | ||
String username = getConfig().getString("Database.username"); | ||
String password = getConfig().getString("Database.password"); | ||
|
||
int minimumConnections = getConfig().getInt("Database.minimumConnections"); | ||
int maximumConnections = getConfig().getInt("Database.maximumConnections"); | ||
long connectionTimeout = getConfig().getLong("Database.connectionTimeout"); | ||
return new MySQLData(host,username,password,port,database,minimumConnections,maximumConnections,connectionTimeout); | ||
} | ||
|
||
/** | ||
* Get SQL Object | ||
* @return SQL object | ||
*/ | ||
public MySQL getMySQL(){ | ||
return sql; | ||
} | ||
|
||
/** | ||
* Get Plugin Instance | ||
* @return Plugin Instance | ||
*/ | ||
public static GrubnestCorePlugin getInstance(){ | ||
return instance; | ||
} | ||
} |
Oops, something went wrong.