-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
257 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<parent> | ||
<groupId>com.vladmihalcea.flexy-pool</groupId> | ||
<artifactId>flexy-pool-parent</artifactId> | ||
<version>1.0.8-SNAPSHOT</version> | ||
</parent> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>flexy-tomcatcp</artifactId> | ||
<version>1.0.8-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>flexy-tomcatcp</name> | ||
<description>The flexible pool Tomcat JDBC connection pool adapter</description> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>com.vladmihalcea.flexy-pool</groupId> | ||
<artifactId>flexy-pool-core</artifactId> | ||
<version>1.0.8-SNAPSHOT</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.tomcat</groupId> | ||
<artifactId>tomcat-jdbc</artifactId> | ||
<version>${tomcat.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!-- Test dependencies --> | ||
<dependency> | ||
<groupId>com.vladmihalcea.flexy-pool</groupId> | ||
<artifactId>flexy-common-adapter</artifactId> | ||
<version>1.0.8-SNAPSHOT</version> | ||
<type>test-jar</type> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-aop</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-tx</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-jdbc</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.hsqldb</groupId> | ||
<artifactId>hsqldb</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<properties> | ||
<tomcat.version>7.0.35</tomcat.version> | ||
</properties> | ||
|
||
</project> |
59 changes: 59 additions & 0 deletions
59
flexy-tomcatcp/src/main/java/com/vladmihalcea/flexypool/adaptor/TomcatCPPoolAdapter.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,59 @@ | ||
package com.vladmihalcea.flexypool.adaptor; | ||
|
||
import com.vladmihalcea.flexypool.exception.AcquireTimeoutException; | ||
import com.vladmihalcea.flexypool.metric.Metrics; | ||
import com.vladmihalcea.flexypool.util.ConfigurationProperties; | ||
import org.apache.tomcat.jdbc.pool.DataSource; | ||
import org.apache.tomcat.jdbc.pool.PoolExhaustedException; | ||
|
||
import java.sql.SQLException; | ||
|
||
/** | ||
* <code>TomcatCPPoolAdapter</code> extends {@link AbstractPoolAdapter} and it adapts the required API to | ||
* communicate with the Tomcat CP {@link DataSource} | ||
* | ||
* @author Vlad Mihalcea | ||
* @version %I%, %E% | ||
* @since 1.0 | ||
*/ | ||
public class TomcatCPPoolAdapter extends AbstractPoolAdapter<DataSource> { | ||
|
||
public static final String ACQUIRE_TIMEOUT_MESSAGE = "Timeout of .*?ms encountered waiting for connection\\."; | ||
|
||
public static final PoolAdapterFactory<DataSource> FACTORY = new PoolAdapterFactory<DataSource>() { | ||
|
||
@Override | ||
public PoolAdapter<DataSource> newInstance( | ||
ConfigurationProperties<DataSource, Metrics, PoolAdapter<DataSource>> configurationProperties) { | ||
return new TomcatCPPoolAdapter(configurationProperties); | ||
} | ||
}; | ||
|
||
public TomcatCPPoolAdapter(ConfigurationProperties<DataSource, Metrics, PoolAdapter<DataSource>> configurationProperties) { | ||
super(configurationProperties); | ||
} | ||
|
||
@Override | ||
public int getMaxPoolSize() { | ||
return getTargetDataSource().getMaxActive(); | ||
} | ||
|
||
@Override | ||
public void setMaxPoolSize(int maxPoolSize) { | ||
getTargetDataSource().setMaxActive(maxPoolSize); | ||
} | ||
|
||
/** | ||
* Translate the TomcatCP Exception to AcquireTimeoutException. | ||
* | ||
* @param e exception | ||
* @return translated exception | ||
*/ | ||
@Override | ||
protected SQLException translateException(Exception e) { | ||
if (e instanceof PoolExhaustedException) { | ||
return new AcquireTimeoutException(e); | ||
} | ||
return new SQLException(e); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
flexy-tomcatcp/src/test/java/com/vladmihalcea/flexypool/TomcatCPIntegrationTest.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,16 @@ | ||
package com.vladmihalcea.flexypool; | ||
|
||
import org.junit.runner.RunWith; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
/** | ||
* TomcatCPIntegrationTest - DataSource Integration Test | ||
* | ||
* @author Vlad Mihalcea | ||
*/ | ||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(locations = "classpath:spring/applicationContext-test.xml") | ||
public class TomcatCPIntegrationTest extends AbstractPoolAdapterIntegrationTest { | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
flexy-tomcatcp/src/test/java/com/vladmihalcea/flexypool/config/FlexyPoolConfiguration.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,43 @@ | ||
package com.vladmihalcea.flexypool.config; | ||
|
||
import com.vladmihalcea.flexypool.FlexyPoolDataSource; | ||
import com.vladmihalcea.flexypool.adaptor.TomcatCPPoolAdapter; | ||
import com.vladmihalcea.flexypool.strategy.IncrementPoolOnTimeoutConnectionAcquiringStrategy; | ||
import com.vladmihalcea.flexypool.strategy.RetryConnectionAcquiringStrategy; | ||
import org.apache.tomcat.jdbc.pool.DataSource; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
/** | ||
* FlexyPoolConfiguration - Configuration for flexypool data source | ||
* | ||
* @author Vlad Mihalcea | ||
*/ | ||
@org.springframework.context.annotation.Configuration | ||
public class FlexyPoolConfiguration { | ||
|
||
@Autowired | ||
private DataSource poolingDataSource; | ||
|
||
@Value("${flexy.pool.uniqueId}") | ||
private String uniqueId; | ||
|
||
@Bean | ||
public Configuration<DataSource> configuration() { | ||
return new Configuration.Builder<DataSource>( | ||
uniqueId, | ||
poolingDataSource, | ||
TomcatCPPoolAdapter.FACTORY | ||
).build(); | ||
} | ||
|
||
@Bean(initMethod = "start", destroyMethod = "stop") | ||
public FlexyPoolDataSource dataSource() { | ||
Configuration<DataSource> configuration = configuration(); | ||
return new FlexyPoolDataSource<DataSource>(configuration, | ||
new IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory(5), | ||
new RetryConnectionAcquiringStrategy.Factory(2) | ||
); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
flexy-tomcatcp/src/test/resources/spring/applicationContext-test.xml
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,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:context="http://www.springframework.org/schema/context" | ||
xsi:schemaLocation=" | ||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd | ||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> | ||
|
||
<context:component-scan base-package="com.vladmihalcea.flexypool.config"/> | ||
<!-- Xml configuration --> | ||
<!--<import resource="classpath:spring/applicationContext-xml-config.xml"/>--> | ||
|
||
<import resource="classpath:spring/applicationContext-local-resource.xml"/> | ||
|
||
<bean id="poolingDataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"> | ||
<property name="driverClassName" value="${jdbc.driverClassName}" /> | ||
<property name="url" value="${jdbc.url}" /> | ||
<property name="username" value="${jdbc.username}" /> | ||
<property name="password" value="${jdbc.password}" /> | ||
<property name="initialSize" value="1"/> | ||
<property name="maxActive" value="3"/> | ||
<property name="maxWait" value="100"/> | ||
</bean> | ||
|
||
</beans> |
29 changes: 29 additions & 0 deletions
29
flexy-tomcatcp/src/test/resources/spring/applicationContext-xml-config.xml
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,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation=" | ||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> | ||
|
||
<bean id="configurationBuilder" class="com.vladmihalcea.flexypool.config.Configuration$Builder"> | ||
<constructor-arg value="uniqueId"/> | ||
<constructor-arg ref="poolingDataSource"/> | ||
<constructor-arg value="#{ T(com.vladmihalcea.flexypool.adaptor.TomcatCPPoolAdapter.FACTORY }"/> | ||
</bean> | ||
|
||
<bean id="configuration" factory-bean="configurationBuilder" factory-method="build"/> | ||
|
||
<bean id="dataSource" class="com.vladmihalcea.flexypool.FlexyPoolDataSource" init-method="start" destroy-method="stop"> | ||
<constructor-arg ref="configuration"/> | ||
<constructor-arg> | ||
<array> | ||
<bean class="com.vladmihalcea.flexypool.strategy.IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory"> | ||
<constructor-arg value="5"/> | ||
</bean> | ||
<bean class="com.vladmihalcea.flexypool.strategy.RetryConnectionAcquiringStrategy.Factory"> | ||
<constructor-arg value="2"/> | ||
</bean> | ||
</array> | ||
</constructor-arg> | ||
</bean> | ||
|
||
</beans> |
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