-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Connection Validation and Pool configuration (#6233)
* Add Connection Pool Validation (#6119) (Cherrypick from 8a26d34) * PoolingJndiDataSourceFactory shouldn't check for DataSource to be XA or not XA (#6187) * Try to determine if a datasource already does pooling (#6199) Only works for non-XA datasources currently, for XA datasources we need to still consider what is best to do as many XA drivers already implement pooling even if it's not configured. (cherry picked from commit 281bf1e)
- Loading branch information
Showing
20 changed files
with
284 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
core/src/main/java/org/frankframework/jndi/AbstractXADataSourceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
Copyright 2024 WeAreFrank! | ||
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 org.frankframework.jndi; | ||
|
||
import javax.sql.CommonDataSource; | ||
import javax.sql.DataSource; | ||
import javax.sql.XADataSource; | ||
|
||
public abstract class AbstractXADataSourceFactory extends PoolingJndiDataSourceFactory { | ||
|
||
@Override | ||
protected DataSource augmentDatasource(CommonDataSource dataSource, String dataSourceName) { | ||
if (dataSource instanceof XADataSource) { | ||
log.info("DataSource [{}] is XA enabled, registering with a Transaction Manager", dataSourceName); | ||
return createXADataSource((XADataSource) dataSource, dataSourceName); | ||
} | ||
|
||
if(maxPoolSize > 1) { | ||
log.info("DataSource [{}] is not XA enabled, creating connection pool for the datasource", dataSourceName); | ||
return createPool((DataSource)dataSource, dataSourceName); | ||
} | ||
log.info("DataSource [{}] is not XA enabled and pooling not configured, used without augmentation", dataSourceName); | ||
return (DataSource) dataSource; | ||
} | ||
|
||
protected abstract DataSource createXADataSource(XADataSource xaDataSource, String dataSourceName); | ||
} |
2 changes: 1 addition & 1 deletion
2
core/src/main/java/org/frankframework/jndi/JndiDataSourceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
core/src/main/java/org/frankframework/jndi/PoolingJndiDataSourceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
Copyright 2024 WeAreFrank! | ||
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 org.frankframework.jndi; | ||
|
||
import java.sql.Connection; | ||
import java.time.Duration; | ||
|
||
import javax.sql.CommonDataSource; | ||
import javax.sql.ConnectionPoolDataSource; | ||
import javax.sql.DataSource; | ||
|
||
import org.apache.commons.dbcp2.ConnectionFactory; | ||
import org.apache.commons.dbcp2.DataSourceConnectionFactory; | ||
import org.apache.commons.dbcp2.PoolableConnection; | ||
import org.apache.commons.dbcp2.PoolableConnectionFactory; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.commons.pool2.impl.GenericObjectPool; | ||
import org.frankframework.jdbc.datasource.OpenPoolingDataSource; | ||
import org.frankframework.util.AppConstants; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
/** | ||
* Factory through which (TX-enabled) Pooling DataSources can be retrieved. | ||
* | ||
* Already created DataSources are stored in a ConcurrentHashMap. | ||
* Every DataSource can be augmented before it is added. | ||
*/ | ||
public class PoolingJndiDataSourceFactory extends JndiDataSourceFactory { | ||
|
||
public static final String DEFAULT_DATASOURCE_NAME_PROPERTY = "jdbc.datasource.default"; | ||
public static final String GLOBAL_DEFAULT_DATASOURCE_NAME = AppConstants.getInstance().getProperty(DEFAULT_DATASOURCE_NAME_PROPERTY); | ||
@Getter @Setter protected int minPoolSize = 0; | ||
@Getter @Setter protected int maxPoolSize = 20; | ||
@Getter @Setter protected int maxIdle = 2; | ||
@Getter @Setter protected int maxLifeTime = 0; | ||
@Getter @Setter protected int connectionCheckInterval = 300; | ||
@Getter @Setter protected String testQuery = null; | ||
|
||
public PoolingJndiDataSourceFactory() { | ||
super(); | ||
AppConstants appConstants = AppConstants.getInstance(); | ||
minPoolSize = appConstants.getInt("transactionmanager.jdbc.connection.minPoolSize", minPoolSize); | ||
maxPoolSize = appConstants.getInt("transactionmanager.jdbc.connection.maxPoolSize", maxPoolSize); | ||
maxIdle = appConstants.getInt("transactionmanager.jdbc.connection.maxIdle", maxIdle); | ||
maxLifeTime = appConstants.getInt("transactionmanager.jdbc.connection.maxLifeTime", maxLifeTime); | ||
connectionCheckInterval = appConstants.getInt("transactionmanager.jdbc.connection.checkInterval", connectionCheckInterval); | ||
testQuery = appConstants.getString("transactionmanager.jdbc.connection.testQuery", testQuery); | ||
} | ||
|
||
@Override | ||
protected DataSource augmentDatasource(CommonDataSource dataSource, String dataSourceName) { | ||
if(maxPoolSize > 1) { | ||
log.info("Creating connection pool for datasource [{}]", dataSourceName); | ||
return createPool((DataSource)dataSource, dataSourceName); | ||
} | ||
log.info("Pooling not configured, using datasource [{}] without augmentation", dataSourceName); | ||
return (DataSource) dataSource; | ||
} | ||
|
||
private static boolean isPooledDataSource(CommonDataSource dataSource) { | ||
return dataSource instanceof ConnectionPoolDataSource | ||
|| dataSource.getClass().getName().startsWith("org.apache.tomcat") | ||
; | ||
} | ||
|
||
protected DataSource createPool(DataSource dataSource, String dataSourceName) { | ||
if (isPooledDataSource(dataSource)) { | ||
log.warn("DataSource [{}] already implements pooling. Will not be wrapped with DBCP2 pool. Frank!Framework connection pooling configuration is ignored, configure pooling properties in the JNDI Resource to avoid issues.", dataSourceName); | ||
return dataSource; | ||
} | ||
ConnectionFactory cf = new DataSourceConnectionFactory(dataSource); | ||
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(cf, null); | ||
|
||
GenericObjectPool<PoolableConnection> connectionPool = createConnectionPool(poolableConnectionFactory); | ||
OpenPoolingDataSource<PoolableConnection> ds = new OpenPoolingDataSource<>(connectionPool); | ||
log.info("registered PoolingDataSource [{}]", ds); | ||
return ds; | ||
} | ||
|
||
protected GenericObjectPool<PoolableConnection> createConnectionPool(PoolableConnectionFactory poolableConnectionFactory) { | ||
poolableConnectionFactory.setAutoCommitOnReturn(false); | ||
poolableConnectionFactory.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); | ||
if (maxLifeTime > 0) { | ||
poolableConnectionFactory.setMaxConn(Duration.ofSeconds(maxLifeTime)); | ||
} | ||
poolableConnectionFactory.setRollbackOnReturn(true); | ||
if (StringUtils.isNotBlank(testQuery)) { | ||
poolableConnectionFactory.setValidationQuery(testQuery); | ||
poolableConnectionFactory.setValidationQueryTimeout(Duration.ofSeconds(5)); | ||
} | ||
poolableConnectionFactory.setFastFailValidation(true); | ||
GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory); | ||
connectionPool.setMinIdle(minPoolSize); | ||
connectionPool.setMaxTotal(maxPoolSize); | ||
connectionPool.setMaxIdle(maxIdle); | ||
connectionPool.setTestOnBorrow(true); | ||
connectionPool.setTestWhileIdle(true); | ||
if (connectionCheckInterval > 0) { | ||
connectionPool.setDurationBetweenEvictionRuns(Duration.ofSeconds(connectionCheckInterval)); | ||
} | ||
connectionPool.setBlockWhenExhausted(true); | ||
poolableConnectionFactory.setPool(connectionPool); | ||
return connectionPool; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.