com.mysql.cj.conf

## Class ConnectionUrl

- java.lang.Object
- - com.mysql.cj.conf.ConnectionUrl

All Implemented Interfaces:
  DatabaseUrlContainer

    public abstract class ConnectionUrl
    extends Object
    implements DatabaseUrlContainer

A container for a database URL and a collection of given connection arguments. The connection string is parsed and split by its components, each of which is then processed and fixed according to the needs of the connection type. This abstract class holds all common behavior to all connection string types. Its subclasses must implement their own specifics such as classifying hosts by type or apply validation rules.

### Nested Class Summary

Nested Classes
`static class` `ConnectionUrl.HostsCardinality` The rules describing the number of hosts a database URL may contain.
`static class` `ConnectionUrl.Type` The database URL type which is determined by the scheme section of the connection string.

### Field Summary

Fields
`private static LRUCache<String,ConnectionUrl>` `connectionUrlCache`
`static String` `DEFAULT_HOST`
`static int` `DEFAULT_PORT`
`protected List<HostInfo>` `hosts`
`protected String` `originalConnStr`
`protected String` `originalDatabase`
`protected Map<String,String>` `properties`
`(package private) ConnectionPropertiesTransform` `propertiesTransformer`
`private static ReadWriteLock` `rwLock`
`protected ConnectionUrl.Type` `type`

### Constructor Summary

Constructors
`protected` `ConnectionUrl()` Empty constructor.
`protected` `ConnectionUrl(ConnectionUrlParser connStrParser, Properties info)` Constructs an instance of `ConnectionUrl`, performing all the required initializations.
`` `ConnectionUrl(String origUrl)` Constructor for unsupported URLs

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`static boolean` `acceptsUrl(String connString)` Checks if this `ConnectionUrl` is able to process the given database URL.
`private static String` `buildConnectionStringCacheKey(String connString, Properties info)` Builds a connection URL cache map key based on the connection string itself plus the string representation of the given connection properties.
`protected HostInfo` `buildHostInfo(String host, int port, String user, String password, Map<String,String> hostProps)` Creates a new `HostInfo` structure with the given components, passing through the properties transformer if there is one defined in this connection string;
`protected void` `collectHostsInfo(ConnectionUrlParser connStrParser)` Collects the hosts information from the `ConnectionUrlParser`.
`protected void` `collectProperties(ConnectionUrlParser connStrParser, Properties info)` Joins the connection arguments from the connection string with the ones from the given connection arguments map collecting them in a single map.
`protected void` `expandPropertiesFromConfigFiles(Map<String,String> props)` Expands the connection argument "useConfig" by reading the mentioned configuration files.
`protected HostInfo` `fixHostInfo(HostInfo hi)` Fixes the host information by moving data around and filling in missing data.
`protected void` `fixProtocolDependencies(Map<String,String> hostProps)` Fixes the protocol (TCP vs PIPE) dependencies for the given host properties map.
`Properties` `getConnectionArgumentsAsProperties()` Returns a `Properties` instance containing the connection arguments extracted from the URL query section, i.e., per host attributes are excluded.
`static ConnectionUrl` `getConnectionUrlInstance(String connString, Properties info)` Static factory method that returns either a new instance of a `ConnectionUrl` or a cached one.
`String` `getDatabase()` Returns the database from this connection URL.
`String` `getDatabaseUrl()` Returns the original database URL that produced this connection string.
`String` `getDefaultHost()` Returns the default host.
`String` `getDefaultPassword()` Returns the default password.
`int` `getDefaultPort()` Returns the default port.
`String` `getDefaultUser()` Returns the default user.
`HostInfo` `getHostOrSpawnIsolated(String hostPortPair)` Returns an existing host info with the same host:port part or spawns a new isolated host info based on this connection URL if none was found.
`HostInfo` `getHostOrSpawnIsolated(String hostPortPair, List<HostInfo> hostsList)` Returns an existing host info with the same host:port part or spawns a new isolated host info based on this connection URL if none was found.
`List<HostInfo>` `getHostsList()` Returns a list of the hosts in this connection URL.
`List<HostInfo>` `getHostsList(HostsListView view)` Returns a list of the hosts in this connection URL, filtered for the given view.
`List<HostInfo>` `getHostsListFromDnsSrv(HostInfo srvHost)` Returns a hosts list built from the result of the DNS SRV lookup for the original host name.
`HostInfo` `getMainHost()` Returns the single or first host info structure.
`Map<String,String>` `getOriginalProperties()` Returns the original (common to all hosts) connection arguments as provided in the connection string query section.
`static Properties` `getPropertiesFromConfigFiles(String configFiles)` Returns a map containing the properties read from the given configuration files.
`ConnectionUrl.Type` `getType()` Returns this connection URL type.
`int` `hostsCount()` Returns the number of hosts in this connection URL.
`protected void` `injectPerTypeProperties(Map<String,String> props)` Subclasses must override this method if they need to inject additional properties in the connection arguments map while it's being constructed.
`protected void` `preprocessPerTypeHostProperties(Map<String,String> hostProps)` Subclasses should override this to perform any required pre-processing on the host information properties.
`protected void` `replaceLegacyPropertyValues(Map<String,String> props)` Some acceptable property values have changed in c/J 8.0 but old values remain hard-coded in widely used software.
`protected void` `setupPropertiesTransformer()` Sets up the `ConnectionPropertiesTransform` if one was provided.
`private List<HostInfo>` `srvRecordsToHostsList(List<DnsSrv.SrvRecord> srvRecords, HostInfo baseHostInfo)` Converts a list of DNS SRV records into a hosts list.
`String` `toString()` Returns a string representation of this object.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Field Detail

