DriverManager-sharpgetConnection is as follows. Its logic is to loop all the driver, registered through SPI to set up connection, one by one to see which one can be established successfully and return directly
. for(DriverInfo aDriver : registeredDrivers) {
// If the caller does not have permission to load the driver then
// skip it.
if(isDriverAllowed(aDriver.driver, callerCL)) {
try {
println(" trying " + aDriver.driver.getClass().getName());
Connection con = aDriver.driver.connect(url, info);
if (con != null) {
// Success!
println("getConnection returning " + aDriver.driver.getClass().getName());
return (con);
}
} catch (SQLException ex) {
if (reason == null) {
reason = ex;
}
}
} else {
println(" skipping: " + aDriver.getClass().getName());
}
}
is actually the
of which driver can be located through jdbcUrl.java.sql.Driver-sharpacceptsURL actually does this. I read the Driver source code of MySQL, PgSQL and found that it all implemented this method. In fact, the above judgment was ok before the connection was obtained in a loop.
if(aDriver.driver.acceptsURL(url){
Connection con = aDriver.driver.connect(url, info);
}
I guess it"s a historical reason?