ensure that we always use the shaded JDBC driver

This commit is contained in:
Luck 2018-01-21 22:20:00 +00:00
parent 8d045be0b0
commit 40294b10f5
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
7 changed files with 93 additions and 90 deletions

View File

@ -147,6 +147,20 @@
<version>2.7.3</version> <version>2.7.3</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version>
<scope>provided</scope>
</dependency>
<!-- sqlite -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.21.0</version>
<scope>provided</scope>
</dependency>
<!-- Jedis --> <!-- Jedis -->
<dependency> <dependency>
<groupId>redis.clients</groupId> <groupId>redis.clients</groupId>

View File

@ -28,62 +28,41 @@ package me.lucko.luckperms.common.storage.dao.sql.connection.file;
import me.lucko.luckperms.common.storage.dao.sql.connection.AbstractConnectionFactory; import me.lucko.luckperms.common.storage.dao.sql.connection.AbstractConnectionFactory;
import java.io.File; import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.concurrent.locks.ReentrantLock; import java.util.LinkedHashMap;
import java.util.Map;
abstract class FlatfileConnectionFactory extends AbstractConnectionFactory { abstract class FlatfileConnectionFactory extends AbstractConnectionFactory {
protected static final DecimalFormat DF = new DecimalFormat("#.##"); protected static final DecimalFormat DF = new DecimalFormat("#.##");
protected final File file; protected final File file;
private final ReentrantLock lock = new ReentrantLock();
private NonClosableConnection connection;
FlatfileConnectionFactory(String name, File file) { FlatfileConnectionFactory(String name, File file) {
super(name); super(name);
this.file = file; this.file = file;
} }
protected abstract String getDriverClass();
protected abstract String getDriverId();
@Override @Override
public void init() { public void init() {
} }
@Override protected File getWriteFile() {
public void shutdown() throws Exception { return this.file;
if (this.connection != null) {
this.connection.shutdown();
}
} }
@Override @Override
public Connection getConnection() throws SQLException { public Map<String, String> getMeta() {
this.lock.lock(); Map<String, String> ret = new LinkedHashMap<>();
try {
if (this.connection == null || this.connection.isClosed()) {
try {
Class.forName(getDriverClass());
} catch (ClassNotFoundException ignored) {}
Connection connection = DriverManager.getConnection(getDriverId() + ":" + this.file.getAbsolutePath()); File databaseFile = getWriteFile();
if (connection != null) { if (databaseFile.exists()) {
this.connection = new NonClosableConnection(connection); double size = databaseFile.length() / 1048576D;
} ret.put("File Size", DF.format(size) + "MB");
} } else {
ret.put("File Size", "0MB");
} finally {
this.lock.unlock();
} }
if (this.connection == null) { return ret;
throw new SQLException("Unable to get a connection.");
}
return this.connection;
} }
} }

View File

@ -25,43 +25,59 @@
package me.lucko.luckperms.common.storage.dao.sql.connection.file; package me.lucko.luckperms.common.storage.dao.sql.connection.file;
import org.h2.Driver;
import java.io.File; import java.io.File;
import java.util.LinkedHashMap; import java.sql.Connection;
import java.util.Map; import java.sql.SQLException;
import java.util.Properties;
import java.util.concurrent.locks.ReentrantLock;
public class H2ConnectionFactory extends FlatfileConnectionFactory { public class H2ConnectionFactory extends FlatfileConnectionFactory {
private final ReentrantLock lock = new ReentrantLock();
private NonClosableConnection connection;
public H2ConnectionFactory(File file) { public H2ConnectionFactory(File file) {
super("H2", file); super("H2", file);
// backwards compat // backwards compat
File data = new File(file.getParent(), "luckperms.db.mv.db"); File data = new File(file.getParent(), "luckperms.db.mv.db");
if (data.exists()) { if (data.exists()) {
data.renameTo(new File(file.getParent(), "luckperms-h2.mv.db")); data.renameTo(new File(file.getParent(), file.getName() + ".mv.db"));
} }
} }
@Override @Override
public Map<String, String> getMeta() { public Connection getConnection() throws SQLException {
Map<String, String> ret = new LinkedHashMap<>(); this.lock.lock();
try {
File databaseFile = new File(super.file.getParent(), "luckperms-h2.mv.db"); if (this.connection == null || this.connection.isClosed()) {
if (databaseFile.exists()) { Connection connection = Driver.load().connect("jdbc:h2:" + this.file.getAbsolutePath(), new Properties());
double size = databaseFile.length() / 1048576D; if (connection != null) {
ret.put("File Size", DF.format(size) + "MB"); this.connection = new NonClosableConnection(connection);
} else { }
ret.put("File Size", "0MB"); }
} finally {
this.lock.unlock();
} }
return ret; if (this.connection == null) {
throw new SQLException("Unable to get a connection.");
}
return this.connection;
} }
@Override @Override
protected String getDriverClass() { public void shutdown() throws Exception {
return "org.h2.Driver"; if (this.connection != null) {
this.connection.shutdown();
}
} }
@Override @Override
protected String getDriverId() { protected File getWriteFile() {
return "jdbc:h2"; // h2 appends this to the end of the database file
return new File(super.file.getParent(), super.file.getName() + ".mv.db");
} }
} }

