Skip to content

Commit

Permalink
Add a db pool in a slightly elegant way without having to modify the …
Browse files Browse the repository at this point in the history
…existing code base TOO much!:

Database pools required you to get a Connection object and then close it afterwards. LWC has 2000+ lines of database code. Modifying every instance of this is extremely (!!) time consuming and very inconvenient.

Instead of doing this we exploit the fact that in LWC's usecase, once a PreparedStatement or ResultSet is closed, everything above it is closed, e.g a close chain: ResultSet -> PreparedStatement -> Connection. This is done automatically by the new backing AutoClosingPreparedStatement and AutoClosingResultSet.

This however did require manually modifying anything that used raw Statement objects.

This means that this build truly is a "dev" build not an unreleased stable build. Care should be taken with this build.
  • Loading branch information
Hidendra committed Nov 18, 2012
1 parent cf5b1c9 commit cec9f85
Show file tree
Hide file tree
Showing 13 changed files with 1,359 additions and 454 deletions.
3 changes: 3 additions & 0 deletions build.xml
Expand Up @@ -65,6 +65,9 @@
<mkdir dir="${bin}/core/"/>
<mkdir dir="${bin}/core/resources/"/>

<!-- shade dbpool -->
<unzip src="lib/dbpool-5.0.jar" dest="${bin}/core/" />

