Skip to content

Instantly share code, notes, and snippets.

@YSMull
Created July 4, 2018 06:23
Show Gist options
  • Save YSMull/1d6441fee5af08273ecf6b82b88b7ac5 to your computer and use it in GitHub Desktop.
Save YSMull/1d6441fee5af08273ecf6b82b88b7ac5 to your computer and use it in GitHub Desktop.
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;
public class Test {
private static void tryConnect(Properties props, String... libs) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
URL[] cp = new URL[libs.length];
ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
for (int i = 0; i < cp.length; i++) {
cp[i] = threadClassLoader.getClass().getResource(libs[i]);
}
URLClassLoader classLoader = new URLClassLoader(cp, threadClassLoader);
Class driverClass = classLoader.loadClass(props.getProperty("driverClass"));
Driver driver = (Driver) driverClass.newInstance();
Connection conn = driver.connect(props.getProperty("url"), props);
System.out.println(conn.getMetaData().getDatabaseProductVersion());
}
public static void main(String[] args) throws Exception {
final int N = 10;
Thread[] m8s = new Thread[N];
Thread[] m5s = new Thread[N];
for (int i = 0; i < N; i++) {
m5s[i] = new Thread(() -> {
try {
Properties props = new Properties();
props.setProperty("user", "root");
props.setProperty("password", "123");
props.setProperty("url", "jdbc:mysql://localhost:3305?autoReconnect=true&useSSL=false");
props.setProperty("driverClass", "com.mysql.jdbc.Driver");
tryConnect(props,
"/lib/mysql5/mysql-connector-java-5.1.34.jar");
} catch (Exception ignored) {
}
});
m8s[i] = new Thread(() -> {
try {
Properties props = new Properties();
props.setProperty("user", "root");
props.setProperty("password", "123");
props.setProperty("url", "jdbc:mysql://localhost:3308?autoReconnect=true&useSSL=false");
props.setProperty("driverClass", "com.mysql.cj.jdbc.Driver");
tryConnect(props,
"/lib/mysql8/mysql-connector-java-8.0.11.jar",
"/lib/mysql8/protobug-java-2.6.0");
} catch (Exception ignored) {
}
});
}
for (int i = 0; i < N; i++) {
m8s[i].start();
m5s[i].start();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment