Skip to content

Commit

Permalink
Merge pull request #263 from art-community/feature/sql-pools
Browse files Browse the repository at this point in the history
Feature/sql pools
  • Loading branch information
Anton Bashirov authored Mar 25, 2020
2 parents ec646c0 + 1f001e2 commit 39737a3
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package ru.art.core.lazy;

import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import static java.util.Objects.nonNull;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;
import lombok.*;
import static java.util.Objects.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import java.util.function.*;

@RequiredArgsConstructor
@AllArgsConstructor
public class LazyLoadingValue<T> {
private final ReentrantLock lock = new ReentrantLock();
private final Supplier<T> loader;
private boolean safe = false;
private final AtomicReference<T> value = new AtomicReference<>();
private final Supplier<T> loader;

public T value() {
T value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import ru.art.core.module.*;
import ru.art.sql.constants.*;
import static ru.art.core.constants.StringConstants.*;
import static ru.art.sql.constants.ConnectionPoolInitializationMode.*;
import static ru.art.sql.constants.ConnectionPoolType.*;
import static ru.art.sql.constants.DbProvider.*;
import static ru.art.sql.factory.SqlConnectionPoolsFactory.*;
Expand All @@ -52,6 +53,8 @@ public interface SqlModuleConfiguration extends ModuleConfiguration {

boolean isEnableMetrics();

ConnectionPoolInitializationMode getConnectionPoolInitializationMode();

SqlModuleDefaultConfiguration DEFAULT_CONFIGURATION = new SqlModuleDefaultConfiguration();

@Getter
Expand All @@ -68,5 +71,6 @@ class SqlModuleDefaultConfiguration implements SqlModuleConfiguration {
private final String jdbcPassword = EMPTY_STRING;
private final DbProvider dbProvider = POSTGRES;
private final boolean enableMetrics = true;
private final ConnectionPoolInitializationMode connectionPoolInitializationMode = ON_MODULE_LOAD;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* ART Java
*
* Copyright 2019 ART
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ru.art.sql.constants;

public enum ConnectionPoolInitializationMode {
ON_MODULE_LOAD,
MANUAL
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ public interface SqlModuleConstants {
interface ConfigurationDefaults {
String DEFAULT_CONNECTION_TEST_QUERY = "select 1 from dual";
}

interface LoggingMessages {
String CLOSING_POOL = "Closing DB connection pool: {0}";
String STARING_POOL = "Starting connection pool to DataSource: {0}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,30 @@ static PoolProperties createTomcatPoolConfig(DbConnectionProperties properties)
poolProperties.setDriverClassName(properties.getDriver().getDriverClassName());
poolProperties.setUsername(properties.getLogin());
poolProperties.setPassword(properties.getPassword());

poolProperties.setJmxEnabled(true);

poolProperties.setTestWhileIdle(false);
poolProperties.setTestOnReturn(false);
poolProperties.setTestOnBorrow(true);

poolProperties.setValidationQuery(DEFAULT_CONNECTION_TEST_QUERY);
poolProperties.setTestOnReturn(false);
poolProperties.setValidationInterval(30000);
poolProperties.setTimeBetweenEvictionRunsMillis(30000);
poolProperties.setMaxActive(100);

poolProperties.setInitialSize(10);
poolProperties.setMaxWait(10000);
poolProperties.setMinEvictableIdleTimeMillis(30000);
poolProperties.setMinIdle(10);
poolProperties.setMaxActive(100);
poolProperties.setMaxIdle(100);

poolProperties.setTimeBetweenEvictionRunsMillis(30000);
poolProperties.setMinEvictableIdleTimeMillis(30000);

poolProperties.setMaxWait(10000);

poolProperties.setLogAbandoned(true);
poolProperties.setRemoveAbandoned(true);
poolProperties.setRemoveAbandonedTimeout(60);

return poolProperties;
}

Expand Down
71 changes: 61 additions & 10 deletions application-sql/src/main/java/ru/art/sql/module/SqlModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,40 @@

package ru.art.sql.module;

import com.zaxxer.hikari.*;
import io.dropwizard.db.*;
import lombok.*;
import org.apache.logging.log4j.*;
import ru.art.core.module.Module;
import ru.art.core.module.*;
import ru.art.sql.configuration.*;
import ru.art.sql.exception.*;
import ru.art.sql.state.*;
import static java.lang.Class.*;
import static java.text.MessageFormat.*;
import static lombok.AccessLevel.*;
import static ru.art.core.context.Context.*;
import static ru.art.core.extension.NullCheckingExtensions.*;
import static ru.art.core.wrapper.ExceptionWrapper.*;
import static ru.art.logging.LoggingModule.*;
import static ru.art.metrics.module.MetricsModule.*;
import static ru.art.sql.configuration.SqlModuleConfiguration.*;
import static ru.art.sql.constants.ConnectionPoolInitializationMode.*;
import static ru.art.sql.constants.SqlModuleConstants.LoggingMessages.*;
import static ru.art.sql.constants.SqlModuleConstants.*;
import javax.sql.*;
import java.util.function.*;

@Getter
public class SqlModule implements Module<SqlModuleConfiguration, ModuleState> {
public class SqlModule implements Module<SqlModuleConfiguration, SqlModuleState> {
@Getter(lazy = true, value = PRIVATE)
private static final SqlModuleConfiguration sqlModule = context().getModule(SQL_MODULE_ID, SqlModule::new);
@Getter(lazy = true, value = PRIVATE)
private static final SqlModuleState sqlModuleState = context().getModuleState(SQL_MODULE_ID, SqlModule::new);
private final String id = SQL_MODULE_ID;
private final SqlModuleConfiguration defaultConfiguration = DEFAULT_CONFIGURATION;
private final SqlModuleState state = new SqlModuleState();
@Getter(lazy = true, value = PRIVATE)
private final Logger logger = loggingModule().getLogger(SqlModule.class);

public static SqlModuleConfiguration sqlModule() {
if (contextIsNotReady()) {
Expand All @@ -46,23 +60,60 @@ public static SqlModuleConfiguration sqlModule() {
return getSqlModule();
}

public static SqlModuleState sqlModuleState() {
return getSqlModuleState();
}

@Override
public void onLoad() {
DataSource dataSource = null;
switch (sqlModule().getConnectionPoolType()) {
case HIKARI:
dataSource = sqlModule().getHikariPoolConfig().getDataSource();
break;
case TOMCAT:
dataSource = new ManagedPooledDataSource(sqlModule().getTomcatPoolConfig(), metricsModule().getDropwizardMetricRegistry());
break;
}
try {
forName(sqlModule().getDbProvider().getDriverClassName());
switch (sqlModule().getConnectionPoolType()) {
case HIKARI:
HikariDataSource hikariDataSource = new HikariDataSource(sqlModule().getHikariPoolConfig());
dataSource = hikariDataSource;
sqlModuleState().hikariDataSource(hikariDataSource);
if (sqlModule().getConnectionPoolInitializationMode() == ON_MODULE_LOAD) {
hikariDataSource.getConnection();
getLogger().info(format(STARING_POOL, hikariDataSource));
}
break;
case TOMCAT:
ManagedPooledDataSource tomcatDataSource = new ManagedPooledDataSource(sqlModule().getTomcatPoolConfig(), metricsModule().getDropwizardMetricRegistry());
sqlModuleState().tomcatDataSource(tomcatDataSource);
dataSource = tomcatDataSource;
if (sqlModule().getConnectionPoolInitializationMode() == ON_MODULE_LOAD) {
tomcatDataSource.start();
getLogger().info(format(STARING_POOL, tomcatDataSource));
}
break;
}
sqlModule().getJooqConfiguration().set(dataSource).set(sqlModule().getJooqSettings());
} catch (Exception throwable) {
throw new SqlModuleException(throwable);
}
}

@Override
public void onUnload() {
try {
switch (sqlModule().getConnectionPoolType()) {
case HIKARI:
doIfNotNull(sqlModuleState().hikariDataSource(), (Consumer<HikariDataSource>) pool -> ignoreException(() -> {
pool.close();
getLogger().info(format(CLOSING_POOL, pool));
}, getLogger()::error));
break;
case TOMCAT:
doIfNotNull(sqlModuleState().tomcatDataSource(), (Consumer<ManagedPooledDataSource>) pool -> ignoreException(() -> {
pool.close();
getLogger().info(format(CLOSING_POOL, pool));
}, getLogger()::error));
break;
}
} catch (Throwable throwable) {
getLogger().error(throwable.getMessage(), throwable);
}
}
}
15 changes: 15 additions & 0 deletions application-sql/src/main/java/ru/art/sql/state/SqlModuleState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.art.sql.state;

import com.zaxxer.hikari.*;
import io.dropwizard.db.*;
import lombok.*;
import lombok.experimental.*;
import ru.art.core.module.*;

@Getter
@Setter
@Accessors(fluent = true)
public class SqlModuleState implements ModuleState {
private volatile HikariDataSource hikariDataSource;
private volatile ManagedPooledDataSource tomcatDataSource;
}

0 comments on commit 39737a3

Please sign in to comment.