Search in sources :

Example 6 with SqlConnectOptions

use of io.vertx.sqlclient.SqlConnectOptions in project vertx-sql-client by eclipse-vertx.

the class DB2Driver method newPoolImpl.

private PoolImpl newPoolImpl(VertxInternal vertx, List<? extends SqlConnectOptions> databases, PoolOptions options, CloseFuture closeFuture) {
    DB2ConnectOptions baseConnectOptions = DB2ConnectOptions.wrap(databases.get(0));
    QueryTracer tracer = vertx.tracer() == null ? null : new QueryTracer(vertx.tracer(), baseConnectOptions);
    VertxMetrics vertxMetrics = vertx.metricsSPI();
    ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(baseConnectOptions.getSocketAddress(), "sql", baseConnectOptions.getMetricsName()) : null;
    boolean pipelinedPool = options instanceof Db2PoolOptions && ((Db2PoolOptions) options).isPipelined();
    int pipeliningLimit = pipelinedPool ? baseConnectOptions.getPipeliningLimit() : 1;
    PoolImpl pool = new PoolImpl(vertx, this, tracer, metrics, pipeliningLimit, options, null, null, closeFuture);
    List<ConnectionFactory> lst = databases.stream().map(o -> createConnectionFactory(vertx, o)).collect(Collectors.toList());
    ConnectionFactory factory = ConnectionFactory.roundRobinSelector(lst);
    pool.connectionProvider(factory::connect);
    pool.init();
    closeFuture.add(factory);
    return pool;
}
Also used : SqlConnectOptions(io.vertx.sqlclient.SqlConnectOptions) VertxInternal(io.vertx.core.impl.VertxInternal) QueryTracer(io.vertx.sqlclient.impl.tracing.QueryTracer) Vertx(io.vertx.core.Vertx) ConnectionFactory(io.vertx.sqlclient.spi.ConnectionFactory) ContextInternal(io.vertx.core.impl.ContextInternal) Driver(io.vertx.sqlclient.spi.Driver) PoolImpl(io.vertx.sqlclient.impl.PoolImpl) Collectors(java.util.stream.Collectors) VertxMetrics(io.vertx.core.spi.metrics.VertxMetrics) PoolOptions(io.vertx.sqlclient.PoolOptions) Connection(io.vertx.sqlclient.impl.Connection) List(java.util.List) DB2Pool(io.vertx.db2client.DB2Pool) CloseFuture(io.vertx.core.impl.CloseFuture) DB2ConnectOptions(io.vertx.db2client.DB2ConnectOptions) io.vertx.db2client.impl(io.vertx.db2client.impl) JsonObject(io.vertx.core.json.JsonObject) ClientMetrics(io.vertx.core.spi.metrics.ClientMetrics) SqlConnectionInternal(io.vertx.sqlclient.impl.SqlConnectionInternal) VertxMetrics(io.vertx.core.spi.metrics.VertxMetrics) ConnectionFactory(io.vertx.sqlclient.spi.ConnectionFactory) QueryTracer(io.vertx.sqlclient.impl.tracing.QueryTracer) DB2ConnectOptions(io.vertx.db2client.DB2ConnectOptions) ClientMetrics(io.vertx.core.spi.metrics.ClientMetrics) PoolImpl(io.vertx.sqlclient.impl.PoolImpl)

Example 7 with SqlConnectOptions

use of io.vertx.sqlclient.SqlConnectOptions in project vertx-sql-client by eclipse-vertx.

the class PgConnectionAutoRetryTest method initialConnector.

@Override
protected void initialConnector(int proxyPort) {
    SqlConnectOptions proxyOptions = new PgConnectOptions(options);
    proxyOptions.setPort(proxyPort);
    proxyOptions.setHost("localhost");
    connectionConnector = ClientConfig.CONNECT.connect(vertx, proxyOptions);
    poolConnector = ClientConfig.POOLED.connect(vertx, proxyOptions);
}
Also used : PgConnectOptions(io.vertx.pgclient.PgConnectOptions) SqlConnectOptions(io.vertx.sqlclient.SqlConnectOptions)

Example 8 with SqlConnectOptions

use of io.vertx.sqlclient.SqlConnectOptions in project hibernate-reactive by hibernate.

the class DefaultSqlClientPoolConfiguration method connectOptions.

