Created
July 4, 2018 09:05
-
-
Save YSMull/edb842892cf797648d7f83b53e3de500 to your computer and use it in GitHub Desktop.
初步完成了一个能连接不同版本mysql的DriverPool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.URL; | |
import java.net.URLClassLoader; | |
import java.sql.Connection; | |
import java.sql.Driver; | |
import java.sql.SQLException; | |
import java.util.Arrays; | |
import java.util.Properties; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.concurrent.CountDownLatch; | |
public class Test { | |
private static ConcurrentHashMap<String, Class> driverClassCacheMap = new ConcurrentHashMap<>(); | |
private static Connection getConnection(Properties props, String... libs) throws InstantiationException, IllegalAccessException, SQLException { | |
Class driverClass = driverClassCacheMap.computeIfAbsent(props.getProperty("driverType"), k -> { | |
ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader(); | |
URL[] libUrls = Arrays.stream(libs) | |
.map(lib -> threadClassLoader.getClass().getResource(lib)) | |
.toArray(URL[]::new); | |
URLClassLoader classLoader = new URLClassLoader(libUrls, threadClassLoader); | |
System.out.println("miss"); | |
try { | |
return classLoader.loadClass(props.getProperty("driverClass")); | |
} catch (ClassNotFoundException e) { | |
return null; | |
} | |
}); | |
System.out.print(driverClassCacheMap.size()); | |
if (driverClass != null) { | |
Driver driver = (Driver) driverClass.newInstance(); | |
return driver.connect(props.getProperty("url"), props); | |
} else { | |
return null; | |
} | |
} | |
public static void main(String[] args) throws InterruptedException { | |
final int N = 120; | |
final CountDownLatch latch = new CountDownLatch(2 * N); | |
Thread[] m5s = new Thread[N]; | |
Thread[] m8s = 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"); | |
props.setProperty("driverType", "mysql5.x"); | |
Connection conn = getConnection(props, | |
"/lib/mysql5/mysql-connector-java-5.1.34.jar"); | |
if (conn != null) { | |
System.out.println(conn.getMetaData().getDatabaseProductVersion()); | |
} | |
latch.countDown(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
}); | |
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"); | |
props.setProperty("driverType", "mysql8.x"); | |
Connection conn = getConnection(props, | |
"/lib/mysql8/mysql-connector-java-8.0.11.jar", | |
"/lib/mysql8/protobug-java-2.6.0"); | |
if (conn != null) { | |
System.out.println(conn.getMetaData().getDatabaseProductVersion()); | |
} | |
latch.countDown(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
}); | |
} | |
long startTime = System.nanoTime(); | |
for (int i = 0; i < N; i++) { | |
m5s[i].start(); | |
m8s[i].start(); | |
} | |
latch.await(); | |
System.out.println((System.nanoTime() - startTime) / 1000_000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment