From 03627eb0c33d84e5f15069bf9f2d8e9b3fc143c0 Mon Sep 17 00:00:00 2001 From: Jonathan Williams Date: Fri, 6 Dec 2013 01:57:09 -0800 Subject: [PATCH] Changed all mysql connections to be persistent. --- .../src/mineplex/bungee/Mineplexer.java | 1 + .../bungee/lobbyBalancer/LobbyBalancer.java | 6 +- .../LobbyBalancerRepository.java | 37 +++-------- .../mineplex/bungee/motd/MotdRepository.java | 37 +++-------- .../playerCount/PlayerCountRepository.java | 59 ++++------------- .../core/antihack/AntiHackRepository.java | 36 ++--------- .../core/status/ServerStatusManager.java | 4 +- .../core/status/ServerStatusRepository.java | 63 +++++-------------- .../src/mineplex/hub/modules/MapManager.java | 2 +- 9 files changed, 56 insertions(+), 189 deletions(-) diff --git a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/Mineplexer.java b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/Mineplexer.java index 4c9cf2518..a24de1edc 100644 --- a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/Mineplexer.java +++ b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/Mineplexer.java @@ -18,6 +18,7 @@ public class Mineplexer extends Plugin new MotdManager(this); new LobbyBalancer(this); new PlayerCount(this); + new FileUpdater(this); /* Socket socket = null; diff --git a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/lobbyBalancer/LobbyBalancer.java b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/lobbyBalancer/LobbyBalancer.java index f019ad137..7f1c534db 100644 --- a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/lobbyBalancer/LobbyBalancer.java +++ b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/lobbyBalancer/LobbyBalancer.java @@ -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(); diff --git a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/lobbyBalancer/LobbyBalancerRepository.java b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/lobbyBalancer/LobbyBalancerRepository.java index 01d38ea1e..149f039a6 100644 --- a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/lobbyBalancer/LobbyBalancerRepository.java +++ b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/lobbyBalancer/LobbyBalancerRepository.java @@ -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 retrieveServerStatuses() { - Connection connection = null; ResultSet resultSet = null; PreparedStatement preparedStatement = null; List serverData = new ArrayList(); 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; diff --git a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/motd/MotdRepository.java b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/motd/MotdRepository.java index 79f311eb0..2bb1ccfff 100644 --- a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/motd/MotdRepository.java +++ b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/motd/MotdRepository.java @@ -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; diff --git a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerCount/PlayerCountRepository.java b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerCount/PlayerCountRepository.java index 8e7cc9588..758139243 100644 --- a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerCount/PlayerCountRepository.java +++ b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerCount/PlayerCountRepository.java @@ -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; diff --git a/Plugins/Mineplex.Core/src/mineplex/core/antihack/AntiHackRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/antihack/AntiHackRepository.java index 4cde80c7a..e273968ba 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/antihack/AntiHackRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/antihack/AntiHackRepository.java @@ -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(); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/status/ServerStatusManager.java b/Plugins/Mineplex.Core/src/mineplex/core/status/ServerStatusManager.java index 03a267f43..969f4e57b 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/status/ServerStatusManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/status/ServerStatusManager.java @@ -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 diff --git a/Plugins/Mineplex.Core/src/mineplex/core/status/ServerStatusRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/status/ServerStatusRepository.java index 922ae3241..aceaedf81 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/status/ServerStatusRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/status/ServerStatusRepository.java @@ -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 retrieveServerStatuses() { - Connection connection = null; ResultSet resultSet = null; PreparedStatement preparedStatement = null; List serverData = new ArrayList(); 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; diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/modules/MapManager.java b/Plugins/Mineplex.Hub/src/mineplex/hub/modules/MapManager.java index ca8037754..251b856d4 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/modules/MapManager.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/modules/MapManager.java @@ -18,7 +18,7 @@ public class MapManager extends MiniPlugin private HubManager Manager; private Map Map; - public MapManager(HubManager manager) + public MapManager(HubManager manager) { super("Map Manager", manager.GetPlugin()); Map = new Map(manager.GetPlugin());