This is an automated email from the ASF dual-hosted git repository. jsbxyyx pushed a commit to branch 2.4.0 in repository https://gitbox.apache.org/repos/asf/incubator-seata.git
The following commit(s) were added to refs/heads/2.4.0 by this push: new 5483453c03 bugfix: the drivers in the lib folder cannot be loaded (#7355) 5483453c03 is described below commit 5483453c03f49a67988d999aca895347a264b28c Author: funkye <jian...@apache.org> AuthorDate: Wed May 21 17:06:40 2025 +0800 bugfix: the drivers in the lib folder cannot be loaded (#7355) --- changes/en-us/2.4.0.md | 2 + changes/zh-cn/2.4.0.md | 2 + .../core/store/db/AbstractDataSourceProvider.java | 110 +++++++++++---------- 3 files changed, 61 insertions(+), 53 deletions(-) diff --git a/changes/en-us/2.4.0.md b/changes/en-us/2.4.0.md index 03079049c7..c791ef9e00 100644 --- a/changes/en-us/2.4.0.md +++ b/changes/en-us/2.4.0.md @@ -31,6 +31,7 @@ The version is updated as follows: - [[#7157](https://github.com/apache/incubator-seata/pull/7157)] migrate the console to the naming server - [[#7213](https://github.com/apache/incubator-seata/pull/7213)] support kingbase xa mode +- [[#7024](https://github.com/apache/incubator-seata/pull/7024)] add console transaction control ### bugfix: @@ -48,6 +49,7 @@ The version is updated as follows: - [[#7241](https://github.com/apache/incubator-seata/pull/7241)] upgrade tomcat-embed-core to 9.0.99 to fix CVE-2025-24813 - [[#7272](https://github.com/apache/incubator-seata/pull/7272)] fix: fix transaction info not display - [[#7277](https://github.com/apache/incubator-seata/pull/7277)] Fix MySQL jdbc driver can't be found properly +- [[#7355](https://github.com/apache/incubator-seata/pull/7355)] fix: the drivers in the lib folder cannot be loaded ### optimize: diff --git a/changes/zh-cn/2.4.0.md b/changes/zh-cn/2.4.0.md index 8639304947..994624cf9f 100644 --- a/changes/zh-cn/2.4.0.md +++ b/changes/zh-cn/2.4.0.md @@ -31,6 +31,7 @@ Apache Seata(incubating) 是一款开源的分布式事务解决方案,提供 - [[#7157](https://github.com/apache/incubator-seata/pull/7157)] 将console迁移至namingserver中 - [[#7213](https://github.com/apache/incubator-seata/pull/7213)] 支持 kingbase xa 模式 +- [[#7024](https://github.com/apache/incubator-seata/pull/7024)] 控制台支持事务控制 ### bugfix: @@ -47,6 +48,7 @@ Apache Seata(incubating) 是一款开源的分布式事务解决方案,提供 - [[#7241](https://github.com/apache/incubator-seata/pull/7241)] 升级 tomcat-embed-core 至 9.0.99 版本以解决 CVE-2025-24813 - [[#7272](https://github.com/apache/incubator-seata/pull/7272)] 修复全局事务显示问题 - [[#7277](https://github.com/apache/incubator-seata/pull/7277)] 修复MySQL jdbc驱动无法正常找到的问题 +- [[#7355](https://github.com/apache/incubator-seata/pull/7355)] 修复驱动无法加载的问题 ### optimize: diff --git a/core/src/main/java/org/apache/seata/core/store/db/AbstractDataSourceProvider.java b/core/src/main/java/org/apache/seata/core/store/db/AbstractDataSourceProvider.java index 860d9dc953..df8725fd52 100644 --- a/core/src/main/java/org/apache/seata/core/store/db/AbstractDataSourceProvider.java +++ b/core/src/main/java/org/apache/seata/core/store/db/AbstractDataSourceProvider.java @@ -16,6 +16,9 @@ */ package org.apache.seata.core.store.db; +import static org.apache.seata.common.DefaultValues.DEFAULT_DB_MAX_CONN; +import static org.apache.seata.common.DefaultValues.DEFAULT_DB_MIN_CONN; + import java.io.File; import java.net.MalformedURLException; import java.net.URL; @@ -24,9 +27,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.stream.Stream; - import javax.sql.DataSource; - import org.apache.seata.common.exception.ShouldNeverHappenException; import org.apache.seata.common.exception.StoreException; import org.apache.seata.common.executor.Initialize; @@ -39,12 +40,9 @@ import org.apache.seata.core.constants.DBType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static org.apache.seata.common.DefaultValues.DEFAULT_DB_MAX_CONN; -import static org.apache.seata.common.DefaultValues.DEFAULT_DB_MIN_CONN; - /** * The abstract datasource provider - * + * */ public abstract class AbstractDataSourceProvider implements DataSourceProvider, Initialize { @@ -57,13 +55,13 @@ public abstract class AbstractDataSourceProvider implements DataSourceProvider, */ protected static final Configuration CONFIG = ConfigurationFactory.getInstance(); - private final static String MYSQL_DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver"; + private static final String MYSQL_DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver"; - private final static String MYSQL8_DRIVER_CLASS_NAME = "com.mysql.cj.jdbc.Driver"; + private static final String MYSQL8_DRIVER_CLASS_NAME = "com.mysql.cj.jdbc.Driver"; - private final static String MYSQL_DRIVER_FILE_PREFIX = "mysql-connector-j"; + private static final String MYSQL_DRIVER_FILE_PREFIX = "mysql-connector-j"; - private final static Map<String, ClassLoader> MYSQL_DRIVER_LOADERS; + private static final Map<String, ClassLoader> MYSQL_DRIVER_LOADERS; private static final long DEFAULT_DB_MAX_WAIT = 5000; @@ -87,7 +85,7 @@ public abstract class AbstractDataSourceProvider implements DataSourceProvider, } public void validate() { - //valid driver class name + // valid driver class name String driverClassName = getDriverClassName(); ClassLoader loader = getDriverClassLoader(); if (null == loader) { @@ -106,14 +104,21 @@ public abstract class AbstractDataSourceProvider implements DataSourceProvider, .map(file -> file.isFile() ? file.getParentFile() : file) .filter(Objects::nonNull) .filter(File::isDirectory) - .map(file -> new File(file, "jdbc")) + // Only the MySQL driver needs to be placed in the jdbc folder. + .map(file -> (MYSQL8_DRIVER_CLASS_NAME.equals(driverClassName) + || MYSQL_DRIVER_CLASS_NAME.equals(driverClassName)) + ? new File(file, "jdbc") + : file) .filter(File::exists) .filter(File::isDirectory) - .distinct().findAny().orElseThrow(() -> new ShouldNeverHappenException("can not find jdbc folder")).getAbsolutePath(); + .distinct() + .findAny() + .map(File::getAbsolutePath) + .orElseThrow(() -> new ShouldNeverHappenException("cannot find jdbc folder")); throw new StoreException(String.format( - "The driver {%s} cannot be found in the path %s. Please ensure that the appropriate database driver dependencies are included in the classpath.", driverClassName, driverClassPath)); + "The driver {%s} cannot be found in the path %s. Please ensure that the appropriate database driver dependencies are included in the classpath.", + driverClassName, driverClassPath)); } - } /** * generate the datasource @@ -139,7 +144,7 @@ public abstract class AbstractDataSourceProvider implements DataSourceProvider, String driverClassName = CONFIG.getConfig(ConfigurationKeys.STORE_DB_DRIVER_CLASS_NAME); if (StringUtils.isBlank(driverClassName)) { throw new StoreException( - String.format("the {%s} can't be empty", ConfigurationKeys.STORE_DB_DRIVER_CLASS_NAME)); + String.format("the {%s} can't be empty", ConfigurationKeys.STORE_DB_DRIVER_CLASS_NAME)); } return driverClassName; } @@ -150,12 +155,12 @@ public abstract class AbstractDataSourceProvider implements DataSourceProvider, * @return the db max wait */ protected Long getMaxWait() { - Long maxWait = CONFIG.getLong(ConfigurationKeys.STORE_DB_MAX_WAIT, DEFAULT_DB_MAX_WAIT); - return maxWait; + return CONFIG.getLong(ConfigurationKeys.STORE_DB_MAX_WAIT, DEFAULT_DB_MAX_WAIT); } protected ClassLoader getDriverClassLoader() { - return MYSQL_DRIVER_LOADERS.getOrDefault(getDriverClassName(), ClassLoader.getSystemClassLoader()); + return MYSQL_DRIVER_LOADERS.getOrDefault( + getDriverClassName(), this.getClass().getClassLoader()); } private static Map<String, ClassLoader> createMysqlDriverClassLoaders() { @@ -168,39 +173,39 @@ public abstract class AbstractDataSourceProvider implements DataSourceProvider, return loaders; } Stream.of(cp.split(File.pathSeparator)) - .map(File::new) - .filter(File::exists) - .map(file -> file.isFile() ? file.getParentFile() : file) - .filter(Objects::nonNull) - .filter(File::isDirectory) - .map(file -> new File(file, "jdbc")) - .filter(File::exists) - .filter(File::isDirectory) - .distinct() - .flatMap(file -> { - File[] files = file.listFiles((f, name) -> name.startsWith(MYSQL_DRIVER_FILE_PREFIX)); - if (files != null) { - return Stream.of(files); - } else { - return Stream.of(); - } - }) - .forEach(file -> { - if (loaders.containsKey(MYSQL8_DRIVER_CLASS_NAME) && loaders.containsKey(MYSQL_DRIVER_CLASS_NAME)) { - return; - } - try { - URL url = file.toURI().toURL(); - ClassLoader loader = new URLClassLoader(new URL[]{url}, ClassLoader.getSystemClassLoader()); + .map(File::new) + .filter(File::exists) + .map(file -> file.isFile() ? file.getParentFile() : file) + .filter(Objects::nonNull) + .filter(File::isDirectory) + .map(file -> new File(file, "jdbc")) + .filter(File::exists) + .filter(File::isDirectory) + .distinct() + .flatMap(file -> { + File[] files = file.listFiles((f, name) -> name.startsWith(MYSQL_DRIVER_FILE_PREFIX)); + if (files != null) { + return Stream.of(files); + } else { + return Stream.of(); + } + }) + .forEach(file -> { + if (loaders.containsKey(MYSQL8_DRIVER_CLASS_NAME) && loaders.containsKey(MYSQL_DRIVER_CLASS_NAME)) { + return; + } try { - loader.loadClass(MYSQL8_DRIVER_CLASS_NAME); - loaders.putIfAbsent(MYSQL8_DRIVER_CLASS_NAME, loader); - } catch (ClassNotFoundException e) { - loaders.putIfAbsent(MYSQL_DRIVER_CLASS_NAME, loader); + URL url = file.toURI().toURL(); + ClassLoader loader = new URLClassLoader(new URL[] {url}, ClassLoader.getSystemClassLoader()); + try { + loader.loadClass(MYSQL8_DRIVER_CLASS_NAME); + loaders.putIfAbsent(MYSQL8_DRIVER_CLASS_NAME, loader); + } catch (ClassNotFoundException e) { + loaders.putIfAbsent(MYSQL_DRIVER_CLASS_NAME, loader); + } + } catch (MalformedURLException ignore) { } - } catch (MalformedURLException ignore) { - } - }); + }); return loaders; } @@ -243,8 +248,8 @@ public abstract class AbstractDataSourceProvider implements DataSourceProvider, password = ConfigTools.publicDecrypt(password, publicKey); } catch (Exception e) { LOGGER.error( - "decryption failed,please confirm whether the ciphertext and secret key are correct! error msg: {}", - e.getMessage()); + "decryption failed,please confirm whether the ciphertext and secret key are correct! error msg: {}", + e.getMessage()); } } return password; @@ -292,5 +297,4 @@ public abstract class AbstractDataSourceProvider implements DataSourceProvider, protected String getPublicKey() { return CONFIG.getConfig(ConfigurationKeys.STORE_PUBLIC_KEY); } - } --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For additional commands, e-mail: notifications-h...@seata.apache.org