<!-- Timestamp, bitches -->
<tstamp>
<!-- Date format: MONTH DD YYYY e.g January 25, 2012 -->
Expand Down
Binary file added lib/dbpool-5.0.jar
Binary file not shown.
5 changes: 4 additions & 1 deletion src/main/java/com/griefcraft/io/BackupManager.java
Expand Up @@ -40,6 +40,7 @@

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -250,7 +251,8 @@ public void run() {
database.load();

// TODO separate stream logic to somewhere else :)
Statement resultStatement = database.getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
Connection connection = database.createConnection();
Statement resultStatement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

if (lwc.getPhysicalDatabase().getType() == Database.Type.MySQL) {
resultStatement.setFetchSize(Integer.MIN_VALUE);
Expand Down Expand Up @@ -324,6 +326,7 @@ public Void call() throws Exception {
// close the sql statements
result.close();
resultStatement.close();
connection.close();

// close the backup file
backup.close();
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/griefcraft/lwc/LWC.java
Expand Up @@ -129,6 +129,7 @@
import org.bukkit.plugin.Plugin;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Expand Down Expand Up @@ -962,7 +963,8 @@ public int fastRemoveProtections(CommandSender sender, String where, boolean sho
sender.sendMessage("Loading protections via STREAM mode");

try {
Statement resultStatement = physicalDatabase.getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
Connection connection = physicalDatabase.createConnection();
Statement resultStatement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

if (physicalDatabase.getType() == Database.Type.MySQL) {
resultStatement.setFetchSize(Integer.MIN_VALUE);
Expand Down Expand Up @@ -1007,6 +1009,7 @@ public int fastRemoveProtections(CommandSender sender, String where, boolean sho
// Close the streaming statement
result.close();
resultStatement.close();
connection.close();

// flush all of the queries
fullRemoveProtections(sender, toRemove);
Expand Down Expand Up @@ -1039,7 +1042,8 @@ private void fullRemoveProtections(CommandSender sender, List<Integer> toRemove)
String prefix = getPhysicalDatabase().getPrefix();

// create the statement to use
Statement statement = getPhysicalDatabase().getConnection().createStatement();
Connection connection = getPhysicalDatabase().createConnection();
Statement statement = connection.createStatement();

while (iter.hasNext()) {
int protectionId = iter.next();
Expand All @@ -1063,6 +1067,7 @@ private void fullRemoveProtections(CommandSender sender, List<Integer> toRemove)
}

statement.close();
connection.close();
}

/**
Expand Down Expand Up @@ -1420,7 +1425,7 @@ public void load() {

physicalDatabase.load();

log("Using database: " + StringUtil.capitalizeFirstLetter(physicalDatabase.getConnection().getMetaData().getDriverVersion()));
log("Using database: " + physicalDatabase.getType().toString());
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/com/griefcraft/migration/DatabaseMigrator.java
Expand Up @@ -47,8 +47,6 @@ public class DatabaseMigrator {
*/
public boolean migrate(PhysDB fromDatabase, PhysDB toDatabase) {
try {
toDatabase.getConnection().setAutoCommit(false);

// some prelim data
int startProtections = toDatabase.getProtectionCount();
int protectionCount = fromDatabase.getProtectionCount();
Expand All @@ -63,7 +61,6 @@ public boolean migrate(PhysDB fromDatabase, PhysDB toDatabase) {
protection.saveNow();
}

toDatabase.getConnection().commit();
if (expectedProtections != (protectionCount = fromDatabase.getProtectionCount())) {
logger.info("Weird, only " + protectionCount + " protections are in the database? Continuing...");
}
Expand All @@ -80,9 +77,6 @@ public boolean migrate(PhysDB fromDatabase, PhysDB toDatabase) {
history.sync();
}
}

fromDatabase.getConnection().close();
toDatabase.getConnection().setAutoCommit(true);
} catch (Exception e) {
e.printStackTrace();
return false;
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/griefcraft/modules/admin/AdminCleanup.java
Expand Up @@ -40,6 +40,7 @@
import org.bukkit.command.CommandSender;
import org.bukkit.scheduler.BukkitScheduler;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Expand Down Expand Up @@ -124,7 +125,8 @@ public void push(List<Integer> toRemove) throws SQLException {
String prefix = lwc.getPhysicalDatabase().getPrefix();

// create the statement to use
Statement statement = lwc.getPhysicalDatabase().getConnection().createStatement();
Connection connection = lwc.getPhysicalDatabase().createConnection();
Statement statement = connection.createStatement();

while (iter.hasNext()) {
int protectionId = iter.next();
Expand All @@ -147,6 +149,7 @@ public void push(List<Integer> toRemove) throws SQLException {
}

statement.close();
connection.close();
}

public void run() {
Expand All @@ -173,7 +176,8 @@ public void run() {
database.connect();
database.load();

Statement resultStatement = database.getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
Connection connection = database.createConnection();
Statement resultStatement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

if (lwc.getPhysicalDatabase().getType() == Database.Type.MySQL) {
resultStatement.setFetchSize(Integer.MIN_VALUE);
Expand Down Expand Up @@ -240,6 +244,7 @@ public Void call() throws Exception {
// close the sql statements
result.close();
resultStatement.close();
connection.close();

// flush all of the queries
push(toRemove);
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/com/griefcraft/modules/admin/AdminQuery.java
Expand Up @@ -37,6 +37,7 @@
import com.griefcraft.util.StringUtil;
import org.bukkit.command.CommandSender;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Expand Down Expand Up @@ -68,14 +69,8 @@ public void onCommand(LWCCommandEvent event) {
if (args[0].equals("query")) {
String query = StringUtil.join(args, 1);

try {
Statement statement = lwc.getPhysicalDatabase().getConnection().createStatement();
statement.executeUpdate(query);
statement.close();
sender.sendMessage(Colors.Green + "Done.");
} catch (SQLException e) {
sender.sendMessage(Colors.Red + "Err: " + e.getMessage());
}
lwc.getPhysicalDatabase().executeUpdateNoException(query);
sender.sendMessage(Colors.Green + "Done.");
}

// Specific query, but the where statement is given
Expand All @@ -92,7 +87,8 @@ public void onCommand(LWCCommandEvent event) {
// execute the query
try {
PhysDB database = lwc.getPhysicalDatabase();
Statement statement = database.getConnection().createStatement();
Connection connection = database.createConnection();
Statement statement = connection.createStatement();

// choose the statement
if (args[0].startsWith("update")) {
Expand Down

0 comments on commit cec9f85

Please sign in to comment.