The following code is from java.sql.DriverManager
:
public static Connection getConnection(String url,String user, String password) {
// Gets the classloader of the code that called this method, may
// be null.
ClassLoader callerCL = DriverManager.getCallerClassLoader();
return (getConnection(url, info, callerCL));
}
My first question is why the result value of DriverManager.getCallerClassLoader();
may be null? I think the caller Class should be user's own Class, and it is usually AppClassLoader.
The subsequence of above code getConnection(url, info, callerCL)
, and the method body contains following code snippet.
if(callerCL == null) {
callerCL = Thread.currentThread().getContextClassLoader();
}
What's Thread.currentThread().getContextClassLoader()
for? I have gone through the document and can't understand it.
Thanks.