diff --git a/LaunchServer/source/auth/MySQLSourceConfig.java b/LaunchServer/source/auth/MySQLSourceConfig.java index 930bde3..6b435df 100644 --- a/LaunchServer/source/auth/MySQLSourceConfig.java +++ b/LaunchServer/source/auth/MySQLSourceConfig.java @@ -17,6 +17,9 @@ import launcher.serialize.config.entry.StringConfigEntry; public final class MySQLSourceConfig extends ConfigObject implements Flushable { + @LauncherAPI public static final int TIMEOUT = VerifyHelper.verifyInt( + Integer.parseUnsignedInt(System.getProperty("launcher.mysql.timeout", Integer.toString(10))), + VerifyHelper.POSITIVE, "launcher.mysql.timeout can't be <= 0"); private static final int MAX_POOL_SIZE = VerifyHelper.verifyInt( Integer.parseUnsignedInt(System.getProperty("launcher.mysql.maxPoolSize", Integer.toString(10))), VerifyHelper.POSITIVE, "launcher.mysql.maxPoolSize can't be <= 0"); @@ -67,8 +70,12 @@ if (source == null) { // New data source MysqlDataSource mysqlSource = new MysqlDataSource(); mysqlSource.setUseUnicode(true); - mysqlSource.setLoginTimeout(IOHelper.TIMEOUT); mysqlSource.setCachePrepStmts(true); + + // Set timeouts and cache + mysqlSource.setEnableQueryTimeouts(true); + mysqlSource.setLoginTimeout(TIMEOUT); + mysqlSource.setConnectTimeout(TIMEOUT * 1000); mysqlSource.setPrepStmtCacheSize(STMT_CACHE_SIZE); mysqlSource.setPrepStmtCacheSqlLimit(IOHelper.BUFFER_SIZE); @@ -91,7 +98,7 @@ // Set pool settings hikariSource.setPoolName(poolName); - hikariSource.setMaximumPoolSize(MAX_POOL_SIZE); + hikariSource.setMaximumPoolSize(TIMEOUT); // Replace source with hds source = hikariSource; diff --git a/LaunchServer/source/auth/handler/FileAuthHandler.java b/LaunchServer/source/auth/handler/FileAuthHandler.java index 6b3ee84..36d0b80 100644 --- a/LaunchServer/source/auth/handler/FileAuthHandler.java +++ b/LaunchServer/source/auth/handler/FileAuthHandler.java @@ -45,7 +45,7 @@ // Read auth handler file if (IOHelper.isFile(file)) { - LogHelper.info("Reading auth handler file"); + LogHelper.info("Reading auth handler file: '%s'", file); try { readAuthFile(); } catch (IOException e) { diff --git a/LaunchServer/source/auth/handler/MySQLAuthHandler.java b/LaunchServer/source/auth/handler/MySQLAuthHandler.java index 36c4bcb..67d12ca 100644 --- a/LaunchServer/source/auth/handler/MySQLAuthHandler.java +++ b/LaunchServer/source/auth/handler/MySQLAuthHandler.java @@ -5,6 +5,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; import java.util.UUID; import launcher.helper.LogHelper; @@ -59,7 +60,8 @@ LogHelper.info("Fetching all AuthHandler entries"); String query = String.format("SELECT %s, %s, %s, %s FROM %s", uuidColumn, usernameColumn, accessTokenColumn, serverIDColumn, table); - try (Connection c = mySQLHolder.getConnection(); ResultSet set = c.createStatement().executeQuery(query)) { + try (Connection c = mySQLHolder.getConnection(); Statement statement = c.createStatement(); + ResultSet set = statement.executeQuery(query)) { for (Entry entry = constructEntry(set); entry != null; entry = constructEntry(set)) { addEntry(entry); } @@ -86,11 +88,13 @@ @Override protected boolean updateAuth(UUID uuid, String username, String accessToken) throws IOException { - try (Connection c = mySQLHolder.getConnection(); - PreparedStatement s = c.prepareStatement(updateAuthSQL)) { + try (Connection c = mySQLHolder.getConnection(); PreparedStatement s = c.prepareStatement(updateAuthSQL)) { s.setString(1, username); // Username case s.setString(2, accessToken); s.setString(3, uuid.toString()); + + // Execute update + s.setQueryTimeout(MySQLSourceConfig.TIMEOUT); return s.executeUpdate() > 0; } catch (SQLException e) { throw new IOException(e); @@ -99,10 +103,12 @@ @Override protected boolean updateServerID(UUID uuid, String serverID) throws IOException { - try (Connection c = mySQLHolder.getConnection(); - PreparedStatement s = c.prepareStatement(updateServerIDSQL)) { + try (Connection c = mySQLHolder.getConnection(); PreparedStatement s = c.prepareStatement(updateServerIDSQL)) { s.setString(1, serverID); s.setString(2, uuid.toString()); + + // Execute update + s.setQueryTimeout(MySQLSourceConfig.TIMEOUT); return s.executeUpdate() > 0; } catch (SQLException e) { throw new IOException(e); @@ -117,6 +123,9 @@ private Entry query(String sql, String value) throws IOException { try (Connection c = mySQLHolder.getConnection(); PreparedStatement s = c.prepareStatement(sql)) { s.setString(1, value); + + // Execute query + s.setQueryTimeout(MySQLSourceConfig.TIMEOUT); try (ResultSet set = s.executeQuery()) { return constructEntry(set); } diff --git a/LaunchServer/source/auth/provider/MySQLAuthProvider.java b/LaunchServer/source/auth/provider/MySQLAuthProvider.java index d39d51b..f1cba03 100644 --- a/LaunchServer/source/auth/provider/MySQLAuthProvider.java +++ b/LaunchServer/source/auth/provider/MySQLAuthProvider.java @@ -31,17 +31,16 @@ @Override public String auth(String login, String password) throws SQLException, AuthException { - try (Connection c = mySQLHolder.getConnection()) { - try (PreparedStatement statement = c.prepareStatement(query)) { - String[] replaceParams = { "login", login, "password", password }; - for (int i = 0; i < queryParams.length; i++) { - statement.setString(i + 1, CommonHelper.replace(queryParams[i], replaceParams)); - } + try (Connection c = mySQLHolder.getConnection(); PreparedStatement s = c.prepareStatement(query)) { + String[] replaceParams = { "login", login, "password", password }; + for (int i = 0; i < queryParams.length; i++) { + s.setString(i + 1, CommonHelper.replace(queryParams[i], replaceParams)); + } - // Execute SQL query - try (ResultSet set = statement.executeQuery()) { - return set.next() ? set.getString(1) : authError("Incorrect username or password"); - } + // Execute SQL query + s.setQueryTimeout(MySQLSourceConfig.TIMEOUT); + try (ResultSet set = s.executeQuery()) { + return set.next() ? set.getString(1) : authError("Incorrect username or password"); } } }