Skip to content

v5 Database

Jake Moore edited this page Aug 30, 2026 · 3 revisions

Database

⚠️ Usage ⚠️

Available in shared-jar and its inheritors (standalone-jar, spigot-jar). Requires Java 11. See Java Versions.

Database is a thin MySQL wrapper over a HikariCP connection pool. It is the only part of KamiCommon that needs more than Java 8, because HikariCP is the only bundled library above Java 8 and this is the only class that touches it.

Creating one

Database is abstract: you supply logging.

public class MyDatabase extends Database {
    private static final java.util.logging.Logger LOG = java.util.logging.Logger.getLogger("MyDatabase");

    public MyDatabase() {
        super("mysql.internal", 3306, "myplugin", "user", "password");
    }

    @Override public void info(String msg) { LOG.info(msg); }
    @Override public void warn(String msg) { LOG.warning(msg); }
}

Database lives in shared-jar, which has no Bukkit on its classpath, so bring your own logger. The two abstract methods are the only ones you must implement.

The constructor connects immediately and calls onConnected(). On Java 8, 9 or 10 it throws IllegalStateException naming your Java version.

Using it

try (Connection conn = db.getConnection();
     PreparedStatement ps = conn.prepareStatement("SELECT 1")) {
    ps.execute();
}

getConnection() is @NotNull. It throws rather than returning null:

  • RuntimeException wrapping the SQLException if the pool cannot hand out a connection
  • IllegalStateException if called after shutdown()

⚠️ Migrating from v4: getConnection() used to catch the SQLException, call onExceptionCaught(...) and return null. Every if (conn == null) branch in your code is now dead, and the exception escapes instead. Update those call sites.

Shutting down

db.shutdown();

Closes the pool and releases the datasource. After this, getConnection() throws IllegalStateException.

Callbacks

onConnected(), onConnectionFailed() and onExceptionCaught(Exception) are final on Database and route into the info / warn methods you implement.

The driver is detected, not assumed

Database tries the relocated driver first and falls back to the plain one:

com.kamikazejam.kamicommon.mysql.cj.jdbc.MysqlDataSource   (inside spigot-jar)
com.mysql.cj.jdbc.MysqlDataSource                          (everywhere else)

So it works whether or not you are running the shaded spigot-jar, and whether or not you relocated com.mysql yourself.

TLS

KamiCommon does not configure TLS. Your driver's default applies, and mysql-connector-j 26 defaults sslMode to PREFERRED. If you need a specific mode, set it on the server or supply your own datasource.

If you are migrating from v4 and believed KamiCommon disabled SSL, it did not. The property that appeared to do so was applied after the datasource had already been built, so it never took effect. Nothing about your connections changes.

Clone this wiki locally