Changed all mysql connections to be persistent.

This commit is contained in:
Jonathan Williams 2013-12-06 01:57:09 -08:00
parent 8984309c56
commit 03627eb0c3
9 changed files with 56 additions and 189 deletions

View File

@ -18,6 +18,7 @@ public class Mineplexer extends Plugin
new MotdManager(this);
new LobbyBalancer(this);
new PlayerCount(this);
new FileUpdater(this);
/*
Socket socket = null;

View File

@ -1,5 +1,6 @@
package mineplex.bungee.lobbyBalancer;
import java.io.File;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collections;
@ -27,7 +28,10 @@ public class LobbyBalancer implements Listener, Runnable
{
_plugin = plugin;
_repository = new LobbyBalancerRepository();
_repository.initialize(true);
boolean us = !new File("eu.dat").exists();
_repository.initialize(us);
loadLobbyServers();

View File

@ -10,6 +10,7 @@ import java.util.List;
public class LobbyBalancerRepository
{
private Connection _connection = null;
private String _connectionString = "jdbc:mysql://sql.mineplex.com:3306/ServerStatus?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh";
@ -22,15 +23,15 @@ public class LobbyBalancerRepository
{
_us = us;
Connection connection = null;
PreparedStatement preparedStatement = null;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
// Create table
preparedStatement = connection.prepareStatement(CREATE_TABLE);
preparedStatement = _connection.prepareStatement(CREATE_TABLE);
preparedStatement.execute();
}
catch (Exception exception)
@ -50,33 +51,21 @@ public class LobbyBalancerRepository
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public List<ServerStatusData> retrieveServerStatuses()
{
Connection connection = null;
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
List<ServerStatusData> serverData = new ArrayList<ServerStatusData>();
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = connection.prepareStatement(RETRIEVE_SERVER_STATUSES);
preparedStatement = _connection.prepareStatement(RETRIEVE_SERVER_STATUSES);
preparedStatement.setBoolean(1, _us);
resultSet = preparedStatement.executeQuery();
@ -137,18 +126,6 @@ public class LobbyBalancerRepository
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return serverData;

View File

@ -8,6 +8,7 @@ import java.sql.SQLException;
public class MotdRepository
{
private Connection _connection = null;
private String _connectionString = "jdbc:mysql://sql.mineplex.com:3306/BungeeServers?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh";
@ -17,15 +18,15 @@ public class MotdRepository
public void initialize()
{
Connection connection = null;
PreparedStatement preparedStatement = null;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
// Create table
preparedStatement = connection.prepareStatement(CREATE_TABLE);
preparedStatement = _connection.prepareStatement(CREATE_TABLE);
preparedStatement.execute();
}
catch (Exception exception)
@ -45,33 +46,21 @@ public class MotdRepository
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public String retrieveMotd()
{
String motd = "§b§l§m §8§l§m[ §r §9§lMineplex§r §f§lGames§r §8§l§m ]§b§l§m §r §c§l§m§kZ§6§l§m§kZ§e§l§m§kZ§a§l§m§kZ§b§l§m§kZ§r §f§lPLAY NOW§r §b§l§m§kZ§a§l§m§kZ§e§l§m§kZ§6§l§m§kZ§c§l§m§kZ";
Connection connection = null;
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = connection.prepareStatement(RETRIEVE_MOTD);
preparedStatement = _connection.prepareStatement(RETRIEVE_MOTD);
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
@ -108,18 +97,6 @@ public class MotdRepository
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return motd;

View File

@ -9,6 +9,7 @@ import java.sql.Statement;
public class PlayerCountRepository
{
private Connection _connection = null;
private String _connectionString = "jdbc:mysql://sql.mineplex.com:3306/BungeeServers?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh";
@ -31,7 +32,6 @@ public class PlayerCountRepository
public void initialize()
{
Connection connection = null;
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
PreparedStatement preparedStatementRetrieve = null;
@ -39,15 +39,16 @@ public class PlayerCountRepository
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
// Create table
preparedStatement = connection.prepareStatement(CREATE_TABLE);
preparedStatement = _connection.prepareStatement(CREATE_TABLE);
preparedStatement.execute();
// Retrieve id
preparedStatementRetrieve = connection.prepareStatement(RETRIEVE_ID);
preparedStatementRetrieve = _connection.prepareStatement(RETRIEVE_ID);
preparedStatementRetrieve.setString(1, _address);
resultSet = preparedStatementRetrieve.executeQuery();
@ -59,7 +60,7 @@ public class PlayerCountRepository
// Insert if not there
if (_id == -1)
{
preparedStatementInsert = connection.prepareStatement(INSERT_PLAYER_COUNT, Statement.RETURN_GENERATED_KEYS);
preparedStatementInsert = _connection.prepareStatement(INSERT_PLAYER_COUNT, Statement.RETURN_GENERATED_KEYS);
preparedStatementInsert.setString(1, _address);
preparedStatementInsert.setInt(2, 0);
@ -137,31 +138,19 @@ public class PlayerCountRepository
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public boolean updatePlayerCountInDatabase(int players)
{
Connection connection = null;
PreparedStatement preparedStatement = null;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = connection.prepareStatement(UPDATE_PLAYER_COUNT, Statement.RETURN_GENERATED_KEYS);
preparedStatement = _connection.prepareStatement(UPDATE_PLAYER_COUNT, Statement.RETURN_GENERATED_KEYS);
preparedStatement.setInt(1, players);
preparedStatement.setInt(2, _maxPlayers);
@ -206,33 +195,21 @@ public class PlayerCountRepository
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public PlayerTotalData retrievePlayerCount()
{
Connection connection = null;
PlayerTotalData playerData = new PlayerTotalData();
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = connection.prepareStatement(RETRIEVE_PLAYER_COUNT);
preparedStatement = _connection.prepareStatement(RETRIEVE_PLAYER_COUNT);
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
@ -271,18 +248,6 @@ public class PlayerCountRepository
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return playerData;

View File

@ -13,6 +13,7 @@ public class AntiHackRepository
private AntiHack _plugin;
private String _serverName;
private Connection _connection;
private String _connectionString = "jdbc:mysql://sql.mineplex.com:3306/Mineplex";
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh";
@ -28,15 +29,15 @@ public class AntiHackRepository
public void initialize()
{
Connection connection = null;
PreparedStatement preparedStatement = null;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
// Create table
preparedStatement = connection.prepareStatement(CREATE_TABLE);
preparedStatement = _connection.prepareStatement(CREATE_TABLE);
preparedStatement.execute();
}
catch (Exception exception)
@ -56,18 +57,6 @@ public class AntiHackRepository
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
@ -77,14 +66,13 @@ public class AntiHackRepository
{
public void run()
{
Connection connection = null;
PreparedStatement preparedStatement = null;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = connection.prepareStatement(UPDATE_PLAYER_OFFENSES);
preparedStatement = _connection.prepareStatement(UPDATE_PLAYER_OFFENSES);
/* XXX
synchronized (_plugin.getOffensesSynch())
@ -124,18 +112,6 @@ public class AntiHackRepository
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
}).start();

View File

@ -46,15 +46,13 @@ public class ServerStatusManager extends MiniPlugin
String address = "localhost";
System.out.println("Enabled : " + _enabled);
try
{
address = InetAddress.getLocalHost().getHostAddress();
}
catch (UnknownHostException e1)
{
System.out.println("Invalid host address, grabbing from eth1");
System.out.println("Invalid host address, grabbing from eth0");
}
try

View File

@ -29,6 +29,8 @@ public class ServerStatusRepository
private String _address;
private int _maxPlayers = 0;
Connection _connection = null;
public ServerStatusRepository(String connectionUrl, String username, String password, boolean us, String serverName, String serverGroup, String address, int maxPlayers)
{
_connectionString = connectionUrl;
@ -43,7 +45,6 @@ public class ServerStatusRepository
public void initialize()
{
Connection connection = null;
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
PreparedStatement preparedStatementRetrieve = null;
@ -51,15 +52,15 @@ public class ServerStatusRepository
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
// Create table
preparedStatement = connection.prepareStatement(CREATE_TABLE);
preparedStatement = _connection.prepareStatement(CREATE_TABLE);
preparedStatement.execute();
// Retrieve id
preparedStatementRetrieve = connection.prepareStatement(RETRIEVE_ID);
preparedStatementRetrieve = _connection.prepareStatement(RETRIEVE_ID);
preparedStatementRetrieve.setString(1, _address);
resultSet = preparedStatementRetrieve.executeQuery();
@ -71,7 +72,7 @@ public class ServerStatusRepository
// Insert if not there
if (_id == -1)
{
preparedStatementInsert = connection.prepareStatement(INSERT_PLAYER_COUNT, Statement.RETURN_GENERATED_KEYS);
preparedStatementInsert = _connection.prepareStatement(INSERT_PLAYER_COUNT, Statement.RETURN_GENERATED_KEYS);
preparedStatementInsert.setString(1, _serverName);
preparedStatementInsert.setString(2, _serverGroup);
@ -154,31 +155,21 @@ public class ServerStatusRepository
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public boolean updatePlayerCountInDatabase(String motd, int players, int maxPlayers, int tps)
{
Connection connection = null;
PreparedStatement preparedStatement = null;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = connection.prepareStatement(players != 0 ? UPDATE_PLAYER_COUNT_WITH_PLAYERS : UPDATE_PLAYER_COUNT_WITHOUT_PLAYERS, Statement.RETURN_GENERATED_KEYS);
preparedStatement = _connection.prepareStatement(players != 0 ? UPDATE_PLAYER_COUNT_WITH_PLAYERS : UPDATE_PLAYER_COUNT_WITHOUT_PLAYERS, Statement.RETURN_GENERATED_KEYS);
preparedStatement.setString(1, _serverName);
preparedStatement.setString(2, _serverGroup);
@ -217,33 +208,23 @@ public class ServerStatusRepository
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public List<ServerStatusData> retrieveServerStatuses()
{
Connection connection = null;
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
List<ServerStatusData> serverData = new ArrayList<ServerStatusData>();
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = connection.prepareStatement(RETRIEVE_SERVER_STATUSES);
preparedStatement = _connection.prepareStatement(RETRIEVE_SERVER_STATUSES);
preparedStatement.setBoolean(1, _us);
resultSet = preparedStatement.executeQuery();
@ -289,18 +270,6 @@ public class ServerStatusRepository
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return serverData;