Skip to content

Commit 39737a3

Browse files
author
Anton Bashirov
authored
Merge pull request #263 from art-community/feature/sql-pools
Feature/sql pools
2 parents ec646c0 + 1f001e2 commit 39737a3

File tree

7 files changed

+129
-24
lines changed

7 files changed

+129
-24
lines changed

application-core/src/main/java/ru/art/core/lazy/LazyLoadingValue.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
package ru.art.core.lazy;
22

3-
import lombok.AllArgsConstructor;
4-
import lombok.RequiredArgsConstructor;
5-
import static java.util.Objects.nonNull;
6-
import java.util.concurrent.atomic.AtomicReference;
7-
import java.util.concurrent.locks.ReentrantLock;
8-
import java.util.function.Supplier;
3+
import lombok.*;
4+
import static java.util.Objects.*;
5+
import java.util.concurrent.atomic.*;
6+
import java.util.concurrent.locks.*;
7+
import java.util.function.*;
98

109
@RequiredArgsConstructor
11-
@AllArgsConstructor
1210
public class LazyLoadingValue<T> {
1311
private final ReentrantLock lock = new ReentrantLock();
14-
private final Supplier<T> loader;
15-
private boolean safe = false;
1612
private final AtomicReference<T> value = new AtomicReference<>();
13+
private final Supplier<T> loader;
1714

1815
public T value() {
1916
T value;

application-sql/src/main/java/ru/art/sql/configuration/SqlModuleConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import ru.art.core.module.*;
2828
import ru.art.sql.constants.*;
2929
import static ru.art.core.constants.StringConstants.*;
30+
import static ru.art.sql.constants.ConnectionPoolInitializationMode.*;
3031
import static ru.art.sql.constants.ConnectionPoolType.*;
3132
import static ru.art.sql.constants.DbProvider.*;
3233
import static ru.art.sql.factory.SqlConnectionPoolsFactory.*;
@@ -52,6 +53,8 @@ public interface SqlModuleConfiguration extends ModuleConfiguration {
5253

5354
boolean isEnableMetrics();
5455

56+
ConnectionPoolInitializationMode getConnectionPoolInitializationMode();
57+
5558
SqlModuleDefaultConfiguration DEFAULT_CONFIGURATION = new SqlModuleDefaultConfiguration();
5659

5760
@Getter
@@ -68,5 +71,6 @@ class SqlModuleDefaultConfiguration implements SqlModuleConfiguration {
6871
private final String jdbcPassword = EMPTY_STRING;
6972
private final DbProvider dbProvider = POSTGRES;
7073
private final boolean enableMetrics = true;
74+
private final ConnectionPoolInitializationMode connectionPoolInitializationMode = ON_MODULE_LOAD;
7175
}
7276
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* ART Java
3+
*
4+
* Copyright 2019 ART
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package ru.art.sql.constants;
20+
21+
public enum ConnectionPoolInitializationMode {
22+
ON_MODULE_LOAD,
23+
MANUAL
24+
}

application-sql/src/main/java/ru/art/sql/constants/SqlModuleConstants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ public interface SqlModuleConstants {
2424
interface ConfigurationDefaults {
2525
String DEFAULT_CONNECTION_TEST_QUERY = "select 1 from dual";
2626
}
27+
28+
interface LoggingMessages {
29+
String CLOSING_POOL = "Closing DB connection pool: {0}";
30+
String STARING_POOL = "Starting connection pool to DataSource: {0}";
31+
}
2732
}

application-sql/src/main/java/ru/art/sql/factory/SqlConnectionPoolsFactory.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,30 @@ static PoolProperties createTomcatPoolConfig(DbConnectionProperties properties)
7878
poolProperties.setDriverClassName(properties.getDriver().getDriverClassName());
7979
poolProperties.setUsername(properties.getLogin());
8080
poolProperties.setPassword(properties.getPassword());
81+
8182
poolProperties.setJmxEnabled(true);
83+
8284
poolProperties.setTestWhileIdle(false);
85+
poolProperties.setTestOnReturn(false);
8386
poolProperties.setTestOnBorrow(true);
87+
8488
poolProperties.setValidationQuery(DEFAULT_CONNECTION_TEST_QUERY);
85-
poolProperties.setTestOnReturn(false);
8689
poolProperties.setValidationInterval(30000);
87-
poolProperties.setTimeBetweenEvictionRunsMillis(30000);
88-
poolProperties.setMaxActive(100);
90+
8991
poolProperties.setInitialSize(10);
90-
poolProperties.setMaxWait(10000);
91-
poolProperties.setMinEvictableIdleTimeMillis(30000);
9292
poolProperties.setMinIdle(10);
93+
poolProperties.setMaxActive(100);
94+
poolProperties.setMaxIdle(100);
95+
96+
poolProperties.setTimeBetweenEvictionRunsMillis(30000);
97+
poolProperties.setMinEvictableIdleTimeMillis(30000);
98+
99+
poolProperties.setMaxWait(10000);
100+
93101
poolProperties.setLogAbandoned(true);
94102
poolProperties.setRemoveAbandoned(true);
95103
poolProperties.setRemoveAbandonedTimeout(60);
104+
96105
return poolProperties;
97106
}
98107

application-sql/src/main/java/ru/art/sql/module/SqlModule.java

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,40 @@
1818

1919
package ru.art.sql.module;
2020

21+
import com.zaxxer.hikari.*;
2122
import io.dropwizard.db.*;
2223
import lombok.*;
24+
import org.apache.logging.log4j.*;
2325
import ru.art.core.module.Module;
24-
import ru.art.core.module.*;
2526
import ru.art.sql.configuration.*;
2627
import ru.art.sql.exception.*;
28+
import ru.art.sql.state.*;
2729
import static java.lang.Class.*;
30+
import static java.text.MessageFormat.*;
2831
import static lombok.AccessLevel.*;
2932
import static ru.art.core.context.Context.*;
33+
import static ru.art.core.extension.NullCheckingExtensions.*;
34+
import static ru.art.core.wrapper.ExceptionWrapper.*;
35+
import static ru.art.logging.LoggingModule.*;
3036
import static ru.art.metrics.module.MetricsModule.*;
3137
import static ru.art.sql.configuration.SqlModuleConfiguration.*;
38+
import static ru.art.sql.constants.ConnectionPoolInitializationMode.*;
39+
import static ru.art.sql.constants.SqlModuleConstants.LoggingMessages.*;
3240
import static ru.art.sql.constants.SqlModuleConstants.*;
3341
import javax.sql.*;
42+
import java.util.function.*;
3443

3544
@Getter
36-
public class SqlModule implements Module<SqlModuleConfiguration, ModuleState> {
45+
public class SqlModule implements Module<SqlModuleConfiguration, SqlModuleState> {
3746
@Getter(lazy = true, value = PRIVATE)
3847
private static final SqlModuleConfiguration sqlModule = context().getModule(SQL_MODULE_ID, SqlModule::new);
48+
@Getter(lazy = true, value = PRIVATE)
49+
private static final SqlModuleState sqlModuleState = context().getModuleState(SQL_MODULE_ID, SqlModule::new);
3950
private final String id = SQL_MODULE_ID;
4051
private final SqlModuleConfiguration defaultConfiguration = DEFAULT_CONFIGURATION;
52+
private final SqlModuleState state = new SqlModuleState();
53+
@Getter(lazy = true, value = PRIVATE)
54+
private final Logger logger = loggingModule().getLogger(SqlModule.class);
4155

4256
public static SqlModuleConfiguration sqlModule() {
4357
if (contextIsNotReady()) {
@@ -46,23 +60,60 @@ public static SqlModuleConfiguration sqlModule() {
4660
return getSqlModule();
4761
}
4862

63+
public static SqlModuleState sqlModuleState() {
64+
return getSqlModuleState();
65+
}
66+
4967
@Override
5068
public void onLoad() {
5169
DataSource dataSource = null;
52-
switch (sqlModule().getConnectionPoolType()) {
53-
case HIKARI:
54-
dataSource = sqlModule().getHikariPoolConfig().getDataSource();
55-
break;
56-
case TOMCAT:
57-
dataSource = new ManagedPooledDataSource(sqlModule().getTomcatPoolConfig(), metricsModule().getDropwizardMetricRegistry());
58-
break;
59-
}
6070
try {
6171
forName(sqlModule().getDbProvider().getDriverClassName());
72+
switch (sqlModule().getConnectionPoolType()) {
73+
case HIKARI:
74+
HikariDataSource hikariDataSource = new HikariDataSource(sqlModule().getHikariPoolConfig());
75+
dataSource = hikariDataSource;
76+
sqlModuleState().hikariDataSource(hikariDataSource);
77+
if (sqlModule().getConnectionPoolInitializationMode() == ON_MODULE_LOAD) {
78+
hikariDataSource.getConnection();
79+
getLogger().info(format(STARING_POOL, hikariDataSource));
80+
}
81+
break;
82+
case TOMCAT:
83+
ManagedPooledDataSource tomcatDataSource = new ManagedPooledDataSource(sqlModule().getTomcatPoolConfig(), metricsModule().getDropwizardMetricRegistry());
84+
sqlModuleState().tomcatDataSource(tomcatDataSource);
85+
dataSource = tomcatDataSource;
86+
if (sqlModule().getConnectionPoolInitializationMode() == ON_MODULE_LOAD) {
87+
tomcatDataSource.start();
88+
getLogger().info(format(STARING_POOL, tomcatDataSource));
89+
}
90+
break;
91+
}
6292
sqlModule().getJooqConfiguration().set(dataSource).set(sqlModule().getJooqSettings());
6393
} catch (Exception throwable) {
6494
throw new SqlModuleException(throwable);
6595
}
96+
}
6697

98+
@Override
99+
public void onUnload() {
100+
try {
101+
switch (sqlModule().getConnectionPoolType()) {
102+
case HIKARI:
103+
doIfNotNull(sqlModuleState().hikariDataSource(), (Consumer<HikariDataSource>) pool -> ignoreException(() -> {
104+
pool.close();
105+
getLogger().info(format(CLOSING_POOL, pool));
106+
}, getLogger()::error));
107+
break;
108+
case TOMCAT:
109+
doIfNotNull(sqlModuleState().tomcatDataSource(), (Consumer<ManagedPooledDataSource>) pool -> ignoreException(() -> {
110+
pool.close();
111+
getLogger().info(format(CLOSING_POOL, pool));
112+
}, getLogger()::error));
113+
break;
114+
}
115+
} catch (Throwable throwable) {
116+
getLogger().error(throwable.getMessage(), throwable);
117+
}
67118
}
68119
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ru.art.sql.state;
2+
3+
import com.zaxxer.hikari.*;
4+
import io.dropwizard.db.*;
5+
import lombok.*;
6+
import lombok.experimental.*;
7+
import ru.art.core.module.*;
8+
9+
@Getter
10+
@Setter
11+
@Accessors(fluent = true)
12+
public class SqlModuleState implements ModuleState {
13+
private volatile HikariDataSource hikariDataSource;
14+
private volatile ManagedPooledDataSource tomcatDataSource;
15+
}

0 commit comments

Comments
 (0)