mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2024-11-23 13:46:46 +01:00
Major optimization to how MySQL connection is handled.
This commit is contained in:
parent
d7d1d0b58d
commit
26c9ba0c3b
@ -29,11 +29,31 @@ import com.gmail.nossr50.config.LoadProperties;
|
|||||||
public class Database {
|
public class Database {
|
||||||
|
|
||||||
private mcMMO plugin;
|
private mcMMO plugin;
|
||||||
private String connectionString;
|
private String connectionString = "jdbc:mysql://" + LoadProperties.MySQLserverName + ":" + LoadProperties.MySQLport + "/" + LoadProperties.MySQLdbName + "?user=" + LoadProperties.MySQLuserName + "&password=" + LoadProperties.MySQLdbPass;
|
||||||
|
private boolean isConnected = false;
|
||||||
|
private Connection conn = null;
|
||||||
|
|
||||||
|
public void connect()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
System.out.println("[mcMMO] Attempting connection to MySQL...");
|
||||||
|
conn = DriverManager.getConnection(connectionString);
|
||||||
|
isConnected = true;
|
||||||
|
System.out.println("[mcMMO] Connection to MySQL established!");
|
||||||
|
} catch (SQLException ex)
|
||||||
|
{
|
||||||
|
isConnected = false;
|
||||||
|
ex.printStackTrace();
|
||||||
|
System.out.println("SQLException: " + ex.getMessage());
|
||||||
|
System.out.println("SQLState: " + ex.getSQLState());
|
||||||
|
System.out.println("VendorError: " + ex.getErrorCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Database(mcMMO instance) {
|
public Database(mcMMO instance) {
|
||||||
|
connect(); //Connect to MySQL
|
||||||
this.plugin = instance;
|
this.plugin = instance;
|
||||||
this.connectionString = "jdbc:mysql://" + LoadProperties.MySQLserverName + ":" + LoadProperties.MySQLport + "/" + LoadProperties.MySQLdbName + "?user=" + LoadProperties.MySQLuserName + "&password=" + LoadProperties.MySQLdbPass;
|
|
||||||
// Load the driver instance
|
// Load the driver instance
|
||||||
try {
|
try {
|
||||||
Class.forName("com.mysql.jdbc.Driver");
|
Class.forName("com.mysql.jdbc.Driver");
|
||||||
@ -118,7 +138,6 @@ public class Database {
|
|||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
HashMap<Integer, ArrayList<String>> Rows = new HashMap<Integer, ArrayList<String>>();
|
HashMap<Integer, ArrayList<String>> Rows = new HashMap<Integer, ArrayList<String>>();
|
||||||
try {
|
try {
|
||||||
Connection conn = DriverManager.getConnection(connectionString);
|
|
||||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||||
if (stmt.executeQuery() != null) {
|
if (stmt.executeQuery() != null) {
|
||||||
stmt.executeQuery();
|
stmt.executeQuery();
|
||||||
@ -131,7 +150,6 @@ public class Database {
|
|||||||
Rows.put(rs.getRow(), Col);
|
Rows.put(rs.getRow(), Col);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
conn.close();
|
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
System.out.println("Updating mcMMO MySQL tables...");
|
System.out.println("Updating mcMMO MySQL tables...");
|
||||||
Write("ALTER TABLE `"+LoadProperties.MySQLtablePrefix + "skills` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;");
|
Write("ALTER TABLE `"+LoadProperties.MySQLtablePrefix + "skills` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;");
|
||||||
@ -141,18 +159,30 @@ public class Database {
|
|||||||
|
|
||||||
// write query
|
// write query
|
||||||
public boolean Write(String sql) {
|
public boolean Write(String sql) {
|
||||||
try {
|
if(conn != null)
|
||||||
Connection conn = DriverManager.getConnection(connectionString);
|
{
|
||||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
try {
|
||||||
stmt.executeUpdate();
|
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||||
conn.close();
|
stmt.executeUpdate();
|
||||||
return true;
|
return true;
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
System.out.println("SQLException: " + ex.getMessage());
|
System.out.println("SQLException: " + ex.getMessage());
|
||||||
System.out.println("SQLState: " + ex.getSQLState());
|
System.out.println("SQLState: " + ex.getSQLState());
|
||||||
System.out.println("VendorError: " + ex.getErrorCode());
|
System.out.println("VendorError: " + ex.getErrorCode());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
isConnected = false;
|
||||||
|
connect(); //Attempt to reconnect
|
||||||
|
if(isConnected = true)
|
||||||
|
{
|
||||||
|
Write(sql); //Try the same operation again now that we are connected
|
||||||
|
} else {
|
||||||
|
System.out.println("[mcMMO] Unable to connect to MySQL! Make sure the SQL server is online!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get Int
|
// Get Int
|
||||||
@ -160,26 +190,35 @@ public class Database {
|
|||||||
public Integer GetInt(String sql) {
|
public Integer GetInt(String sql) {
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
Integer result = 0;
|
Integer result = 0;
|
||||||
try {
|
if(conn != null)
|
||||||
Connection conn = DriverManager.getConnection(connectionString);
|
{
|
||||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
try {
|
||||||
stmt = conn.prepareStatement(sql);
|
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||||
if (stmt.executeQuery() != null) {
|
stmt = conn.prepareStatement(sql);
|
||||||
stmt.executeQuery();
|
if (stmt.executeQuery() != null) {
|
||||||
rs = stmt.getResultSet();
|
stmt.executeQuery();
|
||||||
if (rs.next()) {
|
rs = stmt.getResultSet();
|
||||||
result = rs.getInt(1);
|
if (rs.next()) {
|
||||||
} else {
|
result = rs.getInt(1);
|
||||||
result = 0;
|
} else {
|
||||||
}
|
result = 0;
|
||||||
}
|
}
|
||||||
conn.close();
|
}
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
System.out.println("SQLException: " + ex.getMessage());
|
System.out.println("SQLException: " + ex.getMessage());
|
||||||
System.out.println("SQLState: " + ex.getSQLState());
|
System.out.println("SQLState: " + ex.getSQLState());
|
||||||
System.out.println("VendorError: " + ex.getErrorCode());
|
System.out.println("VendorError: " + ex.getErrorCode());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
isConnected = false;
|
||||||
|
connect(); //Attempt to reconnect
|
||||||
|
if(isConnected = true)
|
||||||
|
{
|
||||||
|
GetInt(sql); //Try the same operation again now that we are connected
|
||||||
|
} else {
|
||||||
|
System.out.println("[mcMMO] Unable to connect to MySQL! Make sure the SQL server is online!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,25 +226,35 @@ public class Database {
|
|||||||
public HashMap<Integer, ArrayList<String>> Read(String sql) {
|
public HashMap<Integer, ArrayList<String>> Read(String sql) {
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
HashMap<Integer, ArrayList<String>> Rows = new HashMap<Integer, ArrayList<String>>();
|
HashMap<Integer, ArrayList<String>> Rows = new HashMap<Integer, ArrayList<String>>();
|
||||||
try {
|
if(conn != null)
|
||||||
Connection conn = DriverManager.getConnection(connectionString);
|
{
|
||||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
try {
|
||||||
if (stmt.executeQuery() != null) {
|
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||||
stmt.executeQuery();
|
if (stmt.executeQuery() != null) {
|
||||||
rs = stmt.getResultSet();
|
stmt.executeQuery();
|
||||||
while (rs.next()) {
|
rs = stmt.getResultSet();
|
||||||
ArrayList<String> Col = new ArrayList<String>();
|
while (rs.next()) {
|
||||||
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
|
ArrayList<String> Col = new ArrayList<String>();
|
||||||
Col.add(rs.getString(i));
|
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
|
||||||
}
|
Col.add(rs.getString(i));
|
||||||
Rows.put(rs.getRow(), Col);
|
}
|
||||||
}
|
Rows.put(rs.getRow(), Col);
|
||||||
}
|
}
|
||||||
conn.close();
|
}
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
System.out.println("SQLException: " + ex.getMessage());
|
System.out.println("SQLException: " + ex.getMessage());
|
||||||
System.out.println("SQLState: " + ex.getSQLState());
|
System.out.println("SQLState: " + ex.getSQLState());
|
||||||
System.out.println("VendorError: " + ex.getErrorCode());
|
System.out.println("VendorError: " + ex.getErrorCode());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
isConnected = false;
|
||||||
|
connect(); //Attempt to reconnect
|
||||||
|
if(isConnected = true)
|
||||||
|
{
|
||||||
|
Read(sql); //Attempt the same operation again now that we are connected
|
||||||
|
} else {
|
||||||
|
System.out.println("[mcMMO] Unable to connect to MySQL! Make sure the SQL server is online!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Rows;
|
return Rows;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user