-
Notifications
You must be signed in to change notification settings - Fork 0
v5 Database
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.
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.
try (Connection conn = db.getConnection();
PreparedStatement ps = conn.prepareStatement("SELECT 1")) {
ps.execute();
}getConnection() is @NotNull. It throws rather than returning null:
-
RuntimeExceptionwrapping theSQLExceptionif the pool cannot hand out a connection -
IllegalStateExceptionif called aftershutdown()
⚠️ Migrating from v4:getConnection()used to catch theSQLException, callonExceptionCaught(...)and returnnull. Everyif (conn == null)branch in your code is now dead, and the exception escapes instead. Update those call sites.
db.shutdown();Closes the pool and releases the datasource. After this, getConnection() throws
IllegalStateException.
onConnected(), onConnectionFailed() and onExceptionCaught(Exception) are final on Database and
route into the info / warn methods you implement.
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.
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.
Setup
Spigot
Text
Data
Migration
Other versions