Re: Aletrnative to ParseURL in Connector/J version
Posted by: Filipe Silva
Date: November 28, 2023 02:24PM

OK, good to know.

You are right. Host and user info are now detached from the main properties map. This is so because of several reasons and there's no point in going through them right now.

Let me show you an example of what you could do:

public static void main(String[] args) throws Exception {
    ConnectionUrl connUrl = ConnectionUrl.getConnectionUrlInstance(
                "jdbc:mysql:loadbalance://user1:password1@host1.mydomain.demo:10000,user2:password2@(host=host2.mydomain.demo,port=20000,prop0=value0)/mydb?prop1=value1&prop2=value2", null);

    System.out.println("Common properties: " + connUrl.getOriginalProperties());
    System.out.println("------------------------------");
    for (HostInfo hi : connUrl.getHostsList()) {
        System.out.println("Host specific info:");
        System.out.println(hi.toString());
        System.out.println("Host specific properties: " + hi.getHostProperties());
        System.out.println("Host: " + hi.getHost());
        System.out.println("Port: " + hi.getPort());
        System.out.println("User: " + hi.getUser());
        System.out.println("Password: " + hi.getPassword());
        System.out.println("Database (also in the properties map): " + hi.getDatabase());
        System.out.println("------------------------------");
    }
}

As you can see, individual hosts can use a different set of connection properties, so all that information has to be host dependent.

For the purpose of your needs in `tryParseMySqlConnectionUri(String connectionUri)` you'll have to decide how to handle this. Maybe you can take the properties from each one of the specified hosts and create a super set from the union of all keys. Of course you'd also have to manually add the keys "host", "port", "user" and/or "password", if you need them.

Note that the connection string above is just an example and you can actually specify the user name and password through common connection properties, e.g.:
"jdbc:mysql://myhost:12345/mydb?user=myuser&password=mypassword". Check the link I shared before to learn about all the connection string syntaxes supported in Connector/J.

I have to warn you again, though. JDBC drivers are not expected to expose this information so these internal Connector/J classes you are using here can change without any notice and break your code again in the future. The alternative would be to parse connection strings by yourself. Please keep that in mind.

I hope this helps.

Options: ReplyQuote




Sorry, only registered users may post in this forum.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.