Skip to content

Commit e5c785d

Browse files
Connection pool examples
1 parent e13e1ac commit e5c785d

File tree

6 files changed

+236
-0
lines changed

6 files changed

+236
-0
lines changed

online-store.persistence/pom.xml

+20
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@
1919
<artifactId>mysql-connector-java</artifactId>
2020
<version>8.0.28</version>
2121
</dependency>
22+
23+
<dependency>
24+
<groupId>org.apache.commons</groupId>
25+
<artifactId>commons-dbcp2</artifactId>
26+
<version>2.9.0</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>c3p0</groupId>
31+
<artifactId>c3p0</artifactId>
32+
<version>0.9.1.2</version>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>com.zaxxer</groupId>
37+
<artifactId>HikariCP</artifactId>
38+
<version>5.0.1</version>
39+
</dependency>
40+
41+
2242
</dependencies>
2343

2444

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.itbulls.learnit.onlinestore.persistence.utils.connectionpools;
2+
3+
import java.beans.PropertyVetoException;
4+
import java.sql.Connection;
5+
import java.sql.SQLException;
6+
7+
import org.apache.commons.dbcp2.BasicDataSource;
8+
9+
import com.mchange.v2.c3p0.ComboPooledDataSource;
10+
11+
public class C3p0Demo {
12+
13+
private static ComboPooledDataSource cpds = new ComboPooledDataSource();
14+
15+
static {
16+
try {
17+
cpds.setDriverClass("com.mysql.cj.jdbc.Driver");
18+
cpds.setJdbcUrl("jdbc:mysql://localhost:3306/learn_it_db");
19+
cpds.setUser("root");
20+
cpds.setPassword("root");
21+
cpds.setMaxIdleTime(100);
22+
cpds.setMaxStatementsPerConnection(10);
23+
} catch (PropertyVetoException e) {
24+
// handle the exception
25+
}
26+
}
27+
28+
public static Connection getConnection() throws SQLException {
29+
return cpds.getConnection();
30+
}
31+
32+
public static void main(String[] args) throws SQLException {
33+
Connection conn = getConnection();
34+
try (conn) {
35+
System.out.println(conn);
36+
}
37+
System.out.println(conn);
38+
}
39+
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.itbulls.learnit.onlinestore.persistence.utils.connectionpools;
2+
3+
import java.sql.Connection;
4+
import java.sql.SQLException;
5+
6+
import org.apache.commons.dbcp2.BasicDataSource;
7+
8+
public class DbcpDemo {
9+
10+
private static BasicDataSource ds = new BasicDataSource();
11+
12+
static {
13+
ds.setUrl("jdbc:mysql://localhost:3306/learn_it_db");
14+
ds.setUsername("root");
15+
ds.setPassword("root");
16+
ds.setMinIdle(3);
17+
ds.setTimeBetweenEvictionRunsMillis(1000);
18+
ds.setMaxIdle(20);
19+
ds.setMaxOpenPreparedStatements(200);
20+
}
21+
22+
public static Connection getConnection() throws SQLException {
23+
return ds.getConnection();
24+
}
25+
26+
public static void main(String[] args) throws SQLException {
27+
Connection conn = getConnection();
28+
try (conn) {
29+
System.out.println(conn);
30+
}
31+
System.out.println(conn);
32+
}
33+
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.itbulls.learnit.onlinestore.persistence.utils.connectionpools;
2+
3+
import java.sql.Connection;
4+
import java.sql.SQLException;
5+
6+
import org.apache.commons.dbcp2.BasicDataSource;
7+
8+
import com.zaxxer.hikari.HikariConfig;
9+
import com.zaxxer.hikari.HikariDataSource;
10+
11+
public class HikaricpDemo {
12+
13+
private static HikariConfig config = new HikariConfig();
14+
private static HikariDataSource ds;
15+
16+
static {
17+
config.setJdbcUrl("jdbc:mysql://localhost:3306/learn_it_db");
18+
config.setUsername("root");
19+
config.setPassword("root");
20+
config.addDataSourceProperty("cachePrepStmts", "true");
21+
config.addDataSourceProperty("prepStmtCacheSize", "100");
22+
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
23+
ds = new HikariDataSource(config);
24+
}
25+
26+
public static Connection getConnection() throws SQLException {
27+
return ds.getConnection();
28+
}
29+
30+
public static void main(String[] args) throws SQLException {
31+
Connection conn = getConnection();
32+
try (conn) {
33+
System.out.println(conn);
34+
}
35+
System.out.println(conn);
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.itbulls.learnit.onlinestore.web.servlets;
2+
3+
import jakarta.servlet.http.HttpServlet;
4+
import java.io.IOException;
5+
import java.sql.Connection;
6+
import java.sql.SQLException;
7+
8+
import javax.naming.Context;
9+
import javax.naming.InitialContext;
10+
import javax.naming.NamingException;
11+
12+
import javax.sql.DataSource;
13+
14+
import jakarta.annotation.Resource;
15+
import jakarta.servlet.ServletException;
16+
import jakarta.servlet.annotation.WebServlet;
17+
import jakarta.servlet.http.HttpServletRequest;
18+
import jakarta.servlet.http.HttpServletResponse;
19+
20+
@WebServlet("/cp-demo")
21+
public class ConnectionPoolDemoServlet extends HttpServlet {
22+
23+
private DataSource ds1;
24+
25+
@Resource(name = "jdbc/connpool")
26+
private DataSource ds2;
27+
28+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
29+
throws ServletException, IOException {
30+
31+
InitialContext initialContext;
32+
try {
33+
initialContext = new InitialContext();
34+
Context context = (Context) initialContext.lookup("java:comp/env");
35+
DataSource ds1 = (DataSource) context.lookup("jdbc/connpool");
36+
37+
Connection conn1 = getConnection(ds1);
38+
Connection conn2 = getConnection(ds2);
39+
40+
try (conn1; conn2) {
41+
System.out.println("Connection 1 from DS1:\t" + conn1);
42+
System.out.println("Connection 2 from DS2:\t" + conn2);
43+
}
44+
45+
System.out.println("Connection 1 from DS1 after closure:\t" + conn1);
46+
System.out.println("Connection 2 from DS2 after closure:\t" + conn2);
47+
48+
System.out.println("DS1 equals DS2:\t" + ds1.equals(ds2));
49+
} catch (NamingException | SQLException e) {
50+
e.printStackTrace();
51+
}
52+
}
53+
54+
private Connection getConnection(DataSource ds) throws SQLException {
55+
return ds.getConnection();
56+
}
57+
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<context>
3+
4+
5+
<Resource name="jdbc/connpool"
6+
auth="Container"
7+
type="javax.sql.DataSource"
8+
maxActive="5"
9+
maxIdle="5"
10+
maxWait="10000"
11+
maxAge="10000"
12+
username="root"
13+
password="root"
14+
driverClassName="com.mysql.cj.jdbc.Driver"
15+
url="jdbc:mysql://localhost:3306/learn_it_db"/>
16+
17+
<!-- Description of all other attributes - https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html -->
18+
19+
<!-- maxActive: Maximum number of database connections in pool. Make sure you
20+
configure your mysqld max_connections large enough to handle
21+
all of your db connections. Set to -1 for no limit.
22+
-->
23+
24+
<!-- maxIdle: Maximum number of idle database connections to retain in pool.
25+
Set to -1 for no limit. See also the DBCP documentation on this
26+
and the minEvictableIdleTimeMillis configuration parameter.
27+
-->
28+
29+
<!-- maxWait: Maximum time to wait for a database connection to become available
30+
in ms, in this example 10 seconds. An Exception is thrown if
31+
this timeout is exceeded. Set to -1 to wait indefinitely.
32+
-->
33+
34+
<!-- username and password: MySQL username and password for database connections -->
35+
36+
<!-- driverClassName: Class name for the old mm.mysql JDBC driver is
37+
org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
38+
Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
39+
-->
40+
41+
<!-- url: The JDBC connection url for connecting to your MySQL database.
42+
-->
43+
44+
</context>

0 commit comments

Comments
 (0)