@Override
public SqlConnectOptions connectOptions(URI uri) {
    String scheme = uri.getScheme();
    String path = scheme.equals("oracle") ? oraclePath(uri) : uri.getPath();
    String database = path.length() > 0 ? path.substring(1) : "";
    if (scheme.equals("db2") && database.indexOf(':') > 0) {
        // DB2 URLs are a bit odd and have the format:
        // jdbc:db2://<HOST>:<PORT>/<DB>:key1=value1;key2=value2;
        database = database.substring(0, database.indexOf(':'));
    }
    String host = scheme.equals("oracle") ? oracleHost(uri) : uri.getHost();
    int port = scheme.equals("oracle") ? oraclePort(uri) : uri.getPort();
    int index = uri.toString().indexOf(';');
    if (scheme.equals("sqlserver") && index > 0) {
        // SQL Server separates parameters in the url with a semicolon (';')
        // and the URI class doesn't get the right value for host and port when the url
        // contains parameters
        URI uriWithoutParams = URI.create(uri.toString().substring(0, index));
        host = uriWithoutParams.getHost();
        port = uriWithoutParams.getPort();
    }
    if (port == -1) {
        port = defaultPort(scheme);
    }
    // see if the credentials were specified via properties
    String username = user;
    String password = pass;
    Map<String, String> extraProps = new HashMap<>();
    if (username == null || password == null) {
        // if not, look for URI-style user info first
        String userInfo = uri.getUserInfo();
        if (userInfo != null) {
            String[] bits = userInfo.split(":");
            username = bits[0];
            if (bits.length > 1) {
                password = bits[1];
            }
        } else {
            // check the query for named parameters
            // in case it's a JDBC-style URL
            String[] params = {};
            // jdbc:db2://<HOST>:<PORT>/<DB>:key1=value1;key2=value2;
            if (scheme.equals("db2")) {
                int queryIndex = uri.getPath().indexOf(':') + 1;
                if (queryIndex > 0) {
                    params = uri.getPath().substring(queryIndex).split(";");
                }
            } else if (scheme.contains("sqlserver")) {
                // SQL Server separates parameters in the url with a semicolon (';')
                // Ex: jdbc:sqlserver://<server>:<port>;<database>=AdventureWorks;user=<user>;password=<password>
                String query = uri.getQuery();
                String rawQuery = uri.getRawQuery();
                String s = uri.toString();
                int queryIndex = s.indexOf(';') + 1;
                if (queryIndex > 0) {
                    params = s.substring(queryIndex).split(";");
                }
            } else {
                final String query = scheme.equals("oracle") ? oracleQuery(uri) : uri.getQuery();
                if (query != null) {
                    params = query.split("&");
                }
            }
            for (String param : params) {
                if (param.startsWith("user=")) {
                    username = param.substring(5);
                } else if (param.startsWith("pass=")) {
                    password = param.substring(5);
                } else if (param.startsWith("password=")) {
                    password = param.substring(9);
                } else if (param.startsWith("database=")) {
                    database = param.substring(9);
                } else {
                    final int position = param.indexOf("=");
                    if (position != -1) {
                        // We assume the first '=' is the one separating key and value
                        String key = param.substring(0, position);
                        String value = param.substring(position + 1);
                        extraProps.put(key, value);
                    } else {
                        // A key without a value
                        extraProps.put(param, null);
                    }
                }
            }
        }
    }
    if (username == null) {
        throw new HibernateError("database username not specified (set the property 'javax.persistence.jdbc.user', or include it as a parameter in the connection URL)");
    }
    SqlConnectOptions connectOptions = new SqlConnectOptions().setHost(host).setPort(port).setDatabase(database).setUser(username);
    if (password != null) {
        connectOptions.setPassword(password);
    }
    for (String key : extraProps.keySet()) {
        connectOptions.addProperty(key, extraProps.get(key));
    }
    // enable the prepared statement cache by default
    connectOptions.setCachePreparedStatements(true);
    if (cacheMaxSize != null) {
        if (cacheMaxSize <= 0) {
            LOG.preparedStatementCacheDisabled();
            connectOptions.setCachePreparedStatements(false);
        } else {
            LOG.preparedStatementCacheMaxSize(cacheMaxSize);
            connectOptions.setCachePreparedStatements(true);
            connectOptions.setPreparedStatementCacheMaxSize(cacheMaxSize);
        }
    }
    if (sqlLimit != null) {
        LOG.preparedStatementCacheSQLLimit(sqlLimit);
        connectOptions.setPreparedStatementCacheSqlLimit(sqlLimit);
    }
    return connectOptions;
}
Also used : HibernateError(org.hibernate.HibernateError) HashMap(java.util.HashMap) SqlConnectOptions(io.vertx.sqlclient.SqlConnectOptions) ConfigurationHelper.getString(org.hibernate.internal.util.config.ConfigurationHelper.getString) URI(java.net.URI)

