mirror of
https://github.com/BlueMap-Minecraft/BlueMap.git
synced 2024-09-28 15:17:34 +02:00
Add some SQL-Storage settings and tweak some connection-pool settings
This commit is contained in:
parent
e314d6597e
commit
471183b419
@ -32,6 +32,8 @@
|
|||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
|
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
|
||||||
@ -42,10 +44,14 @@ public class SQLConfig extends StorageConfig implements SQLStorageSettings {
|
|||||||
@DebugDump private String driverClass = null;
|
@DebugDump private String driverClass = null;
|
||||||
private String connectionUrl = "jdbc:mysql://localhost/bluemap?permitMysqlScheme";
|
private String connectionUrl = "jdbc:mysql://localhost/bluemap?permitMysqlScheme";
|
||||||
|
|
||||||
|
private Map<String, String> connectionProperties = new HashMap<>();
|
||||||
|
|
||||||
@DebugDump private Compression compression = Compression.GZIP;
|
@DebugDump private Compression compression = Compression.GZIP;
|
||||||
|
|
||||||
@DebugDump private transient URL driverJarURL = null;
|
@DebugDump private transient URL driverJarURL = null;
|
||||||
|
|
||||||
|
@DebugDump private int maxConnections = -1;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<URL> getDriverJar() throws MalformedURLException {
|
public Optional<URL> getDriverJar() throws MalformedURLException {
|
||||||
if (driverJar == null) return Optional.empty();
|
if (driverJar == null) return Optional.empty();
|
||||||
@ -67,6 +73,16 @@ public String getConnectionUrl() {
|
|||||||
return connectionUrl;
|
return connectionUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, String> getConnectionProperties() {
|
||||||
|
return connectionProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMaxConnections() {
|
||||||
|
return maxConnections;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Compression getCompression() {
|
public Compression getCompression() {
|
||||||
return compression;
|
return compression;
|
||||||
|
@ -11,10 +11,23 @@ storage-type: SQL
|
|||||||
# The JDBC-Connection URL that is used to connect to the database.
|
# The JDBC-Connection URL that is used to connect to the database.
|
||||||
# The format for this url is usually something like: jdbc:[driver]://[host]:[port]/[database]?user=[user]&password=[password]
|
# The format for this url is usually something like: jdbc:[driver]://[host]:[port]/[database]?user=[user]&password=[password]
|
||||||
# The exact format of the url is determined by the JDBC-Driver you are using.
|
# The exact format of the url is determined by the JDBC-Driver you are using.
|
||||||
|
# (You might need to URL-encode the user and password if it has special characters)
|
||||||
connection-url: "jdbc:mysql://localhost/bluemap?permitMysqlScheme"
|
connection-url: "jdbc:mysql://localhost/bluemap?permitMysqlScheme"
|
||||||
#connection-url: "jdbc:mariadb://localhost/bluemap?user=root"
|
#connection-url: "jdbc:mariadb://localhost/bluemap?user=root"
|
||||||
#connection-url: "jdbc:mysql://localhost:3306/bluemap?user=root&password=password"
|
#connection-url: "jdbc:mysql://localhost:3306/bluemap?user=root&password=password"
|
||||||
|
|
||||||
|
# You can set any additional (JDBC-Driver-specific) properties here
|
||||||
|
# (if you have user/password in your connection-url, you don't need them here, and vice versa)
|
||||||
|
connection-properties: {
|
||||||
|
#user: "root",
|
||||||
|
#password: ""
|
||||||
|
}
|
||||||
|
|
||||||
|
# The maximum number of connections to the database that are allowed to be open at the same time.
|
||||||
|
# A negative number means unlimited.
|
||||||
|
# Default is: -1
|
||||||
|
max-connections: -1
|
||||||
|
|
||||||
# This can be used to load a custom jdbc-driver from a .jar file.
|
# This can be used to load a custom jdbc-driver from a .jar file.
|
||||||
# E.g. if your runtime-environment is not already providing the sql-driver you need,
|
# E.g. if your runtime-environment is not already providing the sql-driver you need,
|
||||||
# you could download the MariaDB JDBC-Connector from https://mariadb.com/downloads/connectors/connectors-data-access/java8-connector/
|
# you could download the MariaDB JDBC-Connector from https://mariadb.com/downloads/connectors/connectors-data-access/java8-connector/
|
||||||
|
@ -45,10 +45,8 @@
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.NoSuchElementException;
|
import java.time.Duration;
|
||||||
import java.util.Objects;
|
import java.util.*;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Properties;
|
|
||||||
import java.util.concurrent.CompletionException;
|
import java.util.concurrent.CompletionException;
|
||||||
|
|
||||||
public class SQLStorage extends Storage {
|
public class SQLStorage extends Storage {
|
||||||
@ -81,13 +79,13 @@ public SQLStorage(SQLStorageSettings config) throws MalformedURLException, SQLDr
|
|||||||
"Instead you'll need to add your driver-jar to the classpath when starting your server," +
|
"Instead you'll need to add your driver-jar to the classpath when starting your server," +
|
||||||
"e.g. using the '-classpath' command-line argument", ex);*/
|
"e.g. using the '-classpath' command-line argument", ex);*/
|
||||||
}
|
}
|
||||||
this.dataSource = createDataSource(config.getConnectionUrl(), driver);
|
this.dataSource = createDataSource(config.getConnectionUrl(), config.getConnectionProperties(), config.getMaxConnections(), driver);
|
||||||
} else {
|
} else {
|
||||||
Class.forName(config.getDriverClass().get());
|
Class.forName(config.getDriverClass().get());
|
||||||
this.dataSource = createDataSource(config.getConnectionUrl());
|
this.dataSource = createDataSource(config.getConnectionUrl(), config.getConnectionProperties(), config.getMaxConnections());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.dataSource = createDataSource(config.getConnectionUrl());
|
this.dataSource = createDataSource(config.getConnectionUrl(), config.getConnectionProperties(), config.getMaxConnections());
|
||||||
}
|
}
|
||||||
} catch (ClassNotFoundException ex) {
|
} catch (ClassNotFoundException ex) {
|
||||||
throw new SQLDriverException("The driver-class does not exist.", ex);
|
throw new SQLDriverException("The driver-class does not exist.", ex);
|
||||||
@ -98,16 +96,6 @@ public SQLStorage(SQLStorageSettings config) throws MalformedURLException, SQLDr
|
|||||||
this.hiresCompression = config.getCompression();
|
this.hiresCompression = config.getCompression();
|
||||||
}
|
}
|
||||||
|
|
||||||
public SQLStorage(String dbUrl, Compression compression) {
|
|
||||||
this.dataSource = createDataSource(dbUrl);
|
|
||||||
this.hiresCompression = compression;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SQLStorage(DataSource dataSource, Compression compression) {
|
|
||||||
this.dataSource = dataSource;
|
|
||||||
this.hiresCompression = compression;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public OutputStream writeMapTile(String mapId, int lod, Vector2i tile) throws IOException {
|
public OutputStream writeMapTile(String mapId, int lod, Vector2i tile) throws IOException {
|
||||||
Compression compression = lod == 0 ? this.hiresCompression : Compression.NONE;
|
Compression compression = lod == 0 ? this.hiresCompression : Compression.NONE;
|
||||||
@ -532,9 +520,8 @@ private <R> R recoveringConnection(ConnectionFunction<R> action, int tries) thro
|
|||||||
try {
|
try {
|
||||||
for (int i = 0; i < tries; i++) {
|
for (int i = 0; i < tries; i++) {
|
||||||
try (Connection connection = dataSource.getConnection()) {
|
try (Connection connection = dataSource.getConnection()) {
|
||||||
R result;
|
|
||||||
try {
|
try {
|
||||||
result = action.apply(connection);
|
R result = action.apply(connection);
|
||||||
connection.commit();
|
connection.commit();
|
||||||
return result;
|
return result;
|
||||||
} catch (SQLRecoverableException ex) {
|
} catch (SQLRecoverableException ex) {
|
||||||
@ -621,21 +608,27 @@ private int lookupFK(String table, String idField, String valueField, String val
|
|||||||
}, 2);
|
}, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
private DataSource createDataSource(String dbUrl) {
|
private DataSource createDataSource(String dbUrl, Map<String, String> properties, int maxPoolSize) {
|
||||||
return createDataSource(new DriverManagerConnectionFactory(dbUrl));
|
Properties props = new Properties();
|
||||||
|
props.putAll(properties);
|
||||||
|
|
||||||
|
return createDataSource(new DriverManagerConnectionFactory(dbUrl, props), maxPoolSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
private DataSource createDataSource(String dbUrl, Driver driver) {
|
private DataSource createDataSource(String dbUrl, Map<String, String> properties, int maxPoolSize, Driver driver) {
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.putAll(properties);
|
||||||
|
|
||||||
ConnectionFactory connectionFactory = new DriverConnectionFactory(
|
ConnectionFactory connectionFactory = new DriverConnectionFactory(
|
||||||
driver,
|
driver,
|
||||||
dbUrl,
|
dbUrl,
|
||||||
new Properties()
|
props
|
||||||
);
|
);
|
||||||
|
|
||||||
return createDataSource(connectionFactory);
|
return createDataSource(connectionFactory, maxPoolSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
private DataSource createDataSource(ConnectionFactory connectionFactory) {
|
private DataSource createDataSource(ConnectionFactory connectionFactory, int maxPoolSize) {
|
||||||
PoolableConnectionFactory poolableConnectionFactory =
|
PoolableConnectionFactory poolableConnectionFactory =
|
||||||
new PoolableConnectionFactory(() -> {
|
new PoolableConnectionFactory(() -> {
|
||||||
Logger.global.logDebug("Creating new SQL-Connection...");
|
Logger.global.logDebug("Creating new SQL-Connection...");
|
||||||
@ -649,9 +642,14 @@ private DataSource createDataSource(ConnectionFactory connectionFactory) {
|
|||||||
poolableConnectionFactory.setFastFailValidation(true);
|
poolableConnectionFactory.setFastFailValidation(true);
|
||||||
|
|
||||||
GenericObjectPoolConfig<PoolableConnection> objectPoolConfig = new GenericObjectPoolConfig<>();
|
GenericObjectPoolConfig<PoolableConnection> objectPoolConfig = new GenericObjectPoolConfig<>();
|
||||||
|
objectPoolConfig.setTestWhileIdle(true);
|
||||||
|
objectPoolConfig.setTimeBetweenEvictionRuns(Duration.ofSeconds(10));
|
||||||
|
objectPoolConfig.setNumTestsPerEvictionRun(3);
|
||||||
|
objectPoolConfig.setBlockWhenExhausted(true);
|
||||||
objectPoolConfig.setMinIdle(1);
|
objectPoolConfig.setMinIdle(1);
|
||||||
objectPoolConfig.setMaxIdle(Runtime.getRuntime().availableProcessors() * 2);
|
objectPoolConfig.setMaxIdle(Runtime.getRuntime().availableProcessors());
|
||||||
objectPoolConfig.setMaxTotal(-1);
|
objectPoolConfig.setMaxTotal(maxPoolSize);
|
||||||
|
objectPoolConfig.setMaxWaitMillis(Duration.ofSeconds(30).toMillis());
|
||||||
|
|
||||||
ObjectPool<PoolableConnection> connectionPool =
|
ObjectPool<PoolableConnection> connectionPool =
|
||||||
new GenericObjectPool<>(poolableConnectionFactory, objectPoolConfig);
|
new GenericObjectPool<>(poolableConnectionFactory, objectPoolConfig);
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface SQLStorageSettings {
|
public interface SQLStorageSettings {
|
||||||
@ -14,6 +15,10 @@ public interface SQLStorageSettings {
|
|||||||
|
|
||||||
String getConnectionUrl();
|
String getConnectionUrl();
|
||||||
|
|
||||||
|
Map<String, String> getConnectionProperties();
|
||||||
|
|
||||||
|
int getMaxConnections();
|
||||||
|
|
||||||
Compression getCompression();
|
Compression getCompression();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user