BAEL-1818 A Simple Guide to Connection Pooling in Java (#4823)

* Initial Commit

* Update parent pom.xml

* Update BasicConnectionPool class

* Update BasicConnectionPool class

* BAEL-1818 removed code from core-java module, cleaned up a little pom files

* BAEL-1818 moved the code from connectionpool.connectionpools package to connectionpool
This commit is contained in:
Alejandro Gervasio 2018-07-28 03:44:51 -03:00 committed by Predrag Maric
parent 25fb0c94c2
commit fc02400f5f
12 changed files with 267 additions and 221 deletions

View File

@ -0,0 +1,59 @@
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.core-java-persistence</groupId>
<artifactId>core-java-persistence</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>core-java-persistence</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2database.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>${commons-dbcp2.version}</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>${HikariCP.version}</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
</dependencies>
<build>
<finalName>core-java-persistence</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<assertj-core.version>3.10.0</assertj-core.version>
<h2database.version>1.4.197</h2database.version>
<commons-dbcp2.version>2.4.0</commons-dbcp2.version>
<HikariCP.version>3.2.0</HikariCP.version>
<c3p0.version>0.9.5.2</c3p0.version>
</properties>
</project>

View File

@ -1,4 +1,4 @@
package com.baeldung.connectionpool.connectionpools; package com.baeldung.connectionpool;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
@ -56,10 +56,16 @@ public class BasicConnectionPool implements ConnectionPool {
return DriverManager.getConnection(url, user, password); return DriverManager.getConnection(url, user, password);
} }
@Override
public int getSize() { public int getSize() {
return connectionPool.size() + usedConnections.size(); return connectionPool.size() + usedConnections.size();
} }
@Override
public List<Connection> getConnectionPool() {
return connectionPool;
}
@Override @Override
public String getUrl() { public String getUrl() {
return url; return url;
@ -75,6 +81,7 @@ public class BasicConnectionPool implements ConnectionPool {
return password; return password;
} }
@Override
public void shutdown() throws SQLException { public void shutdown() throws SQLException {
usedConnections.forEach(this::releaseConnection); usedConnections.forEach(this::releaseConnection);
for (Connection c : connectionPool) { for (Connection c : connectionPool) {

View File

@ -1,4 +1,4 @@
package com.baeldung.connectionpool.connectionpools; package com.baeldung.connectionpool;
import com.mchange.v2.c3p0.ComboPooledDataSource; import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException; import java.beans.PropertyVetoException;

View File

@ -1,4 +1,4 @@
package com.baeldung.connectionpool.connectionpools; package com.baeldung.connectionpool;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
@ -10,9 +10,15 @@ public interface ConnectionPool {
boolean releaseConnection(Connection connection); boolean releaseConnection(Connection connection);
List<Connection> getConnectionPool();
int getSize();
String getUrl(); String getUrl();
String getUser(); String getUser();
String getPassword(); String getPassword();
void shutdown() throws SQLException;;
} }

View File

@ -1,4 +1,4 @@
package com.baeldung.connectionpool.connectionpools; package com.baeldung.connectionpool;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;

View File

@ -1,4 +1,4 @@
package com.baeldung.connectionpool.connectionpools; package com.baeldung.connectionpool;
import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;

View File

@ -1,10 +1,8 @@
package com.baeldung.connectionpool; package com.baeldung.connectionpool;
import com.baeldung.connectionpool.connectionpools.BasicConnectionPool;
import com.baeldung.connectionpool.connectionpools.ConnectionPool;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;

View File

@ -1,6 +1,5 @@
package com.baeldung.connectionpool; package com.baeldung.connectionpool;
import com.baeldung.connectionpool.connectionpools.C3poDataSource;
import java.sql.SQLException; import java.sql.SQLException;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.junit.Test; import org.junit.Test;

View File

@ -1,6 +1,5 @@
package com.baeldung.connectionpool; package com.baeldung.connectionpool;
import com.baeldung.connectionpool.connectionpools.DBCPDataSource;
import java.sql.SQLException; import java.sql.SQLException;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.junit.Test; import org.junit.Test;

View File

@ -1,6 +1,5 @@
package com.baeldung.connectionpool; package com.baeldung.connectionpool;
import com.baeldung.connectionpool.connectionpools.HikariCPDataSource;
import java.sql.SQLException; import java.sql.SQLException;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.junit.Test; import org.junit.Test;

View File

@ -158,21 +158,6 @@
<artifactId>jmimemagic</artifactId> <artifactId>jmimemagic</artifactId>
<version>${jmime-magic.version}</version> <version>${jmime-magic.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>${commons-dbcp2.version}</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>${HikariCP.version}</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
<!-- instrumentation --> <!-- instrumentation -->
<dependency> <dependency>
<groupId>org.javassist</groupId> <groupId>org.javassist</groupId>
@ -544,11 +529,6 @@
<!-- testing --> <!-- testing -->
<assertj-core.version>3.10.0</assertj-core.version> <assertj-core.version>3.10.0</assertj-core.version>
<!-- JDBC pooling frameworks -->
<commons-dbcp2.version>2.4.0</commons-dbcp2.version>
<HikariCP.version>3.2.0</HikariCP.version>
<c3p0.version>0.9.5.2</c3p0.version>
<!-- maven plugins --> <!-- maven plugins -->
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version> <maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<springframework.spring-web.version>4.3.4.RELEASE</springframework.spring-web.version> <springframework.spring-web.version>4.3.4.RELEASE</springframework.spring-web.version>

View File

@ -312,6 +312,7 @@
<module>core-java-collections</module> <module>core-java-collections</module>
<module>core-java-io</module> <module>core-java-io</module>
<module>core-java-8</module> <module>core-java-8</module>
<module>core-java-persistence</module>
<module>core-kotlin</module> <module>core-kotlin</module>
<module>core-groovy</module> <module>core-groovy</module>
<module>core-java-concurrency</module> <module>core-java-concurrency</module>
@ -1233,6 +1234,4 @@
<!-- <maven-pmd-plugin.version>3.9.0</maven-pmd-plugin.version> --> <!-- <maven-pmd-plugin.version>3.9.0</maven-pmd-plugin.version> -->
<maven-pmd-plugin.version>3.8</maven-pmd-plugin.version> <maven-pmd-plugin.version>3.8</maven-pmd-plugin.version>
</properties> </properties>
</project> </project>