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

View File

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

View File

@ -40,7 +40,7 @@ public class MariaDbConnectionFactory extends HikariConnectionFactory {
@Override
protected String getDriverClass() {
return classExists("org.mariadb.jdbc.MariaDbDataSource") ? "org.mariadb.jdbc.MariaDbDataSource" : "org.mariadb.jdbc.MySQLDataSource";
return "org.mariadb.jdbc.MariaDbDataSource";
}
@Override
@ -57,13 +57,4 @@ public class MariaDbConnectionFactory extends HikariConnectionFactory {
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
protected void appendConfigurationInfo(HikariConfig config) {
String address = this.configuration.getAddress();
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());
protected String getDriverClass() {
return "com.mysql.jdbc.jdbc2.optional.MysqlDataSource";
}
@Override