I like these to be explictly closed rather than in a new method.

This commit is contained in:
t00thpick1 2014-07-27 14:56:34 -04:00
parent e6a7c8f5d2
commit ed82a838c8
2 changed files with 161 additions and 48 deletions

View File

@ -98,8 +98,22 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
}
finally {
tryClose(in);
tryClose(out);
if (in != null) {
try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
// Ignore
}
}
}
}
@ -164,8 +178,22 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
}
finally {
tryClose(in);
tryClose(out);
if (in != null) {
try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
// Ignore
}
}
}
}
@ -203,8 +231,22 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
}
finally {
tryClose(in);
tryClose(out);
if (in != null) {
try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
// Ignore
}
}
}
}
@ -294,8 +336,22 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
return false;
}
finally {
tryClose(in);
tryClose(out);
if (in != null) {
try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
// Ignore
}
}
}
}
}
@ -381,7 +437,14 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
e.printStackTrace();
}
finally {
tryClose(out);
if (out != null) {
try {
out.close();
}
catch (IOException e) {
// Ignore
}
}
}
}
}
@ -448,7 +511,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
in.close();
}
catch (IOException e) {
e.printStackTrace();
// Ignore
}
}
}
@ -491,7 +554,14 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
e.printStackTrace();
}
finally {
tryClose(in);
if (in != null) {
try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
}
}
}
@ -532,8 +602,22 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
}
finally {
tryClose(in);
tryClose(out);
if (in != null) {
try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
// Ignore
}
}
}
}
@ -573,8 +657,22 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
}
finally {
tryClose(in);
tryClose(out);
if (in != null) {
try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
// Ignore
}
}
}
}
@ -601,7 +699,14 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
e.printStackTrace();
}
finally {
tryClose(in);
if (in != null) {
try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
}
}
return users;
@ -671,7 +776,14 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " during user " + playerName + " (Are you sure you formatted it correctly?) " + e.toString());
}
finally {
tryClose(in);
if (in != null) {
try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
}
}
@ -897,8 +1009,22 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
}
finally {
tryClose(in);
tryClose(out);
if (in != null) {
try {
in.close();
}
catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
// Ignore
}
}
}
}
@ -923,18 +1049,6 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
}
}
private void tryClose(Closeable c) {
if (c == null) {
return;
}
try {
c.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
private Integer getPlayerRank(String playerName, List<PlayerStat> statsList) {
if (statsList == null) {
return null;

View File

@ -33,8 +33,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
private String connectionString;
private String tablePrefix = Config.getInstance().getMySQLTablePrefix();
// How long to wait when checking if connection is valid (default 3 seconds)
private final int VALID_TIMEOUT = 3;
private final int POOL_FETCH_TIMEOUT = 0; // How long a method will wait for a connection. Since none are on main thread, we can safely say wait for as long as you like.
private final Map<UUID, Integer> cachedUserIDs = new HashMap<UUID, Integer>();
private final Map<String, Integer> cachedUserIDsByName = new HashMap<String, Integer>();
@ -62,7 +61,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
connectionPool.registerShutdownHook(); // Auto release on jvm exit just in case
}
catch (ClassNotFoundException e) {
// TODO tft do something here
// TODO tft do something here everything will blow up
e.printStackTrace();
}
@ -79,7 +78,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
List<String> usernames = new ArrayList<String>();
try {
connection = connectionPool.getConnection(VALID_TIMEOUT);
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT u.user FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE s.user_id = u.id AND (s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0");
@ -142,7 +141,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
List<String> usernames = new ArrayList<String>();
try {
connection = connectionPool.getConnection(VALID_TIMEOUT);
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT user FROM " + tablePrefix + "users WHERE ((NOW() - lastlogin * " + Misc.TIME_CONVERSION_FACTOR + ") > " + PURGE_TIME + ")");
@ -202,7 +201,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
PreparedStatement statement = null;
try {
connection = connectionPool.getConnection(VALID_TIMEOUT);
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
statement = connection.prepareStatement("DELETE FROM u, e, h, s, c " +
"USING " + tablePrefix + "users u " +
"JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " +
@ -250,7 +249,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
Connection connection = null;
try {
connection = connectionPool.getConnection(VALID_TIMEOUT);
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
int id = getUserID(connection, profile.getUniqueId());
@ -366,7 +365,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
Connection connection = null;
try {
connection = connectionPool.getConnection(VALID_TIMEOUT);
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
statement = connection.prepareStatement("SELECT " + query + ", user, NOW() FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON (user_id = id) WHERE " + query + " > 0 ORDER BY " + query + " DESC, user LIMIT ?, ?");
statement.setInt(1, (pageNumber * statsPerPage) - statsPerPage);
statement.setInt(2, statsPerPage);
@ -423,7 +422,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
Connection connection = null;
try {
connection = connectionPool.getConnection(VALID_TIMEOUT);
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
for (SkillType skillType : SkillType.NON_CHILD_SKILLS) {
String skillName = skillType.name().toLowerCase();
String sql = "SELECT COUNT(*) AS rank FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE " + skillName + " > 0 " +
@ -534,7 +533,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
Connection connection = null;
try {
connection = connectionPool.getConnection(VALID_TIMEOUT);
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
newUser(connection, playerName, uuid);
}
catch (SQLException ex) {
@ -604,7 +603,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
ResultSet resultSet = null;
try {
connection = connectionPool.getConnection(VALID_TIMEOUT);
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
int id = getUserID(connection, playerName);
if (id == -1) {
@ -705,7 +704,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
ResultSet resultSet = null;
try {
connection = connectionPool.getConnection(VALID_TIMEOUT);
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
int id = getUserID(connection, playerName);
if (id == -1) {
@ -808,7 +807,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
ResultSet resultSet = null;
try {
connection = connectionPool.getConnection(VALID_TIMEOUT);
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
statement = connection.prepareStatement(
"SELECT "
+ "s.taming, s.mining, s.repair, s.woodcutting, s.unarmed, s.herbalism, s.excavation, s.archery, s.swords, s.axes, s.acrobatics, s.fishing, s.alchemy, "
@ -876,7 +875,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
Connection connection = null;
try {
connection = connectionPool.getConnection(VALID_TIMEOUT);
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
statement = connection.prepareStatement(
"UPDATE `" + tablePrefix + "users` SET "
+ " uuid = ? WHERE user = ?");
@ -916,7 +915,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
Connection connection = null;
try {
connection = connectionPool.getConnection(VALID_TIMEOUT);
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
statement = connection.prepareStatement("UPDATE " + tablePrefix + "users SET uuid = ? WHERE user = ?");
for (Map.Entry<String, UUID> entry : fetchedUUIDs.entrySet()) {
@ -971,7 +970,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
ResultSet resultSet = null;
try {
connection = connectionPool.getConnection(VALID_TIMEOUT);
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT user FROM " + tablePrefix + "users");
while (resultSet.next()) {
@ -1020,7 +1019,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
Connection connection = null;
try {
connection = connectionPool.getConnection(VALID_TIMEOUT);
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
statement = connection.createStatement();
statement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "users` ("