Skip to content

Commit

Permalink
Merge pull request #2 from whimc/bug/reusing-connection
Browse files Browse the repository at this point in the history
Close MySQL Connection After Use
  • Loading branch information
jackah2 committed Jan 12, 2021
2 parents 9fdda6c + 9cd0d3c commit fed3b16
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 76 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>edu.whimc</groupId>
<artifactId>WHIMC-PositionTracker</artifactId>
<version>2.0.2.2</version>
<version>2.0.3</version>
<name>WHIMC Position Tracker</name>
<description>Track player positions to a database</description>

Expand Down
149 changes: 74 additions & 75 deletions src/main/java/edu/whimc/positiontracker/sql/Queryer.java
Expand Up @@ -16,79 +16,78 @@
import edu.whimc.positiontracker.Tracker;

public class Queryer {

public class PositionEntry {

private final int x, y, z;
private final String world, biome, username;
private final UUID uuid;
private final Timestamp time;

public PositionEntry(Player player) {
Location loc = player.getLocation();
this.x = loc.getBlockX();
this.y = loc.getBlockY();
this.z = loc.getBlockZ();
this.world = loc.getWorld().getName();
this.biome = loc.getBlock().getBiome().name();
this.username = player.getName();
this.uuid = player.getUniqueId();
this.time = new Timestamp(System.currentTimeMillis());
}

public void addInsertionToBatch(PreparedStatement statement) throws SQLException {
statement.setInt(1, x);
statement.setInt(2, y);
statement.setInt(3, z);
statement.setString(4, world);
statement.setString(5, biome);
statement.setString(6, username);
statement.setString(7, uuid.toString());
statement.setLong(8, time.getTime() / 1000);
statement.addBatch();
}

}

private static final String INSERT_FORMAT =
"INSERT INTO `whimc_player_positions` " +
"(x, y, z, world, biome, username, uuid, time) " +
"VALUES(?, ?, ?, ?, ?, ?, ?, ?)";

private Tracker plugin;
private MySQLConnection sqlConnection;

public Queryer(Tracker plugin, Consumer<Boolean> callback) {
this.plugin = plugin;
this.sqlConnection = new MySQLConnection(plugin);

Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
final boolean success = sqlConnection.initialize();
Bukkit.getScheduler().runTask(plugin, () -> {
callback.accept(success);
});
});
}

public void storePositionData() {
final List<PositionEntry> entries = Bukkit.getOnlinePlayers().stream()
.map(v -> new PositionEntry(v))
.collect(Collectors.toList());

Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
Connection connection = this.sqlConnection.getConnection();
try {
connection.setAutoCommit(false);
PreparedStatement statement = connection.prepareStatement(INSERT_FORMAT);
for (PositionEntry entry : entries) {
entry.addInsertionToBatch(statement);
}
statement.executeBatch();
connection.commit();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
});
}

public class PositionEntry {

private final int x, y, z;
private final String world, biome, username;
private final UUID uuid;
private final Timestamp time;

public PositionEntry(Player player) {
Location loc = player.getLocation();
this.x = loc.getBlockX();
this.y = loc.getBlockY();
this.z = loc.getBlockZ();
this.world = loc.getWorld().getName();
this.biome = loc.getBlock().getBiome().name();
this.username = player.getName();
this.uuid = player.getUniqueId();
this.time = new Timestamp(System.currentTimeMillis());
}

public void addInsertionToBatch(PreparedStatement statement) throws SQLException {
statement.setInt(1, x);
statement.setInt(2, y);
statement.setInt(3, z);
statement.setString(4, world);
statement.setString(5, biome);
statement.setString(6, username);
statement.setString(7, uuid.toString());
statement.setLong(8, time.getTime() / 1000);
statement.addBatch();
}

}

private static final String INSERT_FORMAT =
"INSERT INTO `whimc_player_positions` " +
"(x, y, z, world, biome, username, uuid, time) " +
"VALUES(?, ?, ?, ?, ?, ?, ?, ?)";

private Tracker plugin;
private MySQLConnection sqlConnection;

public Queryer(Tracker plugin, Consumer<Boolean> callback) {
this.plugin = plugin;
this.sqlConnection = new MySQLConnection(plugin);

Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
final boolean success = sqlConnection.initialize();
Bukkit.getScheduler().runTask(plugin, () -> {
callback.accept(success);
});
});
}

public void storePositionData() {
final List<PositionEntry> entries = Bukkit.getOnlinePlayers().stream()
.map(PositionEntry::new)
.collect(Collectors.toList());

Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
try (Connection connection = this.sqlConnection.getConnection()) {
connection.setAutoCommit(false);
try (PreparedStatement statement = connection.prepareStatement(INSERT_FORMAT)) {
for (PositionEntry entry : entries) {
entry.addInsertionToBatch(statement);
}
statement.executeBatch();
connection.commit();
}
} catch (SQLException e) {
e.printStackTrace();
}
});
}
}

0 comments on commit fed3b16

Please sign in to comment.