View File

@ -25,43 +25,55 @@
package me.lucko.luckperms.common.storage.dao.sql.connection.file; package me.lucko.luckperms.common.storage.dao.sql.connection.file;
import org.sqlite.JDBC;
import java.io.File; import java.io.File;
import java.util.LinkedHashMap; import java.sql.Connection;
import java.util.Map; import java.sql.SQLException;
import java.util.Properties;
import java.util.concurrent.locks.ReentrantLock;
public class SQLiteConnectionFactory extends FlatfileConnectionFactory { public class SQLiteConnectionFactory extends FlatfileConnectionFactory {
private final ReentrantLock lock = new ReentrantLock();
private NonClosableConnection connection;
public SQLiteConnectionFactory(File file) { public SQLiteConnectionFactory(File file) {
super("SQLite", file); super("SQLite", file);
// backwards compat // backwards compat
File data = new File(file.getParent(), "luckperms.sqlite"); File data = new File(file.getParent(), "luckperms.sqlite");
if (data.exists()) { if (data.exists()) {
data.renameTo(new File(file.getParent(), "luckperms-sqlite.db")); data.renameTo(file);
} }
} }
@Override @Override
public Map<String, String> getMeta() { public Connection getConnection() throws SQLException {
Map<String, String> ret = new LinkedHashMap<>(); this.lock.lock();
try {
if (this.connection == null || this.connection.isClosed()) {
Connection connection = JDBC.createConnection("jdbc:sqlite:" + this.file.getAbsolutePath(), new Properties());
if (connection != null) {
this.connection = new NonClosableConnection(connection);
}
}
File databaseFile = new File(super.file.getParent(), "luckperms-sqlite.db"); } finally {
if (databaseFile.exists()) { this.lock.unlock();
double size = databaseFile.length() / 1048576D;
ret.put("File Size", DF.format(size) + "MB");
} else {
ret.put("File Size", "0MB");
} }
return ret; if (this.connection == null) {
throw new SQLException("Unable to get a connection.");
}
return this.connection;
} }
@Override @Override
protected String getDriverClass() { public void shutdown() throws Exception {
return "org.sqlite.JDBC"; if (this.connection != null) {
this.connection.shutdown();
}
} }
@Override
protected String getDriverId() {
return "jdbc:sqlite";
}
} }

View File

@ -100,7 +100,6 @@ public abstract class HikariConnectionFactory extends AbstractConnectionFactory
config.setInitializationFailFast(false); config.setInitializationFailFast(false);
} }
this.hikari = new HikariDataSource(config); this.hikari = new HikariDataSource(config);
} }

View File

@ -40,7 +40,7 @@ public class MariaDbConnectionFactory extends HikariConnectionFactory {
@Override @Override
protected String getDriverClass() { protected String getDriverClass() {
return classExists("org.mariadb.jdbc.MariaDbDataSource") ? "org.mariadb.jdbc.MariaDbDataSource" : "org.mariadb.jdbc.MySQLDataSource"; return "org.mariadb.jdbc.MariaDbDataSource";
} }
@Override @Override
@ -57,13 +57,4 @@ public class MariaDbConnectionFactory extends HikariConnectionFactory {
config.addDataSourceProperty("properties", propertiesString); config.addDataSourceProperty("properties", propertiesString);
} }
private static boolean classExists(String clazz) {
try {
Class.forName(clazz);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
} }

View File

@ -35,16 +35,8 @@ public class MySqlConnectionFactory extends HikariConnectionFactory {
} }
@Override @Override
protected void appendConfigurationInfo(HikariConfig config) { protected String getDriverClass() {
String address = this.configuration.getAddress(); return "com.mysql.jdbc.jdbc2.optional.MysqlDataSource";
String[] addressSplit = address.split(":");
address = addressSplit[0];
String port = addressSplit.length > 1 ? addressSplit[1] : "3306";
String database = this.configuration.getDatabase();
config.setJdbcUrl("jdbc:mysql://" + address + ":" + port + "/" + database);
config.setUsername(this.configuration.getUsername());
config.setPassword(this.configuration.getPassword());
} }
@Override @Override