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,85 +1,92 @@
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;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class BasicConnectionPool implements ConnectionPool { public class BasicConnectionPool implements ConnectionPool {
private final String url; private final String url;
private final String user; private final String user;
private final String password; private final String password;
private final List<Connection> connectionPool; private final List<Connection> connectionPool;
private final List<Connection> usedConnections = new ArrayList<>(); private final List<Connection> usedConnections = new ArrayList<>();
private static final int INITIAL_POOL_SIZE = 10; private static final int INITIAL_POOL_SIZE = 10;
private final int MAX_POOL_SIZE = 20; private final int MAX_POOL_SIZE = 20;
public static BasicConnectionPool create(String url, String user, String password) throws SQLException { public static BasicConnectionPool create(String url, String user, String password) throws SQLException {
List<Connection> pool = new ArrayList<>(INITIAL_POOL_SIZE); List<Connection> pool = new ArrayList<>(INITIAL_POOL_SIZE);
for (int i = 0; i < INITIAL_POOL_SIZE; i++) { for (int i = 0; i < INITIAL_POOL_SIZE; i++) {
pool.add(createConnection(url, user, password)); pool.add(createConnection(url, user, password));
} }
return new BasicConnectionPool(url, user, password, pool); return new BasicConnectionPool(url, user, password, pool);
} }
private BasicConnectionPool(String url, String user, String password, List<Connection> connectionPool) { private BasicConnectionPool(String url, String user, String password, List<Connection> connectionPool) {
this.url = url; this.url = url;
this.user = user; this.user = user;
this.password = password; this.password = password;
this.connectionPool = connectionPool; this.connectionPool = connectionPool;
} }
@Override @Override
public Connection getConnection() throws SQLException { public Connection getConnection() throws SQLException {
if (connectionPool.isEmpty()) { if (connectionPool.isEmpty()) {
if (usedConnections.size() < MAX_POOL_SIZE) { if (usedConnections.size() < MAX_POOL_SIZE) {
connectionPool.add(createConnection(url, user, password)); connectionPool.add(createConnection(url, user, password));
} else { } else {
throw new RuntimeException("Maximum pool size reached, no available connections!"); throw new RuntimeException("Maximum pool size reached, no available connections!");
} }
} }
Connection connection = connectionPool.remove(connectionPool.size() - 1); Connection connection = connectionPool.remove(connectionPool.size() - 1);
usedConnections.add(connection); usedConnections.add(connection);
return connection; return connection;
} }
@Override @Override
public boolean releaseConnection(Connection connection) { public boolean releaseConnection(Connection connection) {
connectionPool.add(connection); connectionPool.add(connection);
return usedConnections.remove(connection); return usedConnections.remove(connection);
} }
private static Connection createConnection(String url, String user, String password) throws SQLException { private static Connection createConnection(String url, String user, String password) throws SQLException {
return DriverManager.getConnection(url, user, password); return DriverManager.getConnection(url, user, password);
} }
public int getSize() { @Override
return connectionPool.size() + usedConnections.size(); public int getSize() {
} return connectionPool.size() + usedConnections.size();
}
@Override
public String getUrl() { @Override
return url; public List<Connection> getConnectionPool() {
} return connectionPool;
}
@Override
public String getUser() { @Override
return user; public String getUrl() {
} return url;
}
@Override
public String getPassword() { @Override
return password; public String getUser() {
} return user;
}
public void shutdown() throws SQLException {
usedConnections.forEach(this::releaseConnection); @Override
for (Connection c : connectionPool) { public String getPassword() {
c.close(); return password;
} }
connectionPool.clear();
} @Override
} public void shutdown() throws SQLException {
usedConnections.forEach(this::releaseConnection);
for (Connection c : connectionPool) {
c.close();
}
connectionPool.clear();
}
}

View File