#### DEFAULT_HOST

    public static final String DEFAULT_HOST

See Also:
  Constant Field Values

#### DEFAULT_PORT

    public static final int DEFAULT_PORT

See Also:
  Constant Field Values

#### connectionUrlCache

    private static final LRUCache<String,ConnectionUrl> connectionUrlCache

#### rwLock

    private static final ReadWriteLock rwLock

#### type

    protected ConnectionUrl.Type type

#### originalConnStr

    protected String originalConnStr

#### originalDatabase

    protected String originalDatabase

#### hosts

    protected List<HostInfo> hosts

#### properties

    protected Map<String,String> properties

#### propertiesTransformer

    ConnectionPropertiesTransform propertiesTransformer

### Constructor Detail

#### ConnectionUrl

    protected ConnectionUrl()

Empty constructor. Required for subclasses initialization.

#### ConnectionUrl

    public ConnectionUrl(String origUrl)

Constructor for unsupported URLs

Parameters:
  `origUrl` - URLs

#### ConnectionUrl

    protected ConnectionUrl(ConnectionUrlParser connStrParser,
                            Properties info)

Constructs an instance of `ConnectionUrl`, performing all the required initializations.

Parameters:
  `connStrParser` - a `ConnectionUrlParser` instance containing the parsed version of the original connection string
  `info` - the connection arguments map

### Method Detail

#### getConnectionUrlInstance

    public static ConnectionUrl getConnectionUrlInstance(String connString,
                                                         Properties info)

Static factory method that returns either a new instance of a `ConnectionUrl` or a cached one. Returns "null" it can't handle the connection string.

Parameters:
  `connString` - the connection string
  `info` - the connection arguments map
Returns:
  an instance of a `ConnectionUrl` or "null" if isn't able to handle the connection string

#### buildConnectionStringCacheKey

    private static String buildConnectionStringCacheKey(String connString,
                                                        Properties info)

Builds a connection URL cache map key based on the connection string itself plus the string representation of the given connection properties.

Parameters:
  `connString` - the connection string
  `info` - the connection arguments map
Returns:
  a connection string cache map key

