1. Overview
1.概述
Connection pooling is a well-known data access pattern. Its main purpose is to reduce the overhead involved in performing database connections and read/write database operations.
连接池是一种著名的数据访问模式。它的主要目的是减少执行数据库连接和读/写数据库操作的开销。
At the most basic level, a connection pool is a database connection cache implementation that can be configured to suit specific requirements.
在最基本的层面上,连接池是一个数据库连接缓存的实现,可以根据具体要求进行配置。
In this tutorial, we’ll discuss a few popular connection pooling frameworks. Then we’ll learn how to implement our own connection pool from scratch.
在本教程中,我们将讨论几个流行的连接池框架。然后我们将学习如何从头开始实现我们自己的连接池。
2. Why Connection Pooling?
2.为什么要建立连接池?
Of course, this question is rhetorical.
当然,这个问题是修辞性的。
If we analyze the sequence of steps involved in a typical database connection life cycle, we’ll understand why:
如果我们分析一下一个典型的数据库连接生命周期所涉及的步骤顺序,我们就会明白为什么。
- Opening a connection to the database using the database driver
- Opening a TCP socket for reading/writing data
- Reading / writing data over the socket
- Closing the connection
- Closing the socket
It becomes evident that database connections are fairly expensive operations, and as such, should be reduced to a minimum in every possible use case (in edge cases, just avoided).
很明显,数据库连接是相当昂贵的操作,因此,在每个可能的用例中都应该将其减少到最低限度(在边缘情况下,只是避免)。
Here’s where connection pooling implementations come into play.
这里就是连接池实现发挥作用的地方。
By just simply implementing a database connection container, which allows us to reuse a number of existing connections, we can effectively save the cost of performing a huge number of expensive database trips. This boosts the overall performance of our database-driven applications.
通过简单地实现一个数据库连接容器,它允许我们重复使用一些现有的连接,我们可以有效地节省执行大量昂贵的数据库旅行的成本。这提升了我们数据库驱动的应用程序的整体性能。
3. JDBC Connection Pooling Frameworks
3.JDBC连接池框架
From a pragmatic perspective, implementing a connection pool from the ground up is pointless considering the number of “enterprise-ready” connection pooling frameworks already available.
从实用的角度来看,考虑到已经有很多 “企业就绪 “的连接池框架,从头开始实施连接池是没有意义的。
From a didactic perspective, which is the goal of this article, it’s not.
从说教的角度来看,也就是本文的目标,它不是。
Even so, before we learn how to implement a basic connection pool, we’ll first showcase a few popular connection pooling frameworks.
即便如此,在我们学习如何实现一个基本的连接池之前,我们首先要展示几个流行的连接池框架。
3.1. Apache Commons DBCP
3.1.Apache Commons DBCP
Let’s start with Apache Commons DBCP Component, a full-featured connection pooling JDBC framework:
让我们从Apache Commons DBCP Component开始,这是一个全功能的连接池的JDBC框架。
public class DBCPDataSource {
private static 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(){ }
}
In this case, we used a wrapper class with a static block to easily configure DBCP’s properties.
在这种情况下,我们用一个带有静态块的封装类来轻松配置DBCP的属性。
And here’s how to get a pooled connection with the DBCPDataSource class:
而这里是如何用DBCPDataSource类获得一个池化连接。
Connection con = DBCPDataSource.getConnection();
3.2. HikariCP
3.2.HikariCP
Now let’s look at HikariCP, a lightning-fast JDBC connection pooling framework created by Brett Wooldridge (for the full details on how to configure and get the most out of HikariCP, please check out this article):
现在让我们来看看HikariCP,这是由Brett Wooldridge创建的闪电式JDBC连接池框架(关于如何配置和充分利用HikariCP的全部细节,请查看本文)。
public class HikariCPDataSource {
private static HikariConfig config = new HikariConfig();
private static 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(){}
}
Similarly, here’s how to get a pooled connection with the HikariCPDataSource class:
同样,这里是如何用HikariCPDataSource类获得一个池化连接。
Connection con = HikariCPDataSource.getConnection();
3.3. C3P0
3.3.C3P0
Last in this review is C3P0, a powerful JDBC4 connection and statement pooling framework developed by Steve Waldman:
本评论的最后一篇是C3P0,这是一个强大的JDBC4连接和语句池框架,由Steve Waldman开发。
public class C3p0DataSource {
private static 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) {
// handle the exception
}
}
public static Connection getConnection() throws SQLException {
return cpds.getConnection();
}
private C3p0DataSource(){}
}
As expected, getting a pooled connection with the C3p0DataSource class is similar to the previous examples:
正如预期的那样,用C3p0DataSource类获得一个池化连接与之前的例子类似。
Connection con = C3p0DataSource.getConnection();
4. A Simple Implementation
4.一个简单的实现
To better understand the underlying logic of connection pooling, let’s create a simple implementation.
为了更好地理解连接池的基本逻辑,让我们创建一个简单的实现。
We’ll start out with a loosely coupled design based on just one single interface:
我们将从一个松散耦合的设计开始,只基于一个单一的接口。
public interface ConnectionPool {
Connection getConnection();
boolean releaseConnection(Connection connection);
String getUrl();
String getUser();
String getPassword();
}
The ConnectionPool interface defines the public API of a basic connection pool.
ConnectionPool接口定义了一个基本连接池的公共API。
Now let’s create an implementation that provides some basic functionality, including getting and releasing a pooled connection:
现在让我们创建一个实现,提供一些基本的功能,包括获取和释放一个池化连接。
public class BasicConnectionPool
implements ConnectionPool {
private String url;
private String user;
private String password;
private List<Connection> connectionPool;
private List<Connection> usedConnections = new ArrayList<>();
private static int INITIAL_POOL_SIZE = 10;
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);
}
// standard constructors
@Override
public Connection getConnection() {
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);
}
public int getSize() {
return connectionPool.size() + usedConnections.size();
}
// standard getters
}
While pretty naive, the BasicConnectionPool class provides the minimal functionality that we’d expect from a typical connection pooling implementation.
虽然很幼稚,但BasicConnectionPool类提供了我们期望从典型的连接池实现中获得的最小功能。
In a nutshell, the class initializes a connection pool based on an ArrayList that stores 10 connections, which can be easily reused.
简而言之,该类基于一个ArrayList初始化了一个连接池,该连接池存储了10个连接,可以很容易地重复使用。
It’s also possible to create JDBC connections with the DriverManager class and Datasource implementations.
还可以通过DriverManager类和Datasource实现来创建JDBC连接。
As it’s much better to keep the creation of connection databases agnostic, we used the former within the create() static factory method.
由于保持连接数据库的创建不可知要好得多,我们在create()静态工厂方法中使用了前者。
In this case, we placed the method within the BasicConnectionPool because this is the only implementation of the interface.
在这种情况下,我们将该方法放在BasicConnectionPool中,因为这是该接口的唯一实现。
In a more complex design, with multiple ConnectionPool implementations, it would be preferable to place it in the interface, thus getting a more flexible design and greater level of cohesion.
在更复杂的设计中,如果有多个ConnectionPool实现,最好把它放在接口中,从而得到更灵活的设计和更大程度的内聚力。
The most relevant point to stress here is that once the pool is created, connections are fetched from the pool, so there’s no need to create new ones.
这里要强调的最相关的一点是,一旦池子被创建,连接就会从池子中获取,所以不需要创建新的连接。
Furthermore, when a connection is released, it’s actually returned back to the pool, so other clients can reuse it.
此外,当一个连接被释放时,它实际上被送回池中,所以其他客户可以重新使用它。
There’s no further interaction with the underlying database, such as an explicit call to the Connection’s close() method.
没有与底层数据库的进一步互动,例如明确调用Connection的close()方法。
5. Using the BasicConnectionPool Class
5.使用BasicConnectionPool类
As expected, using our BasicConnectionPool class is straightforward.
正如预期的那样,使用我们的BasicConnectionPool类是很简单的。
Let’s create a simple unit test and get a pooled in-memory H2 connection:
让我们创建一个简单的单元测试,并获得一个池式内存H2连接。
@Test
public whenCalledgetConnection_thenCorrect() {
ConnectionPool connectionPool = BasicConnectionPool
.create("jdbc:h2:mem:test", "user", "password");
assertTrue(connectionPool.getConnection().isValid(1));
}
6. Further Improvements and Refactoring
6.进一步的改进和重构
Of course, there’s plenty of room to tweak/extend the current functionality of our connection pooling implementation.
当然,我们还有很多空间来调整/扩展我们连接池实现的现有功能。
For instance, we could refactor the getConnection() method and add support for maximum pool size. If all available connections are taken, and the current pool size is less than the configured maximum, the method will create a new connection.
例如,我们可以重构getConnection()方法并添加对最大池大小的支持。如果所有可用的连接都被占用了,并且当前池的大小小于配置的最大值,该方法将创建一个新的连接。
We could also verify whether the connection obtained from the pool is still alive before passing it to the client:
我们也可以在把连接传给客户端之前,验证从池子里获得的连接是否仍然活着。
@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);
if(!connection.isValid(MAX_TIMEOUT)){
connection = createConnection(url, user, password);
}
usedConnections.add(connection);
return connection;
}
Note that the method now throws SQLException, meaning we’ll have to update the interface signature as well.
请注意,该方法现在抛出SQLException,这意味着我们也必须更新接口签名。
Or we could add a method to gracefully shut down our connection pool instance:
或者我们可以添加一个方法来优雅地关闭我们的连接池实例。
public void shutdown() throws SQLException {
usedConnections.forEach(this::releaseConnection);
for (Connection c : connectionPool) {
c.close();
}
connectionPool.clear();
}
In production-ready implementations, a connection pool should provide a bunch of extra features, such as the ability to track the connections that are currently in use, support for prepared statement pooling, and so forth.
在生产就绪的实现中,连接池应该提供一堆额外的功能,如跟踪当前正在使用的连接的能力,支持准备好的语句池,等等。
In order to keep this article simple, we’ll omit how to implement these additional features, and keep the implementation non-thread-safe for the sake of clarity.
为了保持本文的简洁,我们将省略如何实现这些额外的功能,并且为了清晰起见,保持实现的非线程安全。
7. Conclusion
7.结论
In this article, we took an in-depth look at what connection pooling is, and learned how to roll our own connection pooling implementation.
在这篇文章中,我们深入了解了什么是连接池,并学习了如何推出我们自己的连接池实现。
Of course, we don’t have to start from scratch every time we want to add a full-featured connection pooling layer to our applications.
当然,我们不必每次都从头开始,为我们的应用程序添加一个全功能的连接池层。
That’s why we started by exploring some of the most popular connection pool frameworks, so we have a clear idea of how to work with them and pick the one that best suits our requirements.
这就是为什么我们开始探索一些最流行的连接池框架,所以我们对如何使用它们有一个清晰的概念,并挑选一个最适合我们要求的框架。
As usual, all the code samples shown in this article are available over on GitHub.
像往常一样,本文中显示的所有代码样本都可以在GitHub上找到。