qiong-zhou commented on issue #17464: URL: https://github.com/apache/dolphinscheduler/issues/17464#issuecomment-3510183312
> > > Using lock can avoid this? > > > > > > I don't think this is a good idea or that it's necessary. Based on the information I've reviewed, it's generally accepted that manually calling Class.forName to load the driver class is no longer required in Java 6 and later versions. Of course, I can't guarantee this applies to all data source types without issues. If you consider this to be a risk, I've also looked into Storm's approach for your reference. https://issues.apache.org/jira/browse/STORM-2527 > > OK, I got you, you are right, we don't need to call `Class.forName` here, it just registers the driver into DriverManager. This seems bug in driver, we should remove Class.forName here. > > In additional, we might better initialize the driver and use the driver to getConnection, i am not sure if the DriverManager can find the suitable Driver, since multiple driver might be matched. DriverManager will match the suitable Driver by the connection Url `jdbc:mysql://localhost:3306/dolphinscheduler` `jdbc:postgresql://localhost:5432/dolphinscheduler` and invoke all the registered drivers' acceptsURL method to find out if it is the matched driver. This is the driver match method in DriverManger ```java public static Driver getDriver(String url) throws SQLException { println("DriverManager.getDriver(\"" + url + "\")"); Class<?> callerClass = Reflection.getCallerClass(); // Walk through the loaded registeredDrivers attempting to locate someone // who understands the given URL. for (DriverInfo aDriver : registeredDrivers) { // If the caller does not have permission to load the driver then // skip it. if(isDriverAllowed(aDriver.driver, callerClass)) { try { if(aDriver.driver.acceptsURL(url)) { // Success! println("getDriver returning " + aDriver.driver.getClass().getName()); return (aDriver.driver); } } catch(SQLException sqe) { // Drop through and try the next driver. } } else { println(" skipping: " + aDriver.driver.getClass().getName()); } } println("getDriver: no suitable driver"); throw new SQLException("No suitable driver", "08001"); } ``` So just delete `Class.forName` and let the DriverManager do the driver class loading job will be better. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
