Vacuum SQLite database on server stop

Closes #132
This commit is contained in:
Eric 2017-08-15 11:55:06 +02:00
parent a8f589c1b7
commit 8d459220d3
3 changed files with 44 additions and 5 deletions

View File

@ -209,6 +209,10 @@ public class ShopChest extends JavaPlugin {
debug("Removed shop (#" + shop.getID() + ")");
}
if (database instanceof SQLite) {
((SQLite) database).vacuum(false);
}
database.disconnect();
}

View File

@ -537,14 +537,14 @@ public abstract class Database {
}
/**
* Closes a {@link PreparedStatement} and a {@link ResultSet}
* @param ps {@link PreparedStatement} to close
* Closes a {@link Statement} and a {@link ResultSet}
* @param s {@link Statement} to close
* @param rs {@link ResultSet} to close
*/
private void close(PreparedStatement ps, ResultSet rs) {
void close(Statement s, ResultSet rs) {
try {
if (ps != null)
ps.close();
if (s != null)
s.close();
if (rs != null)
rs.close();
} catch (SQLException ex) {

View File

@ -1,12 +1,15 @@
package de.epiceric.shopchest.sql;
import de.epiceric.shopchest.ShopChest;
import org.bukkit.scheduler.BukkitRunnable;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class SQLite extends Database {
@ -46,4 +49,36 @@ public class SQLite extends Database {
return null;
}
/**
* Vacuums the database to reduce file size
* @param async Whether the call should be executed asynchronously
*/
public void vacuum(boolean async) {
BukkitRunnable runnable = new BukkitRunnable() {
@Override
public void run() {
Statement s = null;
try {
s = connection.createStatement();
s.executeUpdate("VACUUM");
plugin.debug("Vacuumed SQLite database");
} catch (final SQLException ex) {
plugin.getLogger().severe("Failed to access database");
plugin.debug("Failed to vacuum database");
plugin.debug(ex);
} finally {
close(s, null);
}
}
};
if (async) {
runnable.runTaskAsynchronously(plugin);
} else {
runnable.run();
}
}
}