Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.simplyvanilla.simplyrank.addresswhitelist.AddressWhitelistService;
Expand Down Expand Up @@ -46,6 +47,8 @@ public class SimplyRankPlugin extends JavaPlugin {

private MySqlClient mySqlClient = null;

private ScheduledTask cleanerTask;


@Override
public void onEnable() {
Expand Down Expand Up @@ -93,7 +96,7 @@ public void onEnable() {
this.playerDataService =
new PlayerDataService(mySqlRepository, mySqlRepository);
proxyService = new ProxyService(mySqlRepository, new ProxyCheckProvider(this.getConfig().getString("proxycheck-api-url", "https://proxycheck.io/v2/%s&vpn=1")));
Bukkit.getAsyncScheduler().runAtFixedRate(this, new ProxyTtlCleanupTask(proxyService, this.getConfig().getInt("proxycache-ttl", 720)), 1, 10, TimeUnit.SECONDS);
this.cleanerTask = Bukkit.getAsyncScheduler().runAtFixedRate(this, new ProxyTtlCleanupTask(proxyService, this.getConfig().getInt("proxycache-ttl", 720)), 1, 10, TimeUnit.SECONDS);

if (!this.playerDataService.groupExists("default")) {
GroupData defaultData = new GroupData(NamedTextColor.GRAY, "Member ");
Expand Down Expand Up @@ -156,6 +159,7 @@ public void onEnable() {
@Override
public void onDisable() {
instance = null;
if (this.cleanerTask != null) this.cleanerTask.cancel();
if (this.mySqlClient != null) {
this.mySqlClient.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public class MySqlClient {
public static final String TABLE_PLAYERS_NAME = "player";
public static final String TABLE_GROUPS_NAME = "group";

private static final int MAX_TRIES = 10;
private static final long WAIT_TIME = 3000;

private final String url;
private final String username;
private final String password;
Expand All @@ -24,6 +27,27 @@ public MySqlClient(String url, String user, String password) {
initTables();
}

public void testConnection() {
for (int i = 0; i < MAX_TRIES; i++) {
if (testConnectionSingle()) return;
try {
Thread.sleep(WAIT_TIME);
} catch (InterruptedException ignored) {
break;
}
}
throw new RuntimeException(String.format("It was not possible to recover Database connection after %s tries", MAX_TRIES));
}

private boolean testConnectionSingle() {
try {
connection = DriverManager.getConnection(url, username, password);
return connection.isValid(1);
} catch (SQLException e) {
return false;
}
}

public void connect() {
try {
connection = DriverManager.getConnection(url, username, password);
Expand All @@ -41,11 +65,13 @@ public void close() {
}

public void update(PreparedStatement statement) throws SQLException {
testConnection();
statement.executeUpdate();
statement.close();
}

public ResultSet query(PreparedStatement statement) throws SQLException {
testConnection();
ResultSet rs;
rs = statement.executeQuery();

Expand All @@ -55,6 +81,7 @@ public ResultSet query(PreparedStatement statement) throws SQLException {
}

private void executeRawStatement(String cmd) throws SQLException {
testConnection();
try (Statement st = connection.createStatement()) {
st.execute(cmd);
}
Expand Down Expand Up @@ -127,6 +154,7 @@ PRIMARY KEY (`id`),
}

public PreparedStatement prepareStatement(String qry) throws SQLException {
testConnection();
return connection.prepareStatement(qry);
}

Expand Down