Вход на сайт

Просмотр новости

Найдите то, что Вас интересует

What is a database connection pool?

Дата публикации: 14-01-2022 11:49:00

An explanation of database connection pools and how to use them in Java applications.
What is a database connection pool? appeared first on MariaDB.org


Основное содержимое страницы с новостью.

A database connection pool stores ready-to-use database connections that threads can borrow when they need them, and return when they finish the work with the database. This improves performance in terms of memory and processing consumption, especially in web applications such as websites and REST web services. The technique is also known as pooling. Here’s how to use it in Java apps.

HikariCP is one, if not the most, popular JDBC connection pool. You can add it to your project using Maven (check the latest version):

<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>LATEST</version>
</dependency>

The connection pool can be configured programmatically or through a configuration file (src/main/resources/database.properties):

jdbcUrl=jdbc:mariadb://localhost:3306/some_database
dataSource.username=the_database_user
dataSource.password=the_password_for_that_user

There are more properties available, like the size of the pool, whether to use auto-commit or not, timeouts, etc. Check the documentation for details.

Now you can create a JDBC DataSource object from which to get Statement or PreparedStatement objects to execute SQL statements:

HikariConfig hikariConfig = new HikariConfig("/database.properties");
HikariDataSource dataSource = new HikariDataSource(hikariConfig);
try (Connection connection = dataSource.getConnection()) {
    // ... run SQL queries here ...
}

If you don’t use a try-with-resources block, remember to return the connection object to the pool by calling connection.close() preferably in a finally block:

try {
    // ... code ...
} finally {
    dataSource.close();
}

See JDBC Tutorial Part 3: Using database connection pools, for a more detailed tutorial or watch me coding an example Java application using Connector/J with a MariaDB database:

Enjoyed this post? I can help your team implement similar solutions—contact me to learn more.

Схожие новости

#Наименование новостиТональностьИнформативностьДата публикации
1How to open and close JDBC connections04.7612-01-2022
2How to execute SQL queries from Java (and prevent SQL injections)03.4812-01-2022
3What is MariaDB?07.6621-09-2023
4Why MariaDB instead of MySQL, PostgreSQL, or MongoDB?05.8525-02-2023
5Why do we need databases and SQL?05.6306-03-2024
6New book (coming) – MariaDB for Developers07.0631-12-2023
7New YouTube channel on programming (mostly Java)04.1304-01-2022
8How to start a web server using Java03.7623-03-2022
9What is JPA?09.1807-02-2022
10High availability and resiliency in databases with MaxScale03.7926-03-2024

Классификация: . Схожих патентов: 0. Схожих новостей: 10. Тональность: 0. Информативность: 6.1. Источник: mariadb.org.