Skip to content

[4.x] Closeable jdbc pool #8571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
* Copyright (c) 2019, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,7 +31,7 @@
/**
* Helidon database client.
*/
public interface DbClient {
public interface DbClient extends AutoCloseable {

/**
* Qualifier used for mapping using {@link io.helidon.common.mapper.MapperManager#map(Object, Class, Class, String...)}.
Expand Down Expand Up @@ -77,6 +77,13 @@ public interface DbClient {
*/
<C> C unwrap(Class<C> cls);


/**
* Closes the DbClient and releases any associated resources.
*/
@Override
void close();

/**
* Create Helidon database client.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,4 +39,9 @@ protected DbClientBase(DbClientContext context) {
public DbClientContext context() {
return context;
}

@Override
public void close() {
// No-op by default
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,12 +27,13 @@
import io.helidon.common.config.Config;
import io.helidon.dbclient.DbClientException;
import io.helidon.dbclient.hikari.spi.HikariMetricsProvider;
import io.helidon.dbclient.jdbc.CloseableJdbcConnectionPool;
import io.helidon.dbclient.jdbc.JdbcConnectionPool;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

class HikariConnectionPool implements JdbcConnectionPool {
class HikariConnectionPool implements CloseableJdbcConnectionPool {

private final HikariDataSource dataSource;
private final String dbType;
Expand Down Expand Up @@ -71,6 +72,11 @@ public Connection connection() {
}
}

@Override
public void close() {
dataSource.close();
}

@Override
public String dbType() {
return dbType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.dbclient.jdbc;

/**
* CloseableJdbcConnectionPool is an interface that represents a JDBC connection pool that can be closed.
* It extends the JdbcConnectionPool interface and the AutoCloseable interface.
*
* @see JdbcConnectionPool
* @see AutoCloseable
*/
public interface CloseableJdbcConnectionPool extends JdbcConnectionPool, AutoCloseable {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
* Copyright (c) 2019, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,7 @@
*/
package io.helidon.dbclient.jdbc;

import java.lang.System.Logger.Level;
import java.sql.Connection;

import io.helidon.dbclient.DbClient;
Expand All @@ -24,6 +25,7 @@
* Helidon DB implementation for JDBC drivers.
*/
class JdbcClient extends DbClientBase implements DbClient {
private static final System.Logger LOGGER = System.getLogger(JdbcClient.class.getName());

private final JdbcConnectionPool connectionPool;

Expand Down Expand Up @@ -64,6 +66,17 @@ public JdbcClientContext context() {
return (JdbcClientContext) super.context();
}

@Override
public void close() {
if (connectionPool instanceof CloseableJdbcConnectionPool cjcp) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

try {
cjcp.close();
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Failed to close the connection pool", e);
}
}
}

@Override
public <C> C unwrap(Class<C> cls) {
if (Connection.class.isAssignableFrom(cls)) {
Expand Down