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:
committed by
Predrag Maric
parent
25fb0c94c2
commit
fc02400f5f
@@ -0,0 +1,92 @@
|
||||
package com.baeldung.connectionpool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BasicConnectionPool implements ConnectionPool {
|
||||
|
||||
private final String url;
|
||||
private final String user;
|
||||
private final String password;
|
||||
private final List<Connection> connectionPool;
|
||||
private final List<Connection> usedConnections = new ArrayList<>();
|
||||
private static final int INITIAL_POOL_SIZE = 10;
|
||||
private final int MAX_POOL_SIZE = 20;
|
||||
|
||||
public static BasicConnectionPool create(String url, String user, String password) throws SQLException {
|
||||
List<Connection> pool = new ArrayList<>(INITIAL_POOL_SIZE);
|
||||
for (int i = 0; i < INITIAL_POOL_SIZE; i++) {
|
||||
pool.add(createConnection(url, user, password));
|
||||
}
|
||||
return new BasicConnectionPool(url, user, password, pool);
|
||||
}
|
||||
|
||||
private BasicConnectionPool(String url, String user, String password, List<Connection> connectionPool) {
|
||||
this.url = url;
|
||||
this.user = user;
|
||||
this.password = password;
|
||||
this.connectionPool = connectionPool;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
if (connectionPool.isEmpty()) {
|
||||
if (usedConnections.size() < MAX_POOL_SIZE) {
|
||||
connectionPool.add(createConnection(url, user, password));
|
||||
} else {
|
||||
throw new RuntimeException("Maximum pool size reached, no available connections!");
|
||||
}
|
||||
}
|
||||
|
||||
Connection connection = connectionPool.remove(connectionPool.size() - 1);
|
||||
usedConnections.add(connection);
|
||||
return connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean releaseConnection(Connection connection) {
|
||||
connectionPool.add(connection);
|
||||
return usedConnections.remove(connection);
|
||||
}
|
||||
|
||||
private static Connection createConnection(String url, String user, String password) throws SQLException {
|
||||
return DriverManager.getConnection(url, user, password);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
return connectionPool.size() + usedConnections.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Connection> getConnectionPool() {
|
||||
return connectionPool;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() throws SQLException {
|
||||
usedConnections.forEach(this::releaseConnection);
|
||||
for (Connection c : connectionPool) {
|
||||
c.close();
|
||||
}
|
||||
connectionPool.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.connectionpool;
|
||||
|
||||
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||
import java.beans.PropertyVetoException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class C3poDataSource {
|
||||
|
||||
private static final ComboPooledDataSource cpds = new ComboPooledDataSource();
|
||||
|
||||
static {
|
||||
try {
|
||||
cpds.setDriverClass("org.h2.Driver");
|
||||
cpds.setJdbcUrl("jdbc:h2:mem:test");
|
||||
cpds.setUser("user");
|
||||
cpds.setPassword("password");
|
||||
} catch (PropertyVetoException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static Connection getConnection() throws SQLException {
|
||||
return cpds.getConnection();
|
||||
}
|
||||
|
||||
private C3poDataSource(){}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.connectionpool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
public interface ConnectionPool {
|
||||
|
||||
Connection getConnection() throws SQLException;
|
||||
|
||||
boolean releaseConnection(Connection connection);
|
||||
|
||||
List<Connection> getConnectionPool();
|
||||
|
||||
int getSize();
|
||||
|
||||
String getUrl();
|
||||
|
||||
String getUser();
|
||||
|
||||
String getPassword();
|
||||
|
||||
void shutdown() throws SQLException;;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.connectionpool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import org.apache.commons.dbcp2.BasicDataSource;
|
||||
|
||||
public class DBCPDataSource {
|
||||
|
||||
private static final BasicDataSource ds = new BasicDataSource();
|
||||
|
||||
static {
|
||||
ds.setUrl("jdbc:h2:mem:test");
|
||||
ds.setUsername("user");
|
||||
ds.setPassword("password");
|
||||
ds.setMinIdle(5);
|
||||
ds.setMaxIdle(10);
|
||||
ds.setMaxOpenPreparedStatements(100);
|
||||
}
|
||||
|
||||
public static Connection getConnection() throws SQLException {
|
||||
return ds.getConnection();
|
||||
}
|
||||
|
||||
private DBCPDataSource(){}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.connectionpool;
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class HikariCPDataSource {
|
||||
|
||||
private static final HikariConfig config = new HikariConfig();
|
||||
private static final HikariDataSource ds;
|
||||
|
||||
static {
|
||||
config.setJdbcUrl("jdbc:h2:mem:test");
|
||||
config.setUsername("user");
|
||||
config.setPassword("password");
|
||||
config.addDataSourceProperty("cachePrepStmts", "true");
|
||||
config.addDataSourceProperty("prepStmtCacheSize", "250");
|
||||
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
||||
ds = new HikariDataSource(config);
|
||||
}
|
||||
|
||||
public static Connection getConnection() throws SQLException {
|
||||
return ds.getConnection();
|
||||
}
|
||||
|
||||
private HikariCPDataSource(){}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.connectionpool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class BasicConnectionPoolUnitTest {
|
||||
|
||||
private static ConnectionPool connectionPool;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpBasicConnectionPoolInstance() throws SQLException {
|
||||
connectionPool = BasicConnectionPool.create("jdbc:h2:mem:test", "user", "password");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBasicConnectionPoolInstance_whenCalledgetConnection_thenCorrect() throws Exception {
|
||||
assertTrue(connectionPool.getConnection().isValid(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBasicConnectionPoolInstance_whenCalledreleaseConnection_thenCorrect() throws Exception {
|
||||
Connection connection = connectionPool.getConnection();
|
||||
assertThat(connectionPool.releaseConnection(connection)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBasicConnectionPoolInstance_whenCalledgetUrl_thenCorrect() {
|
||||
assertThat(connectionPool.getUrl()).isEqualTo("jdbc:h2:mem:test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBasicConnectionPoolInstance_whenCalledgetUser_thenCorrect() {
|
||||
assertThat(connectionPool.getUser()).isEqualTo("user");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBasicConnectionPoolInstance_whenCalledgetPassword_thenCorrect() {
|
||||
assertThat(connectionPool.getPassword()).isEqualTo("password");
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void givenBasicConnectionPoolInstance_whenAskedForMoreThanMax_thenError() throws Exception {
|
||||
// this test needs to be independent so it doesn't share the same connection pool as other tests
|
||||
ConnectionPool cp = BasicConnectionPool.create("jdbc:h2:mem:test", "user", "password");
|
||||
final int MAX_POOL_SIZE = 20;
|
||||
for (int i = 0; i < MAX_POOL_SIZE + 1; i++) {
|
||||
cp.getConnection();
|
||||
}
|
||||
fail();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBasicConnectionPoolInstance_whenSutdown_thenEmpty() throws Exception {
|
||||
ConnectionPool cp = BasicConnectionPool.create("jdbc:h2:mem:test", "user", "password");
|
||||
assertThat(((BasicConnectionPool)cp).getSize()).isEqualTo(10);
|
||||
|
||||
((BasicConnectionPool) cp).shutdown();
|
||||
assertThat(((BasicConnectionPool)cp).getSize()).isEqualTo(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.connectionpool;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
public class C3poDataSourceUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenC3poDataSourceClass_whenCalledgetConnection_thenCorrect() throws SQLException {
|
||||
assertTrue(C3poDataSource.getConnection().isValid(1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.connectionpool;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DBCPDataSourceUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDBCPDataSourceClass_whenCalledgetConnection_thenCorrect() throws SQLException {
|
||||
assertTrue(DBCPDataSource.getConnection().isValid(1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.connectionpool;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HikariCPDataSourceUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenHikariDataSourceClass_whenCalledgetConnection_thenCorrect() throws SQLException {
|
||||
assertTrue(HikariCPDataSource.getConnection().isValid(1));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user