@ -1,28 +1,28 @@
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;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
public class C3poDataSource { public class C3poDataSource {
private static final ComboPooledDataSource cpds = new ComboPooledDataSource(); private static final ComboPooledDataSource cpds = new ComboPooledDataSource();
static { static {
try { try {
cpds.setDriverClass("org.h2.Driver"); cpds.setDriverClass("org.h2.Driver");
cpds.setJdbcUrl("jdbc:h2:mem:test"); cpds.setJdbcUrl("jdbc:h2:mem:test");
cpds.setUser("user"); cpds.setUser("user");
cpds.setPassword("password"); cpds.setPassword("password");
} catch (PropertyVetoException e) { } catch (PropertyVetoException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public static Connection getConnection() throws SQLException { public static Connection getConnection() throws SQLException {
return cpds.getConnection(); return cpds.getConnection();
} }
private C3poDataSource(){} private C3poDataSource(){}
} }

View File

@ -1,18 +1,24 @@
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;
import java.util.List; import java.util.List;
public interface ConnectionPool { public interface ConnectionPool {
Connection getConnection() throws SQLException; Connection getConnection() throws SQLException;
boolean releaseConnection(Connection connection); boolean releaseConnection(Connection connection);
String getUrl(); List<Connection> getConnectionPool();
String getUser(); int getSize();
String getPassword(); String getUrl();
String getUser();
String getPassword();
void shutdown() throws SQLException;;
} }

View File

@ -1,25 +1,25 @@
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;
import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.dbcp2.BasicDataSource;
public class DBCPDataSource { public class DBCPDataSource {
private static final BasicDataSource ds = new BasicDataSource(); private static final BasicDataSource ds = new BasicDataSource();
static { static {
ds.setUrl("jdbc:h2:mem:test"); ds.setUrl("jdbc:h2:mem:test");
ds.setUsername("user"); ds.setUsername("user");
ds.setPassword("password"); ds.setPassword("password");
ds.setMinIdle(5); ds.setMinIdle(5);
ds.setMaxIdle(10); ds.setMaxIdle(10);
ds.setMaxOpenPreparedStatements(100); ds.setMaxOpenPreparedStatements(100);
} }
public static Connection getConnection() throws SQLException { public static Connection getConnection() throws SQLException {
return ds.getConnection(); return ds.getConnection();
} }
private DBCPDataSource(){} private DBCPDataSource(){}
} }

View File

@ -1,28 +1,28 @@
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;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
public class HikariCPDataSource { public class HikariCPDataSource {
private static final HikariConfig config = new HikariConfig(); private static final HikariConfig config = new HikariConfig();
private static final HikariDataSource ds; private static final HikariDataSource ds;
static { static {
config.setJdbcUrl("jdbc:h2:mem:test"); config.setJdbcUrl("jdbc:h2:mem:test");
config.setUsername("user"); config.setUsername("user");
config.setPassword("password"); config.setPassword("password");
config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250"); config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
ds = new HikariDataSource(config); ds = new HikariDataSource(config);
} }
public static Connection getConnection() throws SQLException { public static Connection getConnection() throws SQLException {
return ds.getConnection(); return ds.getConnection();
} }
private HikariCPDataSource(){} private HikariCPDataSource(){}
} }

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>

25
pom.xml
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>
@ -877,7 +878,7 @@
<module>spring-static-resources</module> <module>spring-static-resources</module>
<module>hazelcast</module> <module>hazelcast</module>
<module>hbase</module> <module>hbase</module>
<module>hystrix</module> <module>hystrix</module>
<module>image-processing</module> <module>image-processing</module>
<module>immutables</module> <module>immutables</module>
@ -906,11 +907,11 @@
<module>linkrest</module> <module>linkrest</module>
<module>logging-modules/log-mdc</module> <module>logging-modules/log-mdc</module>
<module>logging-modules/log4j</module> <module>logging-modules/log4j</module>
<module>logging-modules/logback</module> <module>logging-modules/logback</module>
<module>lombok</module> <module>lombok</module>
<module>mapstruct</module> <module>mapstruct</module>
<module>maven</module> <module>maven</module>
<module>mesos-marathon</module> <module>mesos-marathon</module>
<module>msf4j</module> <module>msf4j</module>
@ -962,25 +963,25 @@
<module>spring-cucumber</module> <module>spring-cucumber</module>
<module>spring-ejb</module> <module>spring-ejb</module>
<module>spring-aop</module> <module>spring-aop</module>
<module>persistence-modules/spring-data-dynamodb</module> <module>persistence-modules/spring-data-dynamodb</module>
<module>spring-data-keyvalue</module> <module>spring-data-keyvalue</module>
<module>spring-data-mongodb</module> <module>spring-data-mongodb</module>
<module>persistence-modules/spring-data-neo4j</module> <module>persistence-modules/spring-data-neo4j</module>
<module>spring-data-rest</module> <module>spring-data-rest</module>
<module>persistence-modules/spring-data-solr</module> <module>persistence-modules/spring-data-solr</module>
<module>spring-dispatcher-servlet</module> <module>spring-dispatcher-servlet</module>
<module>spring-exceptions</module> <module>spring-exceptions</module>
<module>spring-freemarker</module> <module>spring-freemarker</module>
<module>persistence-modules/spring-hibernate-3</module> <module>persistence-modules/spring-hibernate-3</module>
<module>persistence-modules/spring-hibernate-5</module> <module>persistence-modules/spring-hibernate-5</module>
<module>persistence-modules/spring-data-eclipselink</module> <module>persistence-modules/spring-data-eclipselink</module>
<module>spring-integration</module> <module>spring-integration</module>
<module>spring-jenkins-pipeline</module> <module>spring-jenkins-pipeline</module>
<module>spring-jersey</module> <module>spring-jersey</module>
<module>spring-jms</module> <module>spring-jms</module>
<module>spring-jooq</module> <module>spring-jooq</module>
<module>persistence-modules/spring-jpa</module> <module>persistence-modules/spring-jpa</module>
@ -1041,13 +1042,13 @@
<module>testing-modules/testing</module> <module>testing-modules/testing</module>
<module>testing-modules/testng</module> <module>testing-modules/testng</module>
<module>video-tutorials</module> <module>video-tutorials</module>
<module>xmlunit-2</module> <module>xmlunit-2</module>
<module>struts-2</module> <module>struts-2</module>
<module>apache-velocity</module> <module>apache-velocity</module>
<module>apache-solrj</module> <module>apache-solrj</module>
<module>rabbitmq</module> <module>rabbitmq</module>
<module>persistence-modules/spring-data-gemfire</module> <module>persistence-modules/spring-data-gemfire</module>
<module>mybatis</module> <module>mybatis</module>
<module>spring-drools</module> <module>spring-drools</module>
@ -1090,7 +1091,7 @@
<module>vertx</module> <module>vertx</module>
<module>metrics</module> <module>metrics</module>
<module>httpclient</module> <module>httpclient</module>
<module>jmeter</module> <module>jmeter</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>