In part 3 of this tutorial series, you’ll learn what a database connection pool is and how to use it. Connection pools are a must in web applications that require handling simultaneous requests that consume a database. We’ll continue where the previous step of the tutorial left off. Refer to and of this tutorial for more details. The source code is available on . Note: part 1 part 2 GitHub Here’s a video version of this article, in case you want to see the concepts in action: What Is a Connection Pool? A pool (or pooling) is a software component that maintains a set of pre-configured resources. Clients of a pool (Java methods, classes, or in general, other software components) can “borrow” a resource, use it, and return it to the pool so that other clients can use it. In our case, the resources are database connections, hence the name . This kind of pool keeps database connections ready to use, that is, JDBC objects. Typically, Java threads request a object from the pool, perform CRUD operations, and close the connection, effectively returning it to the pool. In fact, threads are another common resource that uses pools. If you want to see this in action, I recorded a video about it: database connection pool Connection Connection Adding HikariCP to the Project In this tutorial, we’ll use the popular implementation. Here’s the dependency to add to the file: HikariCP pom.xml <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>5.0.0</version> </dependency> Add an SLF4J binding if you want to see HikariCP logs. SLF4J is a logging facade used by library vendors to allow developers to decide which logging framework to use in their applications. In this example, we’ll use the binding, but you should a more suitable binding in real-world applications. Here’s the snippet for the file: slf4j-simple pick pom.xml <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.8.0-beta4</version> </dependency> If you use a database, you don’t need to add HikariCP to use connection pools. Instead, you can use the class. I created an that uses it. Note: MariaDB MariaDbPoolDataSource example app Initializing the Connection Pool In part 1 of this tutorial, we created the method to initialize a static object. We won’t be using this object anymore. Instead, we’ll get a object from the pool whenever we need it. Therefore, instead of initializing a connection, we need to initialize a connection pool. Let’s start by removing the field in the class and adding a connection pool instead: initDatabaseConnection() Connection Connection Connection Application public class Application { private static HikariDataSource dataSource; ... } For clarity, let’s rename the method to . There, we can set the database connection details directly in Java for now: initDatabaseConnection() initDatabaseConnectionPool() private static void initDatabaseConnectionPool() { dataSource = new HikariDataSource(); dataSource.setJdbcUrl("jdbcUrl=jdbc:mariadb://localhost:3306/jdbc_demo"); dataSource.setUsername("user"); dataSource.setPassword("pasword"); } Similarly, the should be now . Implementing this method is straightforward: closeDatabaseConnection() initDatabaseConnectionPool() private static void closeDatabaseConnectionPool() { dataSource.close(); } Using the Connection Pool Previously, we declared a static field to store a single shared object in the class. We don’t have this instance object anymore. Instead, we need to ask the connection pool (the object) for a . Here’s how the code would look like in the method: Connection Application dataSource Connection createData(String, int) private static void createData(String name, int rating) throws SQLException { try (Connection connection = dataSource.getConnection()) { try (PreparedStatement statement = connection.prepareStatement(""" INSERT INTO programming_language(name, rating) VALUES (?, ?) """)) { statement.setString(1, name); statement.setInt(2, rating); int rowsInserted = statement.executeUpdate(); System.out.println("Rows inserted: " + rowsInserted); } } } We are using a try-with-resources block to call , which returns one of the objects in the pool, if available. All the other CRUD methods should get a object in the same fashion. We won’t list the code here, but you can use the same pattern in every CRUD method. dataSource.getConnection() Connection Connection Configuring the Database Connection in a Properties File At this point, we have the database connection details hardcoded in Java. However, HikariCP supports properties files for its configuration. For this, you need a object that contains the name of the properties file to load: HikariConfig private static void initDatabaseConnectionPool() { HikariConfig hikariConfig = new HikariConfig("/database.properties"); dataSource = new HikariDataSource(hikariConfig); } The should be placed in the directory. Don’t forget to add a character in the string that references the file in Java. Here’s the content of the configuration file: database.properties src/main/resources/ / jdbcUrl=jdbc:mariadb://localhost:3306/jdbc_demo dataSource.username=user dataSource.password=password Connection pools are configurable, and different implementations have different parameters that you can adjust. For example, you might want to configure the maximum number of connection objects that the pool maintains or the maximum lifetime of a connection in the pool. Refer to the pool implementation for more information. documentation Also Published Here