nickdelnano commented on code in PR #8323:
URL: https://github.com/apache/paimon/pull/8323#discussion_r3470294262


##########
paimon-core/src/main/java/org/apache/paimon/jdbc/CachedJdbcClientPool.java:
##########
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.paimon.jdbc;
+
+import org.apache.paimon.annotation.VisibleForTesting;
+import org.apache.paimon.options.Options;
+
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import static org.apache.paimon.options.CatalogOptions.CLIENT_POOL_SIZE;
+import static org.apache.paimon.options.CatalogOptions.URI;
+
+/**
+ * A cache that shares {@link JdbcClientPool} instances across multiple 
catalog instances in the
+ * same JVM. This prevents each Flink operator from creating its own 
connection pool when using the
+ * JDBC catalog.
+ *
+ * <p>The cache is keyed by JDBC URI and catalog key. Pools live for the 
lifetime of the JVM and are
+ * closed via a shutdown hook.
+ */
+public class CachedJdbcClientPool {
+
+    private static final ConcurrentMap<Key, JdbcClientPool> CLIENT_POOLS =
+            new ConcurrentHashMap<>();
+
+    static {
+        Runtime.getRuntime()
+                .addShutdownHook(
+                        new Thread(
+                                () -> {
+                                    for (JdbcClientPool pool : 
CLIENT_POOLS.values()) {
+                                        pool.close();
+                                    }
+                                    CLIENT_POOLS.clear();
+                                },
+                                "jdbc-client-pool-shutdown"));
+    }
+
+    private final Key key;
+    private final int poolSize;
+    private final String dbUrl;
+    private final Map<String, String> props;
+
+    public CachedJdbcClientPool(Options options, Map<String, String> props) {
+        this.dbUrl = options.get(URI);
+        this.poolSize = options.get(CLIENT_POOL_SIZE);
+        this.props = props;
+        this.key = Key.of(dbUrl, options.get(JdbcCatalogOptions.CATALOG_KEY));

Review Comment:
   Fixed and tests added



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to