Skip to content

Commit 471183b

Browse files
committed
Add some SQL-Storage settings and tweak some connection-pool settings
1 parent e314d65 commit 471183b

File tree

4 files changed

+59
-27
lines changed

4 files changed

+59
-27
lines changed

BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/config/storage/SQLConfig.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.net.MalformedURLException;
3333
import java.net.URL;
3434
import java.nio.file.Paths;
35+
import java.util.HashMap;
36+
import java.util.Map;
3537
import java.util.Optional;
3638

3739
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
@@ -42,10 +44,14 @@ public class SQLConfig extends StorageConfig implements SQLStorageSettings {
4244
@DebugDump private String driverClass = null;
4345
private String connectionUrl = "jdbc:mysql://localhost/bluemap?permitMysqlScheme";
4446

47+
private Map<String, String> connectionProperties = new HashMap<>();
48+
4549
@DebugDump private Compression compression = Compression.GZIP;
4650

4751
@DebugDump private transient URL driverJarURL = null;
4852

53+
@DebugDump private int maxConnections = -1;
54+
4955
@Override
5056
public Optional<URL> getDriverJar() throws MalformedURLException {
5157
if (driverJar == null) return Optional.empty();
@@ -67,6 +73,16 @@ public String getConnectionUrl() {
6773
return connectionUrl;
6874
}
6975

76+
@Override
77+
public Map<String, String> getConnectionProperties() {
78+
return connectionProperties;
79+
}
80+
81+
@Override
82+
public int getMaxConnections() {
83+
return maxConnections;
84+
}
85+
7086
@Override
7187
public Compression getCompression() {
7288
return compression;

BlueMapCommon/src/main/resources/de/bluecolored/bluemap/config/storages/sql.conf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,23 @@ storage-type: SQL
1111
# The JDBC-Connection URL that is used to connect to the database.
1212
# The format for this url is usually something like: jdbc:[driver]://[host]:[port]/[database]?user=[user]&password=[password]
1313
# The exact format of the url is determined by the JDBC-Driver you are using.
14+
# (You might need to URL-encode the user and password if it has special characters)
1415
connection-url: "jdbc:mysql://localhost/bluemap?permitMysqlScheme"
1516
#connection-url: "jdbc:mariadb://localhost/bluemap?user=root"
1617
#connection-url: "jdbc:mysql://localhost:3306/bluemap?user=root&password=password"
1718

19+
# You can set any additional (JDBC-Driver-specific) properties here
20+
# (if you have user/password in your connection-url, you don't need them here, and vice versa)
21+
connection-properties: {
22+
#user: "root",
23+
#password: ""
24+
}
25+
26+
# The maximum number of connections to the database that are allowed to be open at the same time.
27+
# A negative number means unlimited.
28+
# Default is: -1
29+
max-connections: -1
30+
1831
# This can be used to load a custom jdbc-driver from a .jar file.
1932
# E.g. if your runtime-environment is not already providing the sql-driver you need,
2033
# you could download the MariaDB JDBC-Connector from https://mariadb.com/downloads/connectors/connectors-data-access/java8-connector/

BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/SQLStorage.java

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@
4545
import java.net.URL;
4646
import java.net.URLClassLoader;
4747
import java.sql.*;
48-
import java.util.NoSuchElementException;
49-
import java.util.Objects;
50-
import java.util.Optional;
51-
import java.util.Properties;
48+
import java.time.Duration;
49+
import java.util.*;
5250
import java.util.concurrent.CompletionException;
5351

5452
public class SQLStorage extends Storage {
@@ -81,13 +79,13 @@ public SQLStorage(SQLStorageSettings config) throws MalformedURLException, SQLDr
8179
"Instead you'll need to add your driver-jar to the classpath when starting your server," +
8280
"e.g. using the '-classpath' command-line argument", ex);*/
8381
}
84-
this.dataSource = createDataSource(config.getConnectionUrl(), driver);
82+
this.dataSource = createDataSource(config.getConnectionUrl(), config.getConnectionProperties(), config.getMaxConnections(), driver);
8583
} else {
8684
Class.forName(config.getDriverClass().get());
87-
this.dataSource = createDataSource(config.getConnectionUrl());
85+
this.dataSource = createDataSource(config.getConnectionUrl(), config.getConnectionProperties(), config.getMaxConnections());
8886
}
8987
} else {
90-
this.dataSource = createDataSource(config.getConnectionUrl());
88+
this.dataSource = createDataSource(config.getConnectionUrl(), config.getConnectionProperties(), config.getMaxConnections());
9189
}
9290
} catch (ClassNotFoundException ex) {
9391
throw new SQLDriverException("The driver-class does not exist.", ex);
@@ -98,16 +96,6 @@ public SQLStorage(SQLStorageSettings config) throws MalformedURLException, SQLDr
9896
this.hiresCompression = config.getCompression();
9997
}
10098

