Skip to content

@ExpectNoConnectionLeak

Jean Bisutti edited this page Nov 10, 2021 · 2 revisions

ExpectNoConnectionLeak annotation allows detecting database connection leaks.

The test method will fail if the tested code gets a connection from javax.sql.DataSource but does not close it.

We recommend attributing a global scope to this annotation. You can look at the other suggested SQL global scope annotations here.

See also @ProfileConnection.

πŸ”Ž Example

    @ExpectNoConnectionLeak
    @Test
    public void code_with_connection_leak() throws SQLException {
        Connection connection = getConnection();
        try (PreparedStatement statement = connection.prepareStatement("select isbn from Book");) {
            statement.executeQuery();
        }
    }

The test fails will the following message on the console: [PERF] Database connection leak.

The test does not fail with this code:

    @ExpectNoConnectionLeak
    @Test
    public void code_without_connection_leak() throws SQLException {
        try (Connection connection = getConnection();
             PreparedStatement statement = connection.prepareStatement("select isbn from Book");) {
            statement.executeQuery();
        }
    }

Annotations

πŸ‘‰ Β Core

πŸ‘‰ Β JVM

πŸ‘‰ Β SQL

πŸ‘‰ Β Scopes

πŸ‘‰ Β Create an annotation

Supported frameworks

πŸ‘‰ Β JUnit 4

πŸ‘‰ Β JUnit 5

πŸ‘‰ Β TestNG

πŸ‘‰ Β Spring

How to

πŸ‘‰ Β Detect and fix N+1 SELECT

Project examples

πŸ‘‰ Β Maven performance

πŸ‘‰ Β Spring Boot - JUnit 4

πŸ‘‰ Β Spring Boot - JUnit 5

πŸ‘‰ Β Micronaut Data - JUnit 5

πŸ‘‰ Β Micronaut - Spring - JUnit 5

πŸ‘‰ Β Quarkus - JUnit 5

Miscellaneous

πŸ‘‰ Β FAQ

πŸ‘‰ Β QuickPerf code

Clone this wiki locally