Skip to content

Connection pooling does not appear to be correctly supported #1254

@robertandrewbain

Description

@robertandrewbain

Using database connection pooling with org.apache.commons.dbcp2.BasicDataSource, upon executing a SQLQuery the connection is never returned to the pool, unless you put it inside a transaction, which is not always convenient. The following workaround manually closes the connection, returning it to the pool.

protected Connection getSQLQueryConnection(@NonNull SQLQuery query) {
    Field declaredField;
    try {
        declaredField = SQLQuery.class.getSuperclass().getDeclaredField("conn");
        declaredField.setAccessible(true);
        return (Connection) declaredField.get(query);
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        log.error("Could not retrieve connection object from Object {}", query);
    }

    return null;
}

protected void closeSQLQuery(@NonNull SQLQuery query) {
    try {
        if (!TransactionSynchronizationManager.isActualTransactionActive()) {
            getSQLQueryConnection(query).close();
        } else {
            log.warn("Not closing connection on transaction wrapped connection {}", query);
        }
    } catch (SQLException e) {
        log.error("Could not close connection {}", query, e);
    }
}

The most notable drawback with this mechanism is the fact that it relies on the programmer knowing that this must be called, otherwise the connection pool will soon run out of available connections.

It would be far better if querydsl could provide support for connection pooling, ensuring that connections are returned to the pool upon execution finishing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions