問題的引出
為什么我們通過DriverManager.getConnection(url, username, password)獲取到的是Connection是一個接口,卻能調用方法?不是說接口只是一個抽象的東西?不能做具體的事情么?
那我們通過DriverManager獲取到的Connection接口為什么可以做獲取Statement對象之類的事情?
首先我們看我們獲取到的對象是怎么樣獲取的:
Connection conn = DriverManager.getConnection(url, username, password);
這種獲取方法是不是很眼熟?我們看一下從小學到大的一個創建對象的寫法:
Person person=new Student();
這樣來看是不是很熟悉?是不是我們的父類引用指向子類對象?
所以我們實際上通過DriverManager.getConnection(url, username, password)獲取到的是實現了Connection接口的實體對象
我們接著看DriverManager是怎么獲取到Connection實體對象的,先通過DriverManager.getConnection(url, username, password)跳到源碼位置
DvierManamger的方法
public static Connection getConnection(String url,
String user, String password) throws SQLException {
java.util.Properties info = new java.util.Properties();//創建了一個Properties對象來存放信息
if (user != null) {
info.put("user", user);//如果user不為空,將user信息存入info對象
}
if (password != null) {
info.put("password", password);//如果password不為空,將password信息存入info對象
}
//再通過另一個重載的getConnection方法獲取連接
return (getConnection(url, info, Reflection.getCallerClass()));
}
我們在第一次跳轉進來的方法中是沒法直接獲取實現了Connection接口的實體類的,這里還要通過DriverManger的重載方法繼續做做事情,接著我們進到getConnection(url, info, Reflection.getCallerClass())方法
private static Connection getConnection(
String url, java.util.Properties info, Class<?> caller) throws SQLException {
/*
* 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();
}
}
if(url == null) {
throw new SQLException("The url cannot be null", "08001");
}
println("DriverManager.getConnection(\"" + url + "\")");
// 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.
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());
}
}
// if we got here nobody could connect.
if (reason != null) {
println("getConnection failed: " + reason);
throw reason;
}
println("getConnection: no suitable driver found for "+ url);
throw new SQLException("No suitable driver found for "+ url, "08001");
}
這一整個方法我們不用全部關心,找到切入點,我們現在知道自己在找Connection,和Connection息息相關的是Driver,那我們現在就針對這兩個對象來查找,可以看到這個方法里的這段關鍵代碼
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());
}
}
這個增強for循環中有一個registeredDrivers對象,我們通過查看可以知道他是一個ArrayList,存在這個List的對象是DriverInfo,現在先看一下DriverInfo
final Driver driver;
DriverAction da;
DriverInfo(Driver driver, DriverAction action) {
this.driver = driver;
da = action;
}
可以看到他接收了一個成員變量driver,那我們初步可以判斷,DriverManager是通過registeredDrivers這個集合來獲取已經注冊好的驅動,那registeredDrivers這個集合是在什么地方添加對象的呢?
public static synchronized void registerDriver(java.sql.Driver driver,
DriverAction da)
throws SQLException {
/* Register the driver if it has not already been added to our list */
if(driver != null) {
registeredDrivers.addIfAbsent(new DriverInfo(driver, da));
} else {
// This is for compatibility with the original DriverManager
throw new NullPointerException();
}
println("registerDriver: " + driver);
}
根據關鍵字"registeredDrivers"查找,我們發現了registerDriver這個方法,如果driver對象不為空就嘗試進行添加,如果沒有添加過,我們就會將該driver對象加入registerDriver集合中,而注冊驅動是我們第一步做的事情,所以在我們注冊驅動的時候,已經將com.mysql.jdbc.Driver加到了該集合中
再回到增強for循環看他做了什么事
Connection con = aDriver.driver.connect(url, info);
if (con != null) {
// Success!
println("getConnection returning " + aDriver.driver.getClass().getName());
return (con);
}
DriverManger通過每次遍歷到的aDriver獲取里面的driver對象,再調用它的connect方法,將url(數據庫地址),info(訪問用戶名,訪問密碼)拿來使用,獲取實現了Connection接口的實現類,到此,我們初步可以看到DriverManager是如何獲取Connection實現類的了
去到我們注冊的com.mysql.jdbc.Driver類中找connect方法,發現沒找到,那子類沒有的方法一般父類會有,我們繼續往父類com.mysql.jdbc.NonRegisteringDriver那查找connect(url,info)方法
我們在父類com.mysql.jdbc.NonRegisteringDriver中可以找到下面方法:
public java.sql.Connection connect(String url, Properties info) throws SQLException
在這個方法里我們就能看到它在返回ConnectionImpl對象了