Permalink
Browse files

Add a db pool in a slightly elegant way without having to modify the …

…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...
1 parent cf5b1c9 commit cec9f858d42c34fd634723426293987963cd69ee @Hidendra committed Nov 18, 2012
View
@@ -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 -->
View
Binary file not shown.
@@ -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;
@@ -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);
@@ -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();
@@ -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;
@@ -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);
@@ -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);
@@ -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();
@@ -1063,6 +1067,7 @@ private void fullRemoveProtections(CommandSender sender, List<Integer> toRemove)
}
statement.close();
+ connection.close();
}
/**
@@ -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();
}
@@ -47,8 +47,6 @@
*/
public boolean migrate(PhysDB fromDatabase, PhysDB toDatabase) {
try {
- toDatabase.getConnection().setAutoCommit(false);
-
// some prelim data
int startProtections = toDatabase.getProtectionCount();
int protectionCount = fromDatabase.getProtectionCount();
@@ -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...");
}
@@ -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;
@@ -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;
@@ -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();
@@ -147,6 +149,7 @@ public void push(List<Integer> toRemove) throws SQLException {
}
statement.close();
+ connection.close();
}
public void run() {
@@ -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);
@@ -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);
@@ -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;
@@ -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
@@ -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")) {
Oops, something went wrong.

0 comments on commit cec9f85

Please sign in to comment.