zhenyue-xu commented on code in PR #9667:
URL: https://github.com/apache/seatunnel/pull/9667#discussion_r2258839542
##########
seatunnel-connectors-v2/connector-doris/src/main/java/org/apache/seatunnel/connectors/doris/catalog/DorisCatalog.java:
##########
@@ -134,14 +157,198 @@ public void open() throws CatalogException {
queryPort,
defaultDatabase);
try {
- conn = DriverManager.getConnection(jdbcUrl, username, password);
+ // Prepare connection properties
+ Properties connectionInfo = new Properties();
+ if (username != null) {
+ connectionInfo.put("user", username);
+ }
+ if (password != null) {
+ connectionInfo.put("password", password);
+ }
+
+ // Attempt to connect using appropriate driver strategy
+ conn = attemptConnectionWithDrivers(jdbcUrl, connectionInfo);
+
+ // Validate connection and initialize catalog
conn.getCatalog();
dorisVersion = getDorisVersion();
typeConverter =
DorisTypeConverterFactory.getTypeConverter(dorisVersion);
+
} catch (SQLException e) {
- throw new CatalogException(String.format("Failed to connect url
%s", jdbcUrl), e);
+ throw new CatalogException(String.format("Failed to connect to
URL: %s", jdbcUrl), e);
+ }
+
+ LOG.info("Catalog [{}] successfully established connection to [{}]",
catalogName, jdbcUrl);
+ }
+
+ /**
+ * Attempt to establish connection using the appropriate driver strategy.
If LDAP is enabled and
+ * no driver is specified, use MySQL 5.x driver as default. If a specific
driver is specified
+ * and fails, throw an exception.
+ *
+ * @param jdbcUrl The JDBC URL to connect to
+ * @param connectionInfo Connection properties including user credentials
+ * @return A valid database connection
+ * @throws SQLException if connection fails
+ */
+ private Connection attemptConnectionWithDrivers(String jdbcUrl, Properties
connectionInfo)
+ throws SQLException {
+
+ // Determine the driver to use
+ String targetDriver = determineTargetDriver();
+
+ if (StringUtils.isNotBlank(targetDriver)) {
+ // Use specific driver (either specified or default for LDAP)
+ return connectWithSpecificDriver(targetDriver, jdbcUrl,
connectionInfo);
+ } else {
+ // No driver specified and LDAP disabled, try all available drivers
+ return connectWithAnyAvailableDriver(jdbcUrl, connectionInfo);
+ }
+ }
+
+ /** Determine which driver to use based on configuration */
+ private String determineTargetDriver() {
+ if (StringUtils.isNotBlank(driverClass)) {
+ // Use explicitly specified driver
+ return driverClass;
+ } else if (enableLdap) {
+ // Use MySQL 5.x driver as default when LDAP is enabled
+ return "com.mysql.jdbc.Driver";
+ }
+ return null;
+ }
+
+ /** Connect using a specific driver class. Throws exception if fails. */
+ private Connection connectWithSpecificDriver(
+ String targetDriverClass, String jdbcUrl, Properties
connectionInfo)
+ throws SQLException {
+
+ LOG.debug("Attempting connection with specific driver: {}",
targetDriverClass);
+
+ Enumeration<Driver> drivers = DriverManager.getDrivers();
+ while (drivers.hasMoreElements()) {
+ Driver driver = drivers.nextElement();
+ if (StringUtils.equals(driver.getClass().getName(),
targetDriverClass)) {
+ Connection connection = tryConnectWithDriver(driver, jdbcUrl,
connectionInfo);
+ if (connection != null) {
+ LOG.info("Successfully connected using driver: {}",
targetDriverClass);
+ return connection;
+ }
+ }
+ }
+
+ // If we reach here, the specified driver failed or wasn't found
+ throw new SQLException(
+ String.format(
+ "Failed to connect using specified driver: %s. "
+ + "Please check if the driver is available in
classpath and configuration is correct.",
+ targetDriverClass));
+ }
+
+ /**
+ * Try connecting with any available driver (fallback when no driver
specified and LDAP
+ * disabled)
+ */
+ private Connection connectWithAnyAvailableDriver(String jdbcUrl,
Properties connectionInfo)
Review Comment:
you are right, Fixed
--
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]