101-
public SQLStorage(String dbUrl, Compression compression) {
102-
this.dataSource = createDataSource(dbUrl);
103-
this.hiresCompression = compression;
104-
}
105-
106-
public SQLStorage(DataSource dataSource, Compression compression) {
107-
this.dataSource = dataSource;
108-
this.hiresCompression = compression;
109-
}
110-
11199
@Override
112100
public OutputStream writeMapTile(String mapId, int lod, Vector2i tile) throws IOException {
113101
Compression compression = lod == 0 ? this.hiresCompression : Compression.NONE;
@@ -532,9 +520,8 @@ private <R> R recoveringConnection(ConnectionFunction<R> action, int tries) thro
532520
try {
533521
for (int i = 0; i < tries; i++) {
534522
try (Connection connection = dataSource.getConnection()) {
535-
R result;
536523
try {
537-
result = action.apply(connection);
524+
R result = action.apply(connection);
538525
connection.commit();
539526
return result;
540527
} catch (SQLRecoverableException ex) {
@@ -621,21 +608,27 @@ private int lookupFK(String table, String idField, String valueField, String val
621608
}, 2);
622609
}
623610

624-
private DataSource createDataSource(String dbUrl) {
625-
return createDataSource(new DriverManagerConnectionFactory(dbUrl));
611+
private DataSource createDataSource(String dbUrl, Map<String, String> properties, int maxPoolSize) {
612+
Properties props = new Properties();
613+
props.putAll(properties);
614+
615+
return createDataSource(new DriverManagerConnectionFactory(dbUrl, props), maxPoolSize);
626616
}
627617

628-
private DataSource createDataSource(String dbUrl, Driver driver) {
618+
private DataSource createDataSource(String dbUrl, Map<String, String> properties, int maxPoolSize, Driver driver) {
619+
Properties props = new Properties();
620+
props.putAll(properties);
621+
629622
ConnectionFactory connectionFactory = new DriverConnectionFactory(
630623
driver,
631624
dbUrl,
632-
new Properties()
625+
props
633626
);
634627

635-
return createDataSource(connectionFactory);
628+
return createDataSource(connectionFactory, maxPoolSize);
636629
}
637630

638-
private DataSource createDataSource(ConnectionFactory connectionFactory) {
631+
private DataSource createDataSource(ConnectionFactory connectionFactory, int maxPoolSize) {
639632
PoolableConnectionFactory poolableConnectionFactory =
640633
new PoolableConnectionFactory(() -> {
641634
Logger.global.logDebug("Creating new SQL-Connection...");
@@ -649,9 +642,14 @@ private DataSource createDataSource(ConnectionFactory connectionFactory) {
649642
poolableConnectionFactory.setFastFailValidation(true);
650643

651644
GenericObjectPoolConfig<PoolableConnection> objectPoolConfig = new GenericObjectPoolConfig<>();
645+
objectPoolConfig.setTestWhileIdle(true);
646+
objectPoolConfig.setTimeBetweenEvictionRuns(Duration.ofSeconds(10));
647+
objectPoolConfig.setNumTestsPerEvictionRun(3);
648+
objectPoolConfig.setBlockWhenExhausted(true);
652649
objectPoolConfig.setMinIdle(1);
653-
objectPoolConfig.setMaxIdle(Runtime.getRuntime().availableProcessors() * 2);
654-
objectPoolConfig.setMaxTotal(-1);
650+
objectPoolConfig.setMaxIdle(Runtime.getRuntime().availableProcessors());
651+
objectPoolConfig.setMaxTotal(maxPoolSize);
652+
objectPoolConfig.setMaxWaitMillis(Duration.ofSeconds(30).toMillis());
655653

656654
ObjectPool<PoolableConnection> connectionPool =
657655
new GenericObjectPool<>(poolableConnectionFactory, objectPoolConfig);

BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/SQLStorageSettings.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.net.MalformedURLException;
66
import java.net.URL;
7+
import java.util.Map;
78
import java.util.Optional;
89

910
public interface SQLStorageSettings {
@@ -14,6 +15,10 @@ public interface SQLStorageSettings {
1415

1516
String getConnectionUrl();
1617

18+
Map<String, String> getConnectionProperties();
19+
20+
int getMaxConnections();
21+
1722
Compression getCompression();
1823

1924
}

0 commit comments

Comments
 (0)