// Worker method called by the public getConnection() methods.privatestaticConnectiongetConnection(String url,java.util.Properties info,Class<?> caller)throwsSQLException{/*
* When callerCl is null, we should check the application's
* (which is invoking this class indirectly)
* classloader, so that the JDBC driver class outside rt.jar
* can be loaded from here.
*/ClassLoader callerCL = caller !=null? caller.getClassLoader():null;synchronized(DriverManager.class){// synchronize loading of the correct classloader.if(callerCL ==null){
callerCL =Thread.currentThread().getContextClassLoader();}}
这里面说的很清楚了JDBC driver 除了可以在rt.jar加载外还可以在这里加载
// Walk through the loaded registeredDrivers attempting to make a connection.// Remember the first exception that gets raised so we can reraise it.SQLException reason =null;for(DriverInfo aDriver : registeredDrivers){// If the caller does not have permission to load the driver then// skip it.// 注释2if(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);}......
返回一个链接
......privatestaticbooleanisDriverAllowed(Driver driver,ClassLoader classLoader){boolean result =false;if(driver !=null){Class<?> aClass =null;try{// 注释3
aClass =Class.forName(driver.getClass().getName(),true, classLoader);}catch(Exception ex){
result =false;}
result =( aClass == driver.getClass())?true:false;}return result;}