diff --git a/dbclient/dbclient/src/main/java/io/helidon/dbclient/DbClient.java b/dbclient/dbclient/src/main/java/io/helidon/dbclient/DbClient.java index 7a7539f8f67..2b1eb545b6e 100644 --- a/dbclient/dbclient/src/main/java/io/helidon/dbclient/DbClient.java +++ b/dbclient/dbclient/src/main/java/io/helidon/dbclient/DbClient.java @@ -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. @@ -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...)}. @@ -77,6 +77,13 @@ public interface DbClient { */ C unwrap(Class cls); + + /** + * Closes the DbClient and releases any associated resources. + */ + @Override + void close(); + /** * Create Helidon database client. * diff --git a/dbclient/dbclient/src/main/java/io/helidon/dbclient/DbClientBase.java b/dbclient/dbclient/src/main/java/io/helidon/dbclient/DbClientBase.java index a8ab869dab9..70639550c93 100644 --- a/dbclient/dbclient/src/main/java/io/helidon/dbclient/DbClientBase.java +++ b/dbclient/dbclient/src/main/java/io/helidon/dbclient/DbClientBase.java @@ -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. @@ -39,4 +39,9 @@ protected DbClientBase(DbClientContext context) { public DbClientContext context() { return context; } + + @Override + public void close() { + // No-op by default + } } diff --git a/dbclient/hikari/src/main/java/io/helidon/dbclient/hikari/HikariConnectionPool.java b/dbclient/hikari/src/main/java/io/helidon/dbclient/hikari/HikariConnectionPool.java index 1a8afa04de4..fbd701801e3 100644 --- a/dbclient/hikari/src/main/java/io/helidon/dbclient/hikari/HikariConnectionPool.java +++ b/dbclient/hikari/src/main/java/io/helidon/dbclient/hikari/HikariConnectionPool.java @@ -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. @@ -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; @@ -71,6 +72,11 @@ public Connection connection() { } } + @Override + public void close() { + dataSource.close(); + } + @Override public String dbType() { return dbType; diff --git a/dbclient/jdbc/src/main/java/io/helidon/dbclient/jdbc/CloseableJdbcConnectionPool.java b/dbclient/jdbc/src/main/java/io/helidon/dbclient/jdbc/CloseableJdbcConnectionPool.java new file mode 100644 index 00000000000..b8d95080afb --- /dev/null +++ b/dbclient/jdbc/src/main/java/io/helidon/dbclient/jdbc/CloseableJdbcConnectionPool.java @@ -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 { +} diff --git a/dbclient/jdbc/src/main/java/io/helidon/dbclient/jdbc/JdbcClient.java b/dbclient/jdbc/src/main/java/io/helidon/dbclient/jdbc/JdbcClient.java index f91c11e062f..f7943083459 100644 --- a/dbclient/jdbc/src/main/java/io/helidon/dbclient/jdbc/JdbcClient.java +++ b/dbclient/jdbc/src/main/java/io/helidon/dbclient/jdbc/JdbcClient.java @@ -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. @@ -15,6 +15,7 @@ */ package io.helidon.dbclient.jdbc; +import java.lang.System.Logger.Level; import java.sql.Connection; import io.helidon.dbclient.DbClient; @@ -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; @@ -64,6 +66,17 @@ public JdbcClientContext context() { return (JdbcClientContext) super.context(); } + @Override + public void close() { + if (connectionPool instanceof CloseableJdbcConnectionPool cjcp) { + try { + cjcp.close(); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Failed to close the connection pool", e); + } + } + } + @Override public C unwrap(Class cls) { if (Connection.class.isAssignableFrom(cls)) {