Why is getting conn in DriverManager's getConnection source code an inefficient way to traverse registered driver?

The key code of

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?

Jul.15,2021

Visual yes, at least with the implementation of hikari, that's not how the connection works

.
  • How java executes sql scripts and passes parameters

    as shown below, use ant.jar to connect to the oracle database and execute the sql script, but if my sql script takes in parameters (for example, through sqlplus: sqlplus user password@db @ test.sql A B C ), it seems that script parameters are not suppo...

    Jun.18,2021
Menu