ComenzarEmpieza gratis

Connection Pooling

Opening a new database connection for every request is slow and resource-intensive. Connection pooling solves this by reusing a pool of open connections. HikariCP is a popular, high-performance connection pool for Java applications.

Set up HikariCP for CityBook Libraries to improve their application's performance. HikariConfig, HikariDataSource, and database credentials are already imported for you.

Este ejercicio forma parte del curso

Querying a PostgreSQL Database in Java

Ver curso

Instrucciones del ejercicio

  • Configure HikariConfig with the database URL, username, and password.
  • Create a HikariDataSource from the configuration.
  • Get a connection from the pool.

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

public class Main {
    public static void main(String[] args) {
        // Configure HikariCP with credentials
        HikariConfig config = new HikariConfig();
        config.____(Credentials.URL);
        config.____(Credentials.USER);
        config.____(Credentials.PASSWORD);

        // Create the connection pool
        HikariDataSource ds = new ____(config);

        // Get a connection from the pool
        try (Connection conn = ____.____()) {
            System.out.println("Connected with HikariCP!");
        } catch (SQLException e) {
            System.out.println("Error: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
        }
    }
}
Editar y ejecutar código