Example 9 with SqlConnectOptions

use of io.vertx.sqlclient.SqlConnectOptions in project hibernate-reactive by hibernate.

the class JdbcUrlParserTest method assertOptions.

/**
 * Create the default {@link SqlConnectOptions} with the given extra properties
 * and assert that's correct.
 */
private void assertOptions(String url, String expectedDbName, Map<String, String> parameters) {
    URI uri = DefaultSqlClientPool.parse(url);
    SqlConnectOptions options = new DefaultSqlClientPoolConfiguration().connectOptions(uri);
    // These keys won't be mapped as properties
    String username = parameters.remove("user");
    String password = parameters.remove("password");
    parameters.remove("database");
    assertThat(options).as("URL: " + url).isNotNull();
    assertThat(options.getUser()).as("URL: " + url).isEqualTo(username);
    assertThat(options.getPassword()).as("URL: " + url).isEqualTo(password);
    assertThat(options.getDatabase()).as("URL: " + url).isEqualTo(expectedDbName);
    assertThat(options.getHost()).as("URL: " + url).isEqualTo("localhost");
    assertThat(options.getPort()).as("URL: " + url).isEqualTo(dbType().getDefaultPort());
    // Check extra properties
    assertThat(options.getProperties()).as("URL: " + url).containsExactlyInAnyOrderEntriesOf(parameters);
}
Also used : SqlConnectOptions(io.vertx.sqlclient.SqlConnectOptions) DefaultSqlClientPoolConfiguration(org.hibernate.reactive.pool.impl.DefaultSqlClientPoolConfiguration) URI(java.net.URI)

Example 10 with SqlConnectOptions

use of io.vertx.sqlclient.SqlConnectOptions in project hibernate-reactive by hibernate.

the class DefaultPortTest method testDefaultPortIsSet.

@Test
public void testDefaultPortIsSet() throws URISyntaxException {
    DefaultSqlClientPoolConfiguration configuration = new DefaultSqlClientPoolConfiguration();
    configuration.configure(requiredProperties());
    SqlConnectOptions sqlConnectOptions = configuration.connectOptions(new URI(scheme() + "://localhost/database"));
    Assertions.assertThat(sqlConnectOptions.getPort()).as("Default port not defined for " + dbType()).isEqualTo(dbType().getDefaultPort());
}
Also used : SqlConnectOptions(io.vertx.sqlclient.SqlConnectOptions) DefaultSqlClientPoolConfiguration(org.hibernate.reactive.pool.impl.DefaultSqlClientPoolConfiguration) URI(java.net.URI) Test(org.junit.Test)

Aggregations

SqlConnectOptions (io.vertx.sqlclient.SqlConnectOptions)20 CloseFuture (io.vertx.core.impl.CloseFuture)11 ContextInternal (io.vertx.core.impl.ContextInternal)11 VertxInternal (io.vertx.core.impl.VertxInternal)11 Vertx (io.vertx.core.Vertx)10 JsonObject (io.vertx.core.json.JsonObject)10 ClientMetrics (io.vertx.core.spi.metrics.ClientMetrics)10 VertxMetrics (io.vertx.core.spi.metrics.VertxMetrics)10 PoolOptions (io.vertx.sqlclient.PoolOptions)10 Connection (io.vertx.sqlclient.impl.Connection)10 PoolImpl (io.vertx.sqlclient.impl.PoolImpl)10 SqlConnectionInternal (io.vertx.sqlclient.impl.SqlConnectionInternal)10 QueryTracer (io.vertx.sqlclient.impl.tracing.QueryTracer)10 ConnectionFactory (io.vertx.sqlclient.spi.ConnectionFactory)10 Driver (io.vertx.sqlclient.spi.Driver)10 List (java.util.List)10 Collectors (java.util.stream.Collectors)10 DB2ConnectOptions (io.vertx.db2client.DB2ConnectOptions)3 MSSQLConnectOptions (io.vertx.mssqlclient.MSSQLConnectOptions)3 MySQLConnectOptions (io.vertx.mysqlclient.MySQLConnectOptions)3