Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 85 additions & 60 deletions patterns/design-patterns/pom.xml
Original file line number Diff line number Diff line change
@@ -1,60 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<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</groupId>
<artifactId>design-patterns</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>patterns-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>

<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${javaee.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate-core.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector.version}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.grep4j</groupId>
<artifactId>grep4j</artifactId>
<version>${grep4j.version}</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<grep4j.version>1.8.7</grep4j.version>
<assertj-core.version>3.9.1</assertj-core.version>
<javaee.version>8.0</javaee.version>
<hibernate-core.version>5.2.16.Final</hibernate-core.version>
<mysql-connector.version>6.0.6</mysql-connector.version>
</properties>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<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.design-patterns</groupId>
<artifactId>design-patterns</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>patterns-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.16.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.googlecode.grep4j</groupId>
<artifactId>grep4j</artifactId>
<version>1.8.7</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.baeldung.connectionpool.application;

import com.baeldung.connectionpool.connectionpools.BasicConnectionPool;
import com.baeldung.connectionpool.connectionpools.C3poDataSource;
import com.baeldung.connectionpool.connectionpools.ConnectionPool;
import com.baeldung.connectionpool.connectionpools.DBCPDataSource;
import com.baeldung.connectionpool.connectionpools.HirakiDataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class Application {

public static void main(String[] args) throws SQLException {
ConnectionPool cp1 = BasicConnectionPool.createFromDriverManager("jdbc:mysql://localhost:3306/mydb", "user", "password");
Connection con1 = cp1.getConnection(0);
System.out.println(con1);
cp1.releaseConnection(con1);

Connection con3 = DBCPDataSource.getConnection();
System.out.println(con3);

Connection con4 = C3poDataSource.getConnection();
System.out.println(con4);

Connection con5 = HirakiDataSource.getConnection();
System.out.println(con5);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.baeldung.connectionpool.connectionpools;

import com.mysql.cj.jdbc.MysqlDataSource;
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 static final int MAX_CONNECTIONS = 10;

public static BasicConnectionPool createFromDriverManager(String url, String user, String password) throws SQLException {
List<Connection> pool = new ArrayList<>(MAX_CONNECTIONS);
for (int i = 0; i < MAX_CONNECTIONS; i++) {
pool.add(DriverManager.getConnection(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(int id) {
if (id < 0 || id > MAX_CONNECTIONS) {
throw new RuntimeException();
}
return connectionPool.get(id);
}

@Override
public void releaseConnection(Connection connection) {
connectionPool.add(connection);
}

@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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.baeldung.connectionpool.connectionpools;

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("com.mysql.jdbc.Driver");
cpds.setJdbcUrl("jdbc:mysql://localhost:3306/mydb");
cpds.setUser("user");
cpds.setPassword("password");
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}

public static Connection getConnection() throws SQLException {
return cpds.getConnection();
}

private C3poDataSource(){}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.baeldung.connectionpool.connectionpools;

import java.sql.Connection;
import java.util.List;

public interface ConnectionPool {

Connection getConnection(int id);

void releaseConnection(Connection connection);

List<Connection> getConnectionPool();

String getUrl();

String getUser();

String getPassword();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.baeldung.connectionpool.connectionpools;

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:mysql://localhost:3306/mydb");
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(){}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.baeldung.connectionpool.connectionpools;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class HirakiDataSource {

private static final HikariConfig config = new HikariConfig();
private static final HikariDataSource ds;

static {
config.setJdbcUrl("jdbc:mysql://localhost:3306/mydb");
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 HirakiDataSource(){}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.baeldung.connectionpool.test;

import com.baeldung.connectionpool.connectionpools.BasicConnectionPool;
import com.baeldung.connectionpool.connectionpools.ConnectionPool;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.BeforeClass;
import org.junit.Test;

public class BasicConnectionPoolIntegrationtTest {

private static ConnectionPool connectionPool;


@BeforeClass
public static void setUpBasicConnectionPoolInstance() throws SQLException {
connectionPool = BasicConnectionPool.createFromDriverManager("jdbc:mysql://localhost:3306/mydb", "user", "password");
}

@Test
public void givenBasicConnectionPoolInstance_whenCalledgetConnection_thenCorrect() {
assertThat(connectionPool.getConnection(1)).isInstanceOf(Connection.class);
}

@Test
public void givenBasicConnectionPoolInstance_whenCalledgetConnectionPool_thenCorrect() {
assertThat(connectionPool.getConnectionPool()).isInstanceOf(List.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.connectionpool.test;

import com.baeldung.connectionpool.connectionpools.C3poDataSource;
import java.sql.Connection;
import java.sql.SQLException;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;

public class C3poDataSourceUnitTest {

@Test
public void givenC3poDataSourceClass_whenCalledgetConnection_thenCorrect() throws SQLException {
assertThat(C3poDataSource.getConnection()).isInstanceOf(Connection.class);
}
}
Loading