Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: Resolve MySQL Driver Loading Issue (#6760) #6765

Merged
merged 5 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6757](https://github.com/apache/incubator-seata/pull/6757)] the bug where multiple nodes cannot be retrieved from the naming server
- [[#6769](https://github.com/apache/incubator-seata/pull/6769)] fix tcc fence deadLock
- [[#6778](https://github.com/apache/incubator-seata/pull/6778)] fix namingserver node term
- [[#6765](https://github.com/apache/incubator-seata/pull/6765)] fix MySQL driver loading by replacing custom classloader with system classloader for better compatibility and simplified process


### optimize:
Expand Down
2 changes: 2 additions & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
- [[#6757](https://github.com/apache/incubator-seata/pull/6757)] 修复client通过namingserver只能获取到一个tc节点的bug
- [[#6769](https://github.com/apache/incubator-seata/pull/6769)] 修复tcc fence死锁
- [[#6778](https://github.com/apache/incubator-seata/pull/6778)] 修复namingserver的节点term为0问题
- [[#6765](https://github.com/apache/incubator-seata/pull/6765)] 改进MySQL驱动加载机制,将自定义类加载器替换为系统类加载器,更兼容简化流程


### optimize:
- [[#6499](https://github.com/apache/incubator-seata/pull/6499)] 拆分 committing 和 rollbacking 状态的任务线程池
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@
*/
package org.apache.seata.core.store.db;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
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.StoreException;
Expand Down Expand Up @@ -56,20 +47,8 @@ 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 final static String MYSQL8_DRIVER_CLASS_NAME = "com.mysql.cj.jdbc.Driver";

private final static String MYSQL_DRIVER_FILE_PREFIX = "mysql-connector-java-";

private final static Map<String, ClassLoader> MYSQL_DRIVER_LOADERS;

private static final long DEFAULT_DB_MAX_WAIT = 5000;

static {
MYSQL_DRIVER_LOADERS = createMysqlDriverClassLoaders();
}

@Override
public void init() {
this.dataSource = generate();
Expand Down Expand Up @@ -145,50 +124,7 @@ protected Long getMaxWait() {
}

protected ClassLoader getDriverClassLoader() {
return MYSQL_DRIVER_LOADERS.getOrDefault(getDriverClassName(), ClassLoader.getSystemClassLoader());
}

private static Map<String, ClassLoader> createMysqlDriverClassLoaders() {
Map<String, ClassLoader> loaders = new HashMap<>();
String cp = System.getProperty("java.class.path");
if (cp == null || cp.isEmpty()) {
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());
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) {
}
});
return loaders;
return ClassLoader.getSystemClassLoader();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class AbstractDataSourceProviderTest {

private final String hikariDatasourceType = "hikari";

private final String mysqlJdbcDriver = "com.mysql.jdbc.Driver";

@Test
public void testDbcpDataSourceProvider() {
DataSource dataSource = EnhancedServiceLoader.load(DataSourceProvider.class, dbcpDatasourceType).provide();
Expand All @@ -52,4 +54,11 @@ public void testHikariDataSourceProvider() {
DataSource dataSource = EnhancedServiceLoader.load(DataSourceProvider.class, hikariDatasourceType).provide();
Assertions.assertNotNull(dataSource);
}

@Test
public void testMySQLDataSourceProvider() throws ClassNotFoundException {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
Class<?> driverClass = Class.forName(mysqlJdbcDriver, true, classLoader);
Assertions.assertNotNull(driverClass);
}
}
Loading