CalvinKirs commented on code in PR #2009: URL: https://github.com/apache/incubator-seatunnel/pull/2009#discussion_r895376646
########## seatunnel-connectors/seatunnel-connectors-seatunnel/seatunnel-connector-seatunnel-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/connection/SimpleJdbcConnectionProvider.java: ########## @@ -0,0 +1,154 @@ +/* + * 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.seatunnel.connectors.seatunnel.jdbc.internal.connection; + +import static com.google.common.base.Preconditions.checkNotNull; + +import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.options.JdbcConnectorOptions; + +import lombok.NonNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.Serializable; +import java.sql.Connection; +import java.sql.Driver; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Enumeration; +import java.util.Properties; + +/** + * Simple JDBC connection provider. + */ +public class SimpleJdbcConnectionProvider + implements JdbcConnectionProvider, Serializable { + + private static final Logger LOG = LoggerFactory.getLogger(SimpleJdbcConnectionProvider.class); + + private static final long serialVersionUID = 1L; + + private final JdbcConnectorOptions jdbcOptions; + + private transient Driver loadedDriver; + private transient Connection connection; + + static { + // Load DriverManager first to avoid deadlock between DriverManager's + // static initialization block and specific driver class's static + // initialization block when two different driver classes are loading + // concurrently using Class.forName while DriverManager is uninitialized + // before. + // + // This could happen in JDK 8 but not above as driver loading has been + // moved out of DriverManager's static initialization block since JDK 9. + DriverManager.getDrivers(); + } + + public SimpleJdbcConnectionProvider(@NonNull JdbcConnectorOptions jdbcOptions) { + this.jdbcOptions = jdbcOptions; + } + + @Override + public Connection getConnection() { + return connection; + } + + @Override + public boolean isConnectionValid() + throws SQLException { + return connection != null + && connection.isValid(jdbcOptions.getConnectionCheckTimeoutSeconds()); + } + + private static Driver loadDriver(String driverName) + throws SQLException, ClassNotFoundException { + checkNotNull(driverName); + Enumeration<Driver> drivers = DriverManager.getDrivers(); + while (drivers.hasMoreElements()) { + Driver driver = drivers.nextElement(); + if (driver.getClass().getName().equals(driverName)) { + return driver; + } + } + + // We could reach here for reasons: + // * Class loader hell of DriverManager(see JDK-8146872). + // * driver is not installed as a service provider. + Class<?> clazz = + Class.forName(driverName, true, Thread.currentThread().getContextClassLoader()); + try { + return (Driver) clazz.newInstance(); Review Comment: Don't use Deprecated method -- 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]
