Skip to content

Commit

Permalink
bugfix: Resolve MySQL Driver Loading Issue (apache#6760)
Browse files Browse the repository at this point in the history
  • Loading branch information
lyl2008dsg committed Aug 18, 2024
1 parent f7c3866 commit e00395e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +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;
import org.apache.seata.common.executor.Initialize;
import org.apache.seata.common.util.ConfigTools;
Expand All @@ -38,6 +27,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;

import static org.apache.seata.common.DefaultValues.DEFAULT_DB_MAX_CONN;
import static org.apache.seata.common.DefaultValues.DEFAULT_DB_MIN_CONN;

Expand All @@ -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
4 changes: 2 additions & 2 deletions distribution/NOTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ Please copy database driver dependencies, such as `mysql-connector-java.jar`, to
├── spring-web-5.3.30.jar
├── spring-webmvc-5.3.30.jar
├── xstream-1.4.20.jar
├── mysql-connector-java-8.0.28.jar
└── jdbc
├── mysql-connector-java-8.0.28.jar
└── NOTICE.md
```
Expand Down Expand Up @@ -555,8 +555,8 @@ Please copy database driver dependencies, such as `mysql-connector-java.jar`, to
├── spring-web-5.3.30.jar
├── spring-webmvc-5.3.30.jar
├── xstream-1.4.20.jar
├── mysql-connector-java-8.0.28.jar
└── jdbc
├── mysql-connector-java-8.0.28.jar
└── NOTICE.md
```

0 comments on commit e00395e

Please sign in to comment.