dengzhhu653 commented on code in PR #6464: URL: https://github.com/apache/hive/pull/6464#discussion_r3556465735
########## standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/datasource/MetastoreDriver.java: ########## @@ -0,0 +1,177 @@ +/* + * 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.hadoop.hive.metastore.datasource; + +import com.google.common.annotations.VisibleForTesting; + +import java.sql.Connection; +import java.sql.Driver; +import java.sql.DriverManager; +import java.sql.DriverPropertyInfo; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +import java.util.Properties; +import java.util.logging.Logger; +import java.util.regex.Pattern; + +import org.apache.commons.lang3.math.NumberUtils; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hive.metastore.conf.MetastoreConf; +import org.apache.hadoop.hive.metastore.utils.MetastoreVersionInfo; +import org.slf4j.LoggerFactory; + +import static java.sql.DriverManager.registerDriver; + +public class MetastoreDriver implements Driver { + private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(MetastoreDriver.class); + private static final String URL_PREFIX = "jdbc:metastore://"; + private static final Configuration configuration; + private static int majorVersion = -1; + private static int minorVersion = -1; + private static volatile Driver delegateDriver; + static { + try { + registerDriver(new MetastoreDriver()); + String versionString = MetastoreVersionInfo.getVersion(); + String[] versionNums = versionString.split("\\."); + if (NumberUtils.isNumber(versionNums[0])) { + majorVersion = Integer.parseInt(versionNums[0]); + } + if (versionNums.length >1 && NumberUtils.isNumber(versionNums[1])) { + minorVersion = Integer.parseInt(versionNums[1]); + } + configuration = MetastoreConf.newMetastoreConf(); + } catch (Exception e) { + throw new RuntimeException("Failed to register Metastore driver", e); + } + } + + private synchronized static Driver + findRegisteredDriver(String jdbcUrl, String driverClassName) throws SQLException { + if (delegateDriver != null && delegateDriver.acceptsURL(jdbcUrl)) { + // Use the cached driver + return delegateDriver; + } + List<Driver> candidates = new ArrayList<>(); + for (Enumeration<Driver> drivers = DriverManager.getDrivers(); drivers.hasMoreElements();) { + Driver driver = drivers.nextElement(); + try { + if (driver.acceptsURL(jdbcUrl)) { + candidates.add(driver); + } + } catch (Exception e) { + } + } + + if (candidates.isEmpty()) { + Class<Driver> driverClz = tryLoadDriver(driverClassName, Thread.currentThread().getContextClassLoader(), + MetastoreDriver.class.getClassLoader()); + if (driverClz != null) { + try { + Driver driver = driverClz.getDeclaredConstructor().newInstance(); + if (!driver.acceptsURL(jdbcUrl)) { + throw new RuntimeException("Driver " + driverClassName + " cannot accept jdbcUrl"); + } + candidates.add(driver); + } catch (Exception e) { + LOG.warn("Failed to create instance of driver class {}", driverClassName, e); Review Comment: There is a final catch `DriverManager.getDriver(jdbcUrl)` for looking up the driver at the end, which will throw ` SQLException("No suitable driver", "08001")` if driver not found. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
