Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
Fix another memory leak
Browse files Browse the repository at this point in the history
Closes #90
  • Loading branch information
Flibio committed Aug 4, 2018
1 parent 8fb08fc commit 8b59e05
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 31 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repositories {

dependencies {
compile "org.spongepowered:spongeapi:${project.apiVersion}"
compile 'com.github.flibiostudio:utils:7233207'
compile 'com.github.flibiostudio:utils:164f3b6'
}

shadowJar {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ name=EconomyLite
owner=Flibio
inceptionYear=2015
currentYear=2018
version=2.14.3
version=2.14.4
apiVersion=7.1.0-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
import org.spongepowered.api.service.economy.account.VirtualAccount;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import javax.sql.DataSource;

public class PlayerServiceCommon implements PlayerEconService {

private SqlManager manager;
Expand All @@ -48,12 +52,9 @@ public PlayerServiceCommon(SqlManager manager, boolean h2) {

private void repair(boolean h2) {
if (h2) {
try {
ResultSet rs = manager.executeQuery("SELECT * FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE TABLE_NAME = 'ECONOMYLITEPLAYERS'").get();
rs.next();
rs.getString(1);
if (needRepair("SELECT * FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE TABLE_NAME = 'ECONOMYLITEPLAYERS'")) {
logger.info("Repairing the database...");
} catch (Exception e) {
} else {
logger.debug("Database repairs not necessary!");
return;
}
Expand All @@ -66,12 +67,9 @@ private void repair(boolean h2) {
logger.info("Repairs complete!");
return;
} else {
try {
ResultSet rs = manager.executeQuery("show index from economyliteplayers where Column_name='uuid'").get();
rs.next();
rs.getString(1);
if (needRepair("show index from economyliteplayers where Column_name='uuid'")) {
logger.info("Repairing the database...");
} catch (Exception e) {
} else {
logger.debug("Database repairs not necessary!");
return;
}
Expand All @@ -86,6 +84,29 @@ private void repair(boolean h2) {
}
}

/**
* Returns true if database needs to be repaired.
*/
private boolean needRepair(String sql) {
DataSource source = manager.getDataSource();
try {
Connection con = source.getConnection();
try {
PreparedStatement ps = con.prepareStatement(sql);
ps.closeOnCompletion();
ResultSet rs = ps.executeQuery();
rs.next();
rs.getString(1);
} finally {
con.close();
}
} catch (Exception e) {
return false;
}

return true;
}

public boolean isWorking() {
return manager.testConnection();
}
Expand Down Expand Up @@ -183,18 +204,29 @@ public boolean setBalanceAll(BigDecimal balance, Currency currency, Cause cause)

public List<String> getAccountsMigration() {
List<String> accounts = new ArrayList<>();
Optional<ResultSet> rOpt = manager.executeQuery("SELECT * FROM economyliteplayers");
if (rOpt.isPresent()) {
ResultSet rs = rOpt.get();
try {
Connection con = manager.getDataSource().getConnection();
try {
while (rs.next()) {
accounts.add(rs.getString("uuid") + "%-%" + rs.getDouble("balance") + "%-%" + rs.getString("currency"));
PreparedStatement ps = con.prepareStatement("SELECT * FROM economyliteplayers");
ps.closeOnCompletion();

ResultSet rs = ps.executeQuery();
try {
while (rs.next()) {
accounts.add(rs.getString("uuid") + "%-%" + rs.getDouble("balance") + "%-%" + rs.getString("currency"));
}
rs.close();
return accounts;
} catch (Exception e) {
logger.error(e.getMessage());
return accounts;
}
return accounts;
} catch (Exception e) {
logger.error(e.getMessage());
return accounts;

} finally {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return accounts;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
import org.spongepowered.api.service.economy.account.VirtualAccount;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import javax.sql.DataSource;

public class VirtualServiceCommon implements VirtualEconService {

private SqlManager manager;
Expand Down Expand Up @@ -53,12 +57,9 @@ public VirtualServiceCommon(SqlManager manager, boolean h2) {

private void repair(boolean h2) {
if (h2) {
try {
ResultSet rs = manager.executeQuery("SELECT * FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE TABLE_NAME = 'ECONOMYLITEVIRTS'").get();
rs.next();
rs.getString(1);
if (needRepair("SELECT * FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE TABLE_NAME = 'ECONOMYLITEVIRTS'")) {
logger.info("Repairing the database...");
} catch (Exception e) {
} else {
logger.debug("Database repairs not necessary!");
return;
}
Expand All @@ -71,12 +72,9 @@ private void repair(boolean h2) {
logger.info("Repairs complete!");
return;
} else {
try {
ResultSet rs = manager.executeQuery("show index from economylitevirts where Column_name='id'").get();
rs.next();
rs.getString(1);
if (needRepair("show index from economylitevirts where Column_name='id'")) {
logger.info("Repairing the database...");
} catch (Exception e) {
} else {
logger.debug("Database repairs not necessary!");
return;
}
Expand All @@ -91,6 +89,29 @@ private void repair(boolean h2) {
}
}

/**
* Returns true if database needs to be repaired.
*/
private boolean needRepair(String sql) {
DataSource source = manager.getDataSource();
try {
Connection con = source.getConnection();
try {
PreparedStatement ps = con.prepareStatement(sql);
ps.closeOnCompletion();
ResultSet rs = ps.executeQuery();
rs.next();
rs.getString(1);
} finally {
con.close();
}
} catch (Exception e) {
return false;
}

return true;
}

public boolean isWorking() {
return manager.testConnection();
}
Expand Down

0 comments on commit 8b59e05

Please sign in to comment.