4545import java .net .URL ;
4646import java .net .URLClassLoader ;
4747import 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 .*;
5250import java .util .concurrent .CompletionException ;
5351
5452public 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 );
0 commit comments