Skip to content

Fix buildJdbcUrl to apply connection properties #651

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

seoyoniee
Copy link

Description

Fixing buildJdbcUrl to apply connection properties

Additional context and related issues

fixing #650

Release notes

( ) This is not user-visible or is docs only, and no release notes are required.
( ) Release notes are required. Please propose a release note for me.
(x) Release notes are required, with the following suggested text:

* Fix buildJdbcUrl to apply connection properties (https://github.com/trinodb/trino-gateway/issues/650)

Copy link

cla-bot bot commented Mar 26, 2025

Thank you for your pull request and welcome to the Trino community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. Continue to work with us on the review and improvements in this PR, and submit the signed CLA to [email protected]. Photos, scans, or digitally-signed PDF files are all suitable. Processing may take a few days. The CLA needs to be on file before we merge your changes. For more information, see https://github.com/trinodb/cla

@seoyoniee
Copy link
Author

@cla-bot check

Copy link

cla-bot bot commented Apr 4, 2025

Thank you for your pull request and welcome to the Trino community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. Continue to work with us on the review and improvements in this PR, and submit the signed CLA to [email protected]. Photos, scans, or digitally-signed PDF files are all suitable. Processing may take a few days. The CLA needs to be on file before we merge your changes. For more information, see https://github.com/trinodb/cla

Copy link

cla-bot bot commented Apr 4, 2025

The cla-bot has been summoned, and re-checked this pull request!

@mosabua
Copy link
Member

mosabua commented Apr 8, 2025

@cla-bot check

@cla-bot cla-bot bot added the cla-signed label Apr 8, 2025
Copy link

cla-bot bot commented Apr 8, 2025

The cla-bot has been summoned, and re-checked this pull request!

Copy link
Member

@mosabua mosabua left a comment

Choose a reason for hiding this comment

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

I think this requires a pretty static format for the JDBC URL. Can you confirm that this works for MySQL, Oracle, and PostgreSQL as we suggest. At a minimum I think we should add some simple test to verify that the property you reported as failing now works in terms of passing it through. Overall I am ok with this approach though. Might be worth looking in the Trino codebase if we have any similar setup and tests for it.

@seoyoniee
Copy link
Author

@mosabua
As you pointed out, the code I modified previously had a pretty static format and wouldn't work well for various database, especially Oracle. So I made two modifications:

  1. re-modified buildJdbcUrl to ensure it works correctly even when query parameters contain slashes 083ee64
  2. I also added test code for h2, mysql, Oracle, and PostgreSQL 5c0b9dd

Copy link
Member

@mosabua mosabua left a comment

Choose a reason for hiding this comment

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

This looks good to me now. Can you squash commits @seoyoniee ?

Anything else to change @ebyhr ?

@seoyoniee seoyoniee force-pushed the fix-build-jdbc-url branch from 01de0f9 to f38be06 Compare May 14, 2025 23:18
@seoyoniee seoyoniee changed the title Fixing buildJdbcUrl to apply connection properties Fix buildJdbcUrl to apply connection properties May 14, 2025

final class TestJdbcConnectionManager
{
private JdbcConnectionManager createConnectionManager(String jdbcUrl)
Copy link
Member

Choose a reason for hiding this comment

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

Please move this method to the bottom. We locate helper method after the caller in this project.

Copy link
Author

Choose a reason for hiding this comment

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

@@ -60,11 +63,26 @@ public Jdbi getJdbi(@Nullable String routingGroupDatabase)
.registerRowMapper(new RecordAndAnnotatedConstructorMapper());
}

private String buildJdbcUrl(@Nullable String routingGroupDatabase)
public String buildJdbcUrl(@Nullable String routingGroupDatabase)
Copy link
Member

Choose a reason for hiding this comment

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

The package-private is sufficient, right? Please add @VisibleForTesting annotation.

Copy link
Author

Choose a reason for hiding this comment

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

yes right! 7d2d2ae

}
catch (URISyntaxException e) {
throw new RuntimeException(e);
}
Copy link
Member

Choose a reason for hiding this comment

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

Please return jdbcUrl early when routingGroupDatabase is null so we can avoid redundant nesting:

        if (routingGroupDatabase == null) {
            return jdbcUrl;
        }

        try {
...

Copy link
Author

Choose a reason for hiding this comment

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


final class TestJdbcConnectionManager
{
private JdbcConnectionManager createConnectionManager(String jdbcUrl)
Copy link
Member

Choose a reason for hiding this comment

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

This method can be static.

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Member

@Chaho12 Chaho12 left a comment

Choose a reason for hiding this comment

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

Can you add some failures in test cases? For incorrect jdbc urls?

@@ -60,13 +64,29 @@ public Jdbi getJdbi(@Nullable String routingGroupDatabase)
.registerRowMapper(new RecordAndAnnotatedConstructorMapper());
}

private String buildJdbcUrl(@Nullable String routingGroupDatabase)
@VisibleForTesting
String buildJdbcUrl(@Nullable String routingGroupDatabase)
{
String jdbcUrl = configuration.getJdbcUrl();
Copy link
Member

Choose a reason for hiding this comment

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

maybe check?

if (jdbcUrl == null) {
        throw new IllegalArgumentException("JDBC URL cannot be null");
    }

Comment on lines +87 to +88
catch (URISyntaxException e) {
throw new RuntimeException(e);
Copy link
Member

Choose a reason for hiding this comment

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

Hey @ebyhr WDYT of catching more broder exception like RuntimeException or Exception
Because NPE could happen if jdbcUrl is none, or IndexOutOfBounds if / is not present, or IllegalArgumentException if Path.of(uri.getPath()) is malformed path.

Suggested change
catch (URISyntaxException e) {
throw new RuntimeException(e);
catch (Exception e) { // catch all
Logger.get(getClass()).error(e, "Failed to build JDBC URL for routing group '%s'", routingGroupDatabase);
throw new RuntimeException(e);
}

return jdbcUrl;
}
try {
int index = jdbcUrl.indexOf("/") + 1;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add check?

Suggested change
int index = jdbcUrl.indexOf("/") + 1;
int index = jdbcUrl.indexOf("/") + 1;
if (index < 0) {
throw new IllegalArgumentException("Invalid JDBC URL: no '/' found in " + jdbcUrl);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

5 participants