Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SQLLINE-5] Use ServiceLoader to load drivers rather than forName #235

Closed
wants to merge 3 commits into from

Conversation

snuyanzin
Copy link
Collaborator

@snuyanzin snuyanzin commented Dec 20, 2018

The PR implements usage of drivers mentioned by META-INF/services/java.sql.Driver

Also it allows to see errors in case of drivers loading which is impossible for current version.

Checked with latest drivers

  • mysql
  • oracle
  • avatica
  • calcite
  • phoenix

fixes #5

@snuyanzin snuyanzin changed the title Use ServiceLoader to load drivers rather than forName [SQLLINE-5] Use ServiceLoader to load drivers rather than forName Dec 20, 2018
@@ -200,6 +201,7 @@ public String getVersion() {
*
* @see #DEFAULT_DRIVERS
*/
@Deprecated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@snuyanzin how list of known drivers can be overridden without using depicted method?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please clarify what you mean?

Before this PR known drivers used to filter classes from classpath and to load drivers if they are matched to known drivers.
After PR changes java service provider functionality [1] will be used, i.e. java could go through jars looking for META-INF/services/java.sql.Driver and if success then load the driver.
So each driver which has such file inside its jar becomes known. It will allow to add more drivers in classpath and load them automatically without code changes

[1] https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Drill we were overriding all known drivers to only one - "org.apache.drill.jdbc.Driver" since Drill SqlLine is intended only for Apache Drill.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like for your case it is required to have limit which could be done via Application#initDrivers. What about logic like if Application#initDrivers is empty then any driver found via servicelocator is allowed. If Application#initDrivers is not empty then only drivers which are present in both Application#initDrivers and servicelocator are allowed.

However the name Application#initDrivers becomes not matching to what it started to do. So it makes sense to add a new method with more suitable name and leave it deprecated and made call to the new one as it is public.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, just indicate in Javadoc which new method should be used.

@julianhyde
Copy link
Owner

Don't represent "allow any driver" with empty collection. That causes a discontinuity going from 0 to 1 element. Use the null collection.

How about using a List rather than a Collection? Some applications that have a strong preference of one driver over another, so order might be important.

Please continue to add separate commits rather than squashing. I have already started reviewing the first commit, so separate follow-up commits will be easier for me to merge.

@snuyanzin
Copy link
Collaborator Author

Thank you for your comment.
Changed Collection to List and started to use null collection to represent "allow any driver".
Added several more tests to check the work of limitation

@julianhyde
Copy link
Owner

The two most recent commits have @snuyanzin's email address but use the name "Ekaterina". Do I need to preserve that authorship information or can I squash the commits?

@snuyanzin
Copy link
Collaborator Author

snuyanzin commented Jan 8, 2019

:) it can be squashed
sorry..
looks like I forgot to change git username from default while working on my wife's laptop

@julianhyde
Copy link
Owner

Merged as b053ffe, fixes #5.

@julianhyde julianhyde closed this Jan 9, 2019
@snuyanzin snuyanzin deleted the SQLLINE_5 branch January 15, 2019 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

revisit how JDBC drivers are loaded
3 participants