-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[Fix][Connector-JDBC] Fix JDBC driver selection for data source connections #8986
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
Conversation
|
@jinkachy thanks for your contribution, please also update the document to describe this feature, and add test case. |
|
Thank you for the review and feedback. I've updated the documentation to describe this feature and added a test case that verifies the driver selection mechanism. The test confirms that our implementation correctly prioritizes explicitly specified drivers regardless of their registration order in DriverManager. @liunaijie |
| Driver driver = drivers.nextElement(); | ||
| if (StringUtils.equals(driver.getClass().getName(), driverClass)) { | ||
| try { | ||
| return driver.connect(url, info); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the connection should be put into connection map to reused. Please refer https://github.com/apache/seatunnel/pull/8986/files#diff-fea654341466928ce751051d2a2b1718e1d267834b9468ddc02c15ba87922ac6R154
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the connection should be put into connection map to reused. Please refer https://github.com/apache/seatunnel/pull/8986/files#diff-fea654341466928ce751051d2a2b1718e1d267834b9468ddc02c15ba87922ac6R154
@Hisoka-X thanks, you are right. done
5b3b85b to
542b26b
Compare
Hisoka-X
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Purpose of this pull request
This pull request fixes an issue with JDBC driver selection when dealing with heterogeneous data sources. In the current implementation, DriverManager may select the wrong driver when multiple compatible drivers are available in the classpath. This can cause problems when transferring data between similar databases (like Greenplum to PostgreSQL or MySQL 5 to MySQL 8), where driver "drift" can occur.
The underlying issue exists in the DriverManager's getConnection method implementation: it iterates through all registered drivers and uses the first one that successfully connects to the given URL. As shown in the Java source code, it doesn't prioritize drivers by class name but simply tries each registered driver in sequence:
This behavior means that if multiple drivers can handle the same URL format (e.g., both MySQL 5 and MySQL 8 drivers can handle "jdbc:mysql://..." URLs), the one loaded first will be used, regardless of which one is more appropriate for the specific database version being connected to.
The fix explicitly finds and uses the driver specified by class name before falling back to the default DriverManager behavior, ensuring that the correct driver is used for the connection.
Does this PR introduce any user-facing change?
This PR improves the reliability of JDBC connections when working with heterogeneous data sources, but doesn't change any user-facing API or configuration. Users may notice improved stability when transferring data between similar database systems that require different drivers.
How was this patch tested?
The implementation was tested with various database connections where driver drift could occur, particularly focusing on:
The tests verified that the correct driver is selected when explicitly specified, even when multiple compatible drivers are available.
Check list
org.apache.seatunnel.connectors.seatunnel.jdbc.catalog.AbstractJdbcCatalog#getConnectionmethod, prioritizing the use of the specified driver class to establish connections and avoid driver drift issuesdriverClassparameter is correctly passed to the getConnection method, allowing the specified driver priority to take effect