#### acceptsUrl

    public static boolean acceptsUrl(String connString)

Checks if this `ConnectionUrl` is able to process the given database URL.

Parameters:
  `connString` - the connection string
Returns:
  true if this class is able to process the given URL, false otherwise

#### collectProperties

    protected void collectProperties(ConnectionUrlParser connStrParser,
                                     Properties info)

Joins the connection arguments from the connection string with the ones from the given connection arguments map collecting them in a single map. Additionally may also collect other connection arguments from configuration files.

Parameters:
  `connStrParser` - the `ConnectionUrlParser` from where to collect the properties
  `info` - the connection arguments map

#### setupPropertiesTransformer

    protected void setupPropertiesTransformer()

Sets up the `ConnectionPropertiesTransform` if one was provided.

#### expandPropertiesFromConfigFiles

    protected void expandPropertiesFromConfigFiles(Map<String,String> props)

Expands the connection argument "useConfig" by reading the mentioned configuration files.

Parameters:
  `props` - a connection arguments map from where to read the "useConfig" property and where to save the loaded properties.

#### getPropertiesFromConfigFiles

    public static Properties getPropertiesFromConfigFiles(String configFiles)

Returns a map containing the properties read from the given configuration files. Multiple files can be referenced using a comma as separator.

Parameters:
  `configFiles` - the list of the configuration files to read
Returns:
  the map containing all the properties read

#### injectPerTypeProperties

    protected void injectPerTypeProperties(Map<String,String> props)

Subclasses must override this method if they need to inject additional properties in the connection arguments map while it's being constructed.

Parameters:
  `props` - the properties already containing all known connection arguments

#### replaceLegacyPropertyValues

    protected void replaceLegacyPropertyValues(Map<String,String> props)

Some acceptable property values have changed in c/J 8.0 but old values remain hard-coded in widely used software. So, old values must be accepted and translated to new ones.

Parameters:
  `props` - the host properties map to fix

#### collectHostsInfo

    protected void collectHostsInfo(ConnectionUrlParser connStrParser)

Collects the hosts information from the `ConnectionUrlParser`.

Parameters:
  `connStrParser` - the `ConnectionUrlParser` from where to collect the hosts information

#### fixHostInfo

    protected HostInfo fixHostInfo(HostInfo hi)

Fixes the host information by moving data around and filling in missing data. Applies properties transformations to the collected properties if `ConnectionPropertiesTransform` was declared in the connection arguments.

Parameters:
  `hi` - the host information data to fix
Returns:
  a new `HostInfo` with all required data

#### preprocessPerTypeHostProperties

    protected void preprocessPerTypeHostProperties(Map<String,String> hostProps)

Subclasses should override this to perform any required pre-processing on the host information properties.

Parameters:
  `hostProps` - the host properties map to process

#### getDefaultHost

    public String getDefaultHost()

Returns the default host. Subclasses must override this method if they have different default host value.

Returns:
  the default host

#### getDefaultPort

    public int getDefaultPort()

Returns the default port. Subclasses must override this method if they have different default port value.

Returns:
  the default port

#### getDefaultUser

    public String getDefaultUser()

Returns the default user. Usually the one provided in the method `DriverManager.getConnection(String, String, String)` or as connection argument.

Returns:
  the default user

#### getDefaultPassword

    public String getDefaultPassword()

Returns the default password. Usually the one provided in the method `DriverManager.getConnection(String, String, String)` or as connection argument.

Returns:
  the default password

#### fixProtocolDependencies

    protected void fixProtocolDependencies(Map<String,String> hostProps)

Fixes the protocol (TCP vs PIPE) dependencies for the given host properties map.

Parameters:
  `hostProps` - the host properties map to fix

#### getType

    public ConnectionUrl.Type getType()

Returns this connection URL type.

Returns:
  the connection URL type

#### getDatabaseUrl

    public String getDatabaseUrl()

Returns the original database URL that produced this connection string.

