Skip to content

Commit

Permalink
improve sql connect: fail completely when connection to mysql fails
Browse files Browse the repository at this point in the history
  • Loading branch information
weaondara committed Jul 24, 2019
1 parent 668cd3e commit 7c6f1ce
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/main/java/net/alpenblock/bungeeperms/Mysql.java
Expand Up @@ -48,16 +48,14 @@ public void connect()
String url = "jdbc:mysql://" + config.getString(configsection + ".general.mysqlhost", "localhost") + ":" + config.getString(configsection + ".general.mysqlport", "3306") + "/" + config.getString(configsection + ".general.mysqldb", "database") + "?autoReconnect=true&dontTrackOpenResources=true";
this.connection = DriverManager.getConnection(url, config.getString(configsection + ".general.mysqluser", configsection), config.getString(configsection + ".general.mysqlpw", "password"));
}
catch (SQLException e) {
if (e.getCause() != null && e.getCause().getMessage().startsWith("Access denied for user")) {
BungeePerms.getInstance().getPlugin().getLogger().severe("Failed to connect to database: " + e.getMessage());
} else {
debug.log(e);
}
}
catch (Exception e)
{
debug.log(e);
RuntimeException t;
if (e.getCause() != null && e.getCause().getMessage().startsWith("Access denied for user"))
t = new RuntimeException("Failed to connect to database: " + e.getCause().getMessage());
else
t = new RuntimeException(e);
throw t;
}
}

Expand Down Expand Up @@ -106,7 +104,7 @@ else if (rs.next())
}
return connected;
}

@SneakyThrows
public PreparedStatement stmt(String template)
{
Expand Down Expand Up @@ -188,7 +186,7 @@ public boolean addColumn(String table, String column, String type, String after,
stmt = stmt("ALTER TABLE `" + table + "` ADD COLUMN `" + column + "` " + type + " AFTER `" + after + "`");
runQuery(stmt);
stmt.close();

checkConnection();
stmt = stmt("UPDATE " + table + " SET " + column + "=?");
stmt.setString(1, value);
Expand Down Expand Up @@ -321,7 +319,7 @@ private void reconnect()
close();
connect();
}

//maybe for later use
// //transaction stuff
// private ReentrantLock transactionlock = new ReentrantLock();
Expand Down

0 comments on commit 7c6f1ce

Please sign in to comment.