Skip to content

Commit

Permalink
fix: backport the pool fix by copying the upstream module
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Nov 9, 2023
1 parent 9515db2 commit 32110f0
Show file tree
Hide file tree
Showing 19 changed files with 1,194 additions and 17 deletions.
14 changes: 3 additions & 11 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ maven/mavencentral/io.swagger.core.v3/swagger-annotations-jakarta/2.2.2, Apache-
maven/mavencentral/io.swagger.core.v3/swagger-annotations/2.2.10, Apache-2.0, approved, #11362
maven/mavencentral/io.swagger.core.v3/swagger-core-jakarta/2.2.2, Apache-2.0, approved, #5929
maven/mavencentral/io.swagger.core.v3/swagger-core/2.2.10, Apache-2.0, approved, #9265
maven/mavencentral/io.swagger.core.v3/swagger-integration-jakarta/2.2.2, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.swagger.core.v3/swagger-integration-jakarta/2.2.2, Apache-2.0, approved, #11475
maven/mavencentral/io.swagger.core.v3/swagger-integration/2.2.10, Apache-2.0, approved, #10352
maven/mavencentral/io.swagger.core.v3/swagger-jaxrs2-jakarta/2.2.2, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.swagger.core.v3/swagger-jaxrs2-jakarta/2.2.2, Apache-2.0, approved, #11477
maven/mavencentral/io.swagger.core.v3/swagger-jaxrs2/2.2.10, Apache-2.0, approved, #9814
maven/mavencentral/io.swagger.core.v3/swagger-models-jakarta/2.2.2, Apache-2.0, approved, #5919
maven/mavencentral/io.swagger.core.v3/swagger-models/2.2.10, Apache-2.0, approved, #10353
Expand Down Expand Up @@ -179,7 +179,7 @@ maven/mavencentral/org.antlr/antlr4-runtime/4.9.3, BSD-3-Clause, approved, #322
maven/mavencentral/org.apache.commons/commons-compress/1.23.0, Apache-2.0 AND BSD-3-Clause, approved, #7506
maven/mavencentral/org.apache.commons/commons-lang3/3.11, Apache-2.0, approved, CQ22642
maven/mavencentral/org.apache.commons/commons-lang3/3.12.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.apache.commons/commons-pool2/2.11.1, Apache-2.0, approved, CQ23795
maven/mavencentral/org.apache.commons/commons-pool2/2.12.0, Apache-2.0 AND LicenseRef-Public-Domain, approved, #10843
maven/mavencentral/org.apache.groovy/groovy-bom/4.0.11, Apache-2.0, approved, #9266
maven/mavencentral/org.apache.groovy/groovy-json/4.0.11, Apache-2.0, approved, #7411
maven/mavencentral/org.apache.groovy/groovy-xml/4.0.11, Apache-2.0, approved, #10179
Expand Down Expand Up @@ -231,7 +231,6 @@ maven/mavencentral/org.eclipse.edc/control-plane-api-client-spi/0.2.1, Apache-2.
maven/mavencentral/org.eclipse.edc/control-plane-core/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/control-plane-spi/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/core-spi/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/core-spi/0.3.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/data-plane-api/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/data-plane-aws-s3/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/data-plane-client/0.2.1, Apache-2.0, approved, technology.edc
Expand Down Expand Up @@ -287,20 +286,14 @@ maven/mavencentral/org.eclipse.edc/policy-engine-spi/0.2.1, Apache-2.0, approved
maven/mavencentral/org.eclipse.edc/policy-engine/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/policy-evaluator/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/policy-model/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/policy-model/0.3.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/policy-spi/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/runtime-metamodel/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/runtime-metamodel/0.3.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/sql-core/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/sql-core/0.3.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/sql-lease/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/sql-pool-apache-commons/0.3.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/state-machine/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/transaction-datasource-spi/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/transaction-datasource-spi/0.3.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/transaction-local/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/transaction-spi/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/transaction-spi/0.3.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/transfer-core/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/transfer-data-plane-spi/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/transfer-data-plane/0.2.1, Apache-2.0, approved, technology.edc
Expand All @@ -311,7 +304,6 @@ maven/mavencentral/org.eclipse.edc/transfer-spi/0.2.1, Apache-2.0, approved, tec
maven/mavencentral/org.eclipse.edc/transform-core/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/transform-spi/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/util/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/util/0.3.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/validator-core/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/validator-spi/0.2.1, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/vault-azure/0.2.1, Apache-2.0, approved, technology.edc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ plugins {
dependencies {
runtimeOnly(project(":edc-controlplane:edc-controlplane-base"))
runtimeOnly(project(":edc-extensions:postgresql-migration"))
runtimeOnly(project(":edc-extensions:sql-pool"))
runtimeOnly(project(":edc-extensions:edr:edr-cache-sql"))
runtimeOnly(project(":edc-extensions:bpn-validation:business-partner-store-sql"))
runtimeOnly(libs.edc.azure.vault)
runtimeOnly(libs.bundles.edc.sqlstores)
runtimeOnly(libs.edc.transaction.local)
runtimeOnly(libs.edc.sql.pool)
runtimeOnly(libs.edc.core.controlplane)
runtimeOnly(libs.edc.dpf.transfer)
runtimeOnly(libs.postgres)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ plugins {
dependencies {
runtimeOnly(project(":edc-controlplane:edc-controlplane-base"))
runtimeOnly(project(":edc-extensions:postgresql-migration"))
runtimeOnly(project(":edc-extensions:sql-pool"))
runtimeOnly(project(":edc-extensions:edr:edr-cache-sql"))
runtimeOnly(project(":edc-extensions:bpn-validation:business-partner-store-sql"))
runtimeOnly(libs.edc.vault.hashicorp)
runtimeOnly(libs.bundles.edc.sqlstores)
runtimeOnly(libs.edc.transaction.local)
runtimeOnly(libs.edc.sql.pool)
runtimeOnly(libs.edc.core.controlplane)
runtimeOnly(libs.edc.dpf.transfer)
runtimeOnly(libs.postgres)
Expand Down
2 changes: 1 addition & 1 deletion edc-dataplane/edc-dataplane-azure-vault/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ dependencies {
implementation(libs.edc.azure.identity)
implementation("com.azure:azure-security-keyvault-secrets:4.6.4")
runtimeOnly(project(":edc-extensions:edr:edr-cache-sql"))
runtimeOnly(project(":edc-extensions:sql-pool"))
runtimeOnly(libs.edc.transaction.local)
runtimeOnly(libs.edc.sql.pool)
runtimeOnly(libs.postgres)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ dependencies {
implementation(project(":edc-dataplane:edc-dataplane-base"))
runtimeOnly(libs.edc.vault.hashicorp)
runtimeOnly(project(":edc-extensions:edr:edr-cache-sql"))
runtimeOnly(project(":edc-extensions:sql-pool"))
runtimeOnly(libs.edc.transaction.local)
runtimeOnly(libs.edc.sql.pool)
runtimeOnly(libs.postgres)
}

Expand Down
36 changes: 36 additions & 0 deletions edc-extensions/sql-pool/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# SQL Pool Apache Commons Pool

This extension registers named `javax.sql.DataSource`s to
the `org.eclipse.edc.transaction.datasource.spi.DataSourceRegistry`
capable of pooling `java.sql.Connection`s. The pooling mechanism is backed by
the [Apache Commons Pool library](https://commons.apache.org/proper/commons-pool/).

## Old Configuration (Deprecated since 0.3.1)

| Key | Description | Mandatory |
|:--------------------------------------------------------------|:-------------------------------------------------------------------------------------------------------|-----------|
| edc.datasource.<datasource_name>.url | JDBC driver url | X |
| edc.datasource.<datasource_name>.pool.maxIdleConnections | The maximum amount of idling connections maintained by the pool | |
| edc.datasource.<datasource_name>.pool.maxTotalConnections | The maximum amount of total connections maintained by the pool | |
| edc.datasource.<datasource_name>.pool.minIdleConnections | The minimum amount of idling connections maintained by the pool | |
| edc.datasource.<datasource_name>.pool.testConnectionOnBorrow | Flag to define whether connections will be validated when a connection has been obtained from the pool | |
| edc.datasource.<datasource_name>.pool.testConnectionOnCreate | Flag to define whether connections will be validated when a connection has been established | |
| edc.datasource.<datasource_name>.pool.testConnectionOnReturn | Flag to define whether connections will be validated when a connection has been returned to the pool | |
| edc.datasource.<datasource_name>.pool.testConnectionWhileIdle | Flag to define whether idling connections will be validated | |
| edc.datasource.<datasource_name>.pool.testQuery | Test query to validate a connection maintained by the pool | |
| edc.datasource.<datasource_name>.<jdbc_properties> | JDBC driver specific configuration properties | |

## New Configuration (since 0.3.1)

| Key | Description | Mandatory |
|:-----------------------------------------------------------------|:-------------------------------------------------------------------------------------------------------|-----------|
| edc.datasource.<datasource_name>.url | JDBC driver url | X |
| edc.datasource.<datasource_name>.pool.connections.max-idle | The maximum amount of idling connections maintained by the pool | |
| edc.datasource.<datasource_name>.pool.connections.max-total | The maximum amount of total connections maintained by the pool | |
| edc.datasource.<datasource_name>.pool.connections.min-idle | The minimum amount of idling connections maintained by the pool | |
| edc.datasource.<datasource_name>.pool.connection.test.on-borrow | Flag to define whether connections will be validated when a connection has been obtained from the pool | |
| edc.datasource.<datasource_name>.pool.connection.test.on-create | Flag to define whether connections will be validated when a connection has been established | |
| edc.datasource.<datasource_name>.pool.connection.test.on-return | Flag to define whether connections will be validated when a connection has been returned to the pool | |
| edc.datasource.<datasource_name>.pool.connection.test.while-idle | Flag to define whether idling connections will be validated | |
| edc.datasource.<datasource_name>.pool.connection.test.query | Test query to validate a connection maintained by the pool | |
| edc.datasource.<datasource_name>.<jdbc_properties> | JDBC driver specific configuration properties | |
32 changes: 32 additions & 0 deletions edc-extensions/sql-pool/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2021 Daimler TSS GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Daimler TSS GmbH - Initial build file
*
*/

plugins {
`java-library`
`java-test-fixtures`
`maven-publish`
}


dependencies {
api(libs.edc.spi.transaction.datasource)
api(libs.edc.sql.core)
implementation(libs.apache.commons.pool)

testImplementation(libs.edc.junit)
testImplementation(libs.edc.transaction.local)

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.tractusx.edc.sql.pool.commons;

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.DestroyMode;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.persistence.EdcPersistenceException;
import org.eclipse.edc.sql.pool.ConnectionPool;
import org.jetbrains.annotations.NotNull;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Objects;
import javax.sql.DataSource;

public final class CommonsConnectionPool implements ConnectionPool, AutoCloseable {
private final GenericObjectPool<Connection> connectionObjectPool;
private final CommonsConnectionPoolConfig poolConfig;

public CommonsConnectionPool(DataSource dataSource, CommonsConnectionPoolConfig commonsConnectionPoolConfig, Monitor monitor) {
this.poolConfig = commonsConnectionPoolConfig;
Objects.requireNonNull(dataSource, "connectionFactory");
Objects.requireNonNull(commonsConnectionPoolConfig, "commonsConnectionPoolConfig");

this.connectionObjectPool = new GenericObjectPool<>(
new PooledConnectionObjectFactory(dataSource, commonsConnectionPoolConfig.getTestQuery(), monitor),
getGenericObjectPoolConfig(commonsConnectionPoolConfig));
}

private static GenericObjectPoolConfig<Connection> getGenericObjectPoolConfig(CommonsConnectionPoolConfig commonsConnectionPoolConfig) {
GenericObjectPoolConfig<Connection> genericObjectPoolConfig = new GenericObjectPoolConfig<>();

// no need for JMX
genericObjectPoolConfig.setJmxEnabled(false);

genericObjectPoolConfig.setMaxIdle(commonsConnectionPoolConfig.getMaxIdleConnections());
genericObjectPoolConfig.setMaxTotal(commonsConnectionPoolConfig.getMaxTotalConnections());
genericObjectPoolConfig.setMinIdle(commonsConnectionPoolConfig.getMinIdleConnections());

genericObjectPoolConfig.setTestOnBorrow(commonsConnectionPoolConfig.getTestConnectionOnBorrow());
genericObjectPoolConfig.setTestOnCreate(commonsConnectionPoolConfig.getTestConnectionOnCreate());
genericObjectPoolConfig.setTestOnReturn(commonsConnectionPoolConfig.getTestConnectionOnReturn());
genericObjectPoolConfig.setTestWhileIdle(commonsConnectionPoolConfig.getTestConnectionWhileIdle());

return genericObjectPoolConfig;
}

@Override
public Connection getConnection() {
try {
return connectionObjectPool.borrowObject();
} catch (Exception e) {
throw new EdcPersistenceException(e.getMessage(), e);
}
}

@Override
public void returnConnection(Connection connection) {
Objects.requireNonNull(connection, "connection");

connectionObjectPool.returnObject(connection);
}

@Override
public void close() {
connectionObjectPool.close();
}

public CommonsConnectionPoolConfig getPoolConfig() {
return poolConfig;
}

private static class PooledConnectionObjectFactory extends BasePooledObjectFactory<Connection> {
private final String testQuery;
private final DataSource dataSource;

private final Monitor monitor;

PooledConnectionObjectFactory(@NotNull DataSource dataSource, @NotNull String testQuery, Monitor monitor) {
this.dataSource = Objects.requireNonNull(dataSource);
this.testQuery = Objects.requireNonNull(testQuery);
this.monitor = monitor;
}

@Override
public Connection create() throws SQLException {
return dataSource.getConnection();
}

@Override
public boolean validateObject(PooledObject<Connection> pooledObject) {
if (pooledObject == null) {
return false;
}

Connection connection = pooledObject.getObject();
if (connection == null) {
return false;
}

return isConnectionValid(connection);
}

@Override
public PooledObject<Connection> wrap(Connection connection) {
return new DefaultPooledObject<>(connection);
}

@Override
public void destroyObject(PooledObject<Connection> pooledObject, DestroyMode destroyMode) throws Exception {
if (pooledObject == null) {
return;
}

Connection connection = pooledObject.getObject();

if (connection != null && !connection.isClosed()) {
connection.close();
}

pooledObject.invalidate();
}

private boolean isConnectionValid(Connection connection) {
try {
if (connection.isClosed()) {
return false;
}

try (PreparedStatement preparedStatement = connection.prepareStatement(testQuery)) {
preparedStatement.execute();
return rollbackIfNeeded(connection);
}
} catch (Exception e) { // any exception thrown indicates invalidity of the connection
return false;
}
}

private boolean rollbackIfNeeded(Connection connection) {
try {
if (!connection.getAutoCommit()) {
connection.rollback();
}
return true;
} catch (SQLException e) {
monitor.debug("Failed to rollback transaction", e);
}
return false;
}

}
}
Loading

0 comments on commit 32110f0

Please sign in to comment.