Specified by:
  `getDatabaseUrl` in interface `DatabaseUrlContainer`
Returns:
  the original database URL

#### getDatabase

    public String getDatabase()

Returns the database from this connection URL. Note that a "DBNAME" property overrides the database identified in the connection string.

Returns:
  the database name

#### hostsCount

    public int hostsCount()

Returns the number of hosts in this connection URL.

Returns:
  the number of hosts

#### getMainHost

    public HostInfo getMainHost()

Returns the single or first host info structure.

Returns:
  the first host info structure

#### getHostsList

    public List<HostInfo> getHostsList()

Returns a list of the hosts in this connection URL.

Returns:
  the hosts list from this connection URL

#### getHostsList

    public List<HostInfo> getHostsList(HostsListView view)

Returns a list of the hosts in this connection URL, filtered for the given view. By default returns all hosts. Subclasses should override this method in order to implement support for different views, usually by splitting the global hosts into smaller sub-lists.

Parameters:
  `view` - the type of the view to use in the returned list of hosts. This argument is ignored in this implementation.
Returns:
  the hosts list from this connection URL, filtered for the given view.

#### getHostOrSpawnIsolated

    public HostInfo getHostOrSpawnIsolated(String hostPortPair)

Returns an existing host info with the same host:port part or spawns a new isolated host info based on this connection URL if none was found.

Parameters:
  `hostPortPair` - the host:port part to search for
Returns:
  the existing host info or a new independent one

#### getHostOrSpawnIsolated

    public HostInfo getHostOrSpawnIsolated(String hostPortPair,
                                           List<HostInfo> hostsList)

Returns an existing host info with the same host:port part or spawns a new isolated host info based on this connection URL if none was found.

Parameters:
  `hostPortPair` - the host:port part to search for
  `hostsList` - the hosts list from where to search the host list
Returns:
  the existing host info or a new independent one

#### buildHostInfo

    protected HostInfo buildHostInfo(String host,
                                     int port,
                                     String user,
                                     String password,
                                     Map<String,String> hostProps)

Creates a new `HostInfo` structure with the given components, passing through the properties transformer if there is one defined in this connection string;

Parameters:
  `host` - the host
  `port` - the port
  `user` - the user name
  `password` - the password
  `hostProps` - the host properties map
Returns:
  a new instance of `HostInfo`

#### getOriginalProperties

    public Map<String,String> getOriginalProperties()

Returns the original (common to all hosts) connection arguments as provided in the connection string query section.

Returns:
  the original (common to all hosts) connection arguments

#### getConnectionArgumentsAsProperties

    public Properties getConnectionArgumentsAsProperties()

Returns a `Properties` instance containing the connection arguments extracted from the URL query section, i.e., per host attributes are excluded. Applies properties transformations to the collected properties if `ConnectionPropertiesTransform` was declared in the connection arguments.

Returns:
  a `Properties` instance containing the common connection arguments.

#### getHostsListFromDnsSrv

    public List<HostInfo> getHostsListFromDnsSrv(HostInfo srvHost)

Returns a hosts list built from the result of the DNS SRV lookup for the original host name.

Parameters:
  `srvHost` - the `HostInfo` from where to get the DNS SRV service name to lookup.
Returns:
  the hosts list from the result of the DNS SRV lookup, filtered for the given view.

#### srvRecordsToHostsList

    private List<HostInfo> srvRecordsToHostsList(List<DnsSrv.SrvRecord> srvRecords,
                                                 HostInfo baseHostInfo)

Converts a list of DNS SRV records into a hosts list.

Parameters:
  `srvRecords` - the list of DNS SRV records.
  `baseHostInfo` - the `HostInfo` to use as source of all common host specific options.
Returns:
  a list of hosts.

#### toString

    public String toString()

Returns a string representation of this object.

Overrides:
  `toString` in class `Object`
Returns:
  a string representation of this object

Copyright © 2016, 2026, Oracle and/or its affiliates.