Skip to content

Commit e2005e9

Browse files
[4.x] Closeable jdbc pool (helidon-io#8571)
* Add close methods to database client classes - Extended DbClient with AutoCloseable interface - Updated copyright * Small fixes to the contribution to align with copyright and formatting rules --------- Co-authored-by: bjcoombs <ben.coombs@senapt.co.uk>
1 parent e73e996 commit e2005e9

5 files changed

Lines changed: 64 additions & 6 deletions

File tree

dbclient/dbclient/src/main/java/io/helidon/dbclient/DbClient.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
2+
* Copyright (c) 2019, 2024 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,7 +31,7 @@
3131
/**
3232
* Helidon database client.
3333
*/
34-
public interface DbClient {
34+
public interface DbClient extends AutoCloseable {
3535

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

80+
81+
/**
82+
* Closes the DbClient and releases any associated resources.
83+
*/
84+
@Override
85+
void close();
86+
8087
/**
8188
* Create Helidon database client.
8289
*

dbclient/dbclient/src/main/java/io/helidon/dbclient/DbClientBase.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 Oracle and/or its affiliates.
2+
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,4 +39,9 @@ protected DbClientBase(DbClientContext context) {
3939
public DbClientContext context() {
4040
return context;
4141
}
42+
43+
@Override
44+
public void close() {
45+
// No-op by default
46+
}
4247
}

dbclient/hikari/src/main/java/io/helidon/dbclient/hikari/HikariConnectionPool.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 Oracle and/or its affiliates.
2+
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,12 +27,13 @@
2727
import io.helidon.common.config.Config;
2828
import io.helidon.dbclient.DbClientException;
2929
import io.helidon.dbclient.hikari.spi.HikariMetricsProvider;
30+
import io.helidon.dbclient.jdbc.CloseableJdbcConnectionPool;
3031
import io.helidon.dbclient.jdbc.JdbcConnectionPool;
3132

3233
import com.zaxxer.hikari.HikariConfig;
3334
import com.zaxxer.hikari.HikariDataSource;
3435

35-
class HikariConnectionPool implements JdbcConnectionPool {
36+
class HikariConnectionPool implements CloseableJdbcConnectionPool {
3637

3738
private final HikariDataSource dataSource;
3839
private final String dbType;
@@ -71,6 +72,11 @@ public Connection connection() {
7172
}
7273
}
7374

75+
@Override
76+
public void close() {
77+
dataSource.close();
78+
}
79+
7480
@Override
7581
public String dbType() {
7682
return dbType;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2024 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.helidon.dbclient.jdbc;
18+
19+
/**
20+
* CloseableJdbcConnectionPool is an interface that represents a JDBC connection pool that can be closed.
21+
* It extends the JdbcConnectionPool interface and the AutoCloseable interface.
22+
*
23+
* @see JdbcConnectionPool
24+
* @see AutoCloseable
25+
*/
26+
public interface CloseableJdbcConnectionPool extends JdbcConnectionPool, AutoCloseable {
27+
}

dbclient/jdbc/src/main/java/io/helidon/dbclient/jdbc/JdbcClient.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
2+
* Copyright (c) 2019, 2024 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
1515
*/
1616
package io.helidon.dbclient.jdbc;
1717

18+
import java.lang.System.Logger.Level;
1819
import java.sql.Connection;
1920

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

2830
private final JdbcConnectionPool connectionPool;
2931

@@ -64,6 +66,17 @@ public JdbcClientContext context() {
6466
return (JdbcClientContext) super.context();
6567
}
6668

69+
@Override
70+
public void close() {
71+
if (connectionPool instanceof CloseableJdbcConnectionPool cjcp) {
72+
try {
73+
cjcp.close();
74+
} catch (Exception e) {
75+
LOGGER.log(Level.WARNING, "Failed to close the connection pool", e);
76+
}
77+
}
78+
}
79+
6780
@Override
6881
public <C> C unwrap(Class<C> cls) {
6982
if (Connection.class.isAssignableFrom(cls)) {

0 commit comments

Comments
 (0)