Search in sources :

Example 21 with ConnectionUrl

use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.

the class MysqlDataSource method getConnection.

/**
 * Creates a connection using the specified properties.
 *
 * @param props
 *            the properties to connect with
 *
 * @return a connection to the database
 *
 * @throws SQLException
 *             if an error occurs
 */
protected java.sql.Connection getConnection(Properties props) throws SQLException {
    String jdbcUrlToUse = this.explicitUrl ? this.url : getUrl();
    // 
    // URL should take precedence over properties
    // 
    ConnectionUrl connUrl = ConnectionUrl.getConnectionUrlInstance(jdbcUrlToUse, null);
    Properties urlProps = connUrl.getConnectionArgumentsAsProperties();
    urlProps.remove(PropertyKey.HOST.getKeyName());
    urlProps.remove(PropertyKey.PORT.getKeyName());
    urlProps.remove(PropertyKey.DBNAME.getKeyName());
    urlProps.stringPropertyNames().stream().forEach(k -> props.setProperty(k, urlProps.getProperty(k)));
    return mysqlDriver.connect(jdbcUrlToUse, props);
}
Also used : ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) Properties(java.util.Properties)

Example 22 with ConnectionUrl

use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.

the class TestXDevAPIRequirements method testSessionCreation.

/**
 * Session [11]
 * Session.Connect.Single [6]
 * Session.Connect.DataSource [7]
 * Session.Connect.Mysqls [8] [9] - not supported in first version
 *
 * @throws Exception
 */
@Test
public void testSessionCreation() throws Exception {
    if (!this.isSetForXTests) {
        return;
    }
    Session sess;
    String url = this.baseUrl;
    sess = getSession(url);
    sess.close();
    // TODO test different URLs
    ConnectionUrl conUrl = ConnectionUrl.getConnectionUrlInstance(url, null);
    Properties props = conUrl.getMainHost().exposeAsProperties();
    sess = getSession(props);
    sess.close();
    // test connection without port specification
    if (props.getProperty(PropertyKey.PORT.getKeyName()).equals("33060")) {
        props.remove(PropertyKey.PORT.getKeyName());
        sess = getSession(props);
        ConnectionUrl conUrl1 = ConnectionUrl.getConnectionUrlInstance(sess.getUri(), null);
        assertEquals("33060", conUrl1.getMainHost().exposeAsProperties().getProperty(PropertyKey.PORT.getKeyName()));
        sess.close();
    }
// TODO test different properties
}
Also used : ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) Properties(java.util.Properties) Session(com.mysql.cj.xdevapi.Session) Test(org.junit.jupiter.api.Test)

Example 23 with ConnectionUrl

use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.

the class FailoverConnectionPlugin method createClusterInstanceTemplate.

private HostInfo createClusterInstanceTemplate(HostInfo hostInfo, String host, int port) {
    // TODO: review whether we still need this method
    Map<String, String> properties = new HashMap<>(this.initialConnectionProps);
    properties.put(PropertyKey.connectTimeout.getKeyName(), String.valueOf(this.failoverConnectTimeoutMs));
    properties.put(PropertyKey.socketTimeout.getKeyName(), String.valueOf(this.failoverSocketTimeoutMs));
    if (!Objects.equals(hostInfo.getDatabase(), "")) {
        properties.put(PropertyKey.DBNAME.getKeyName(), hostInfo.getDatabase());
    }
    final Properties connectionProperties = new Properties();
    connectionProperties.putAll(this.initialConnectionProps);
    final ConnectionUrl connectionUrl = ConnectionUrl.getConnectionUrlInstance(hostInfo.getDatabaseUrl(), connectionProperties);
    return new HostInfo(connectionUrl, host, port, hostInfo.getUser(), hostInfo.getPassword(), hostInfo.isPasswordless(), properties);
}
Also used : HashMap(java.util.HashMap) ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) Properties(java.util.Properties) HostInfo(com.mysql.cj.conf.HostInfo)

Example 24 with ConnectionUrl

use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.

the class AuroraTopologyService method createHost.

private HostInfo createHost(ResultSet resultSet) throws SQLException {
    String hostEndpoint = getHostEndpoint(resultSet.getString(FIELD_SERVER_ID));
    ConnectionUrl hostUrl = ConnectionUrl.getConnectionUrlInstance(getUrlFromEndpoint(hostEndpoint, this.clusterInstanceTemplate.getPort(), this.clusterInstanceTemplate.getDatabase()), new Properties());
    return new HostInfo(hostUrl, hostEndpoint, this.clusterInstanceTemplate.getPort(), this.clusterInstanceTemplate.getUser(), this.clusterInstanceTemplate.getPassword(), this.clusterInstanceTemplate.isPasswordless(), getPropertiesFromTopology(resultSet));
}
Also used : ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) Properties(java.util.Properties) HostInfo(com.mysql.cj.conf.HostInfo)

Example 25 with ConnectionUrl

use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.

the class AWSSecretsManagerPlugin method attemptConnectionWithSecrets.

private void attemptConnectionWithSecrets(Properties props, Secret secret, ConnectionUrl connectionUrl) throws SQLException {
    updateConnectionProperties(props, secret);
    final ConnectionUrl newConnectionUrl = ConnectionUrl.getConnectionUrlInstance(connectionUrl.getDatabaseUrl(), props);
    this.nextPlugin.openInitialConnection(newConnectionUrl);
}
Also used : ConnectionUrl(com.mysql.cj.conf.ConnectionUrl)

Aggregations

ConnectionUrl (com.mysql.cj.conf.ConnectionUrl)49 Test (org.junit.jupiter.api.Test)41 Properties (java.util.Properties)39 HostInfo (com.mysql.cj.conf.HostInfo)21 SQLException (java.sql.SQLException)10 ReplicationConnectionUrl (com.mysql.cj.conf.url.ReplicationConnectionUrl)9 JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)9 ResultSet (java.sql.ResultSet)8 Statement (java.sql.Statement)8 ArrayList (java.util.ArrayList)8 FailoverConnectionUrl (com.mysql.cj.conf.url.FailoverConnectionUrl)7 FailoverDnsSrvConnectionUrl (com.mysql.cj.conf.url.FailoverDnsSrvConnectionUrl)7 LoadBalanceConnectionUrl (com.mysql.cj.conf.url.LoadBalanceConnectionUrl)7 LoadBalanceDnsSrvConnectionUrl (com.mysql.cj.conf.url.LoadBalanceDnsSrvConnectionUrl)7 ReplicationDnsSrvConnectionUrl (com.mysql.cj.conf.url.ReplicationDnsSrvConnectionUrl)7 SingleConnectionUrl (com.mysql.cj.conf.url.SingleConnectionUrl)7 XDevApiConnectionUrl (com.mysql.cj.conf.url.XDevApiConnectionUrl)7 XDevApiDnsSrvConnectionUrl (com.mysql.cj.conf.url.XDevApiDnsSrvConnectionUrl)7 ReplicationConnection (com.mysql.cj.jdbc.ha.ReplicationConnection)6 Connection (java.sql.Connection)6