io.vertx.ext.sql.SQLClient Java Examples

The following examples show how to use io.vertx.ext.sql.SQLClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JDBCConfig.java    From xyz-hub with Apache License 2.0 6 votes vote down vote up
public static SQLClient getClient() {
  if (client != null) {
    return client;
  }

  synchronized (CONNECTOR_TABLE) {
    if (client == null) {
      String db_url = Service.configuration.STORAGE_DB_URL;
      db_url += (db_url.contains("?") ? "&" : "?") + "ApplicationName=XYZ-Hub";
      JsonObject config = new JsonObject()
          .put("url", db_url)
          .put("user", Service.configuration.STORAGE_DB_USER)
          .put("password", Service.configuration.STORAGE_DB_PASSWORD)
          .put("min_pool_size", 1)
          .put("max_pool_size", 4)
          .put("acquire_retry_attempts", 1);
      client = io.vertx.ext.jdbc.JDBCClient.createShared(Service.vertx, config);
    }
    return client;
  }
}
 
Example #2
Source File: RefCountTest.java    From vertx-jdbc-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testSharedDefault() throws Exception {
  JsonObject config = new JsonObject();
  SQLClient client1 = JDBCClient.createShared(vertx, config);
  assertEquals(1, map.size());
  SQLClient client2 = JDBCClient.createShared(vertx, config);
  assertEquals(1, map.size());
  SQLClient client3 = JDBCClient.createShared(vertx, config);
  assertEquals(1, map.size());

  close(client1, 1)
    .compose(v -> close(client2, 1))
    .compose(v -> close(client3, 0))
    .onComplete(onSuccess(v -> testComplete()));

  await();
}
 
Example #3
Source File: Store.java    From hono with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Create a new instance.
 *
 * @param client The SQL client to use.
 * @param tracer The tracer to use.
 * @param cfg The statement configuration to use.
 */
public Store(final SQLClient client, final Tracer tracer, final StatementConfiguration cfg) {
    super(client, tracer, cfg.getStatement("checkConnection"));
    cfg.dump(log);

    this.client = client;
    this.tracer = tracer;

    this.readStatement = cfg
            .getRequiredStatement("read")
            .validateParameters(
                    "tenant_id",
                    "device_id");

    this.updateStatement = cfg.getRequiredStatement("update")
            .validateParameters(
                    "tenant_id",
                    "device_id",
                    "gateway_id");

    this.dropTenantStatement = cfg.getRequiredStatement("dropTenant")
            .validateParameters("tenant_id");

}
 
Example #4
Source File: RefCountTest.java    From vertx-jdbc-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testNonShared() {
  JsonObject config = new JsonObject();
  SQLClient client1 = JDBCClient.create(vertx, config);
  assertEquals(1, map.size());
  SQLClient client2 = JDBCClient.create(vertx, config);
  assertEquals(2, map.size());
  SQLClient client3 = JDBCClient.create(vertx, config);
  assertEquals(3, map.size());

  close(client1, 2)
    .compose(v -> close(client2, 1))
    .compose(v -> close(client3, 0))
    .onComplete(onSuccess(v -> testComplete()));

  await();
}
 
Example #5
Source File: CloseTest.java    From vertx-jdbc-client with Apache License 2.0 6 votes vote down vote up
@Override
public void start(io.vertx.core.Promise<Void> f) throws Exception {
  SQLClient client = JDBCClient.create(vertx, ds);
  String sql = "SELECT ID, FNAME, LNAME FROM select_table ORDER BY ID";
  client.getConnection(ar1 -> {
    if (ar1.succeeded()) {
      SQLConnection conn = ar1.result();
      conn.query(sql, ar2 -> {
        if (ar2.succeeded()) {
          f.complete();
        } else {
          f.fail(ar2.cause());
        }
      });
    } else {
      f.fail(ar1.cause());
    }
  });
}
 
Example #6
Source File: CloseTest.java    From vertx-jdbc-client with Apache License 2.0 6 votes vote down vote up
@Override
public void start(io.vertx.core.Promise<Void> f) throws Exception {
  SQLClient client = JDBCClient.createShared(vertx, theConfig);
  String sql = "SELECT ID, FNAME, LNAME FROM select_table ORDER BY ID";
  client.getConnection(ar1 -> {
    if (ar1.succeeded()) {
      SQLConnection conn = ar1.result();
      conn.query(sql, ar2 -> {
        if (ar2.succeeded()) {
          f.complete();
        } else {
          f.fail(ar2.cause());
        }
      });
    } else {
      f.fail(ar1.cause());
    }
  });
}
 
Example #7
Source File: CloseTest.java    From vertx-jdbc-client with Apache License 2.0 6 votes vote down vote up
@Override
public void start(io.vertx.core.Promise<Void> f) throws Exception {
  SQLClient client = JDBCClient.create(vertx, theConfig);
  String sql = "SELECT ID, FNAME, LNAME FROM select_table ORDER BY ID";
  client.getConnection(ar1 -> {
    if (ar1.succeeded()) {
      SQLConnection conn = ar1.result();
      conn.query(sql, ar2 -> {
        if (ar2.succeeded()) {
          f.complete();
        } else {
          f.fail(ar2.cause());
        }
      });
    } else {
      f.fail(ar1.cause());
    }
  });
}
 
Example #8
Source File: SQLConnectionTest.java    From vertx-jdbc-client with Apache License 2.0 5 votes vote down vote up
private void checkAutocloseable(SQLClient client) {
  client.getConnection(ar -> {
    try (SQLConnection conn = ar.result()) {

    }
  });
}
 
Example #9
Source File: JDBCEventStore.java    From enode with MIT License 5 votes vote down vote up
private SQLClient batchWithParams(String sql, List<JsonArray> params, Handler<AsyncResult<List<Integer>>> handler) {
    sqlClient.getConnection(getConnection -> {
        if (getConnection.failed()) {
            handler.handle(Future.failedFuture(getConnection.cause()));
            return;
        }
        final SQLConnection conn = getConnection.result();
        conn.setAutoCommit(false, autocommit -> {
            if (autocommit.failed()) {
                handler.handle(Future.failedFuture(autocommit.cause()));
                resetAutoCommitAndCloseConnection(conn);
                return;
            }
            conn.batchWithParams(sql, params, batch -> {
                if (batch.succeeded()) {
                    conn.commit(commit -> {
                        if (commit.succeeded()) {
                            handler.handle(Future.succeededFuture(batch.result()));
                        } else {
                            handler.handle(Future.failedFuture(commit.cause()));
                        }
                        resetAutoCommitAndCloseConnection(conn);
                    });
                } else {
                    conn.rollback(rollback -> {
                        if (rollback.succeeded()) {
                            handler.handle(Future.failedFuture(batch.cause()));
                        } else {
                            handler.handle(Future.failedFuture(rollback.cause()));
                        }
                        resetAutoCommitAndCloseConnection(conn);
                    });
                }
            });
        });
    });
    return sqlClient;
}
 
Example #10
Source File: RefCountTest.java    From vertx-jdbc-client with Apache License 2.0 5 votes vote down vote up
private Future<Void> close(SQLClient client, int expectedMapSize) {
  Promise<Void> promise = Promise.promise();
  client.close(promise);
  return promise.future().compose(v -> {
    assertEquals(expectedMapSize, map.size());
    return Future.succeededFuture(v);
  });
}
 
Example #11
Source File: RefCountTest.java    From vertx-jdbc-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testSharedNamed() throws Exception {
  JsonObject config = new JsonObject();
  SQLClient client1 = JDBCClient.createShared(vertx, config, "ds1");
  assertEquals(1, map.size());
  SQLClient client2 = JDBCClient.createShared(vertx, config, "ds1");
  assertEquals(1, map.size());
  SQLClient client3 = JDBCClient.createShared(vertx, config, "ds1");
  assertEquals(1, map.size());

  SQLClient client4 = JDBCClient.createShared(vertx, config, "ds2");
  assertEquals(2, map.size());
  SQLClient client5 = JDBCClient.createShared(vertx, config, "ds2");
  assertEquals(2, map.size());
  SQLClient client6 = JDBCClient.createShared(vertx, config, "ds2");
  assertEquals(2, map.size());

  close(client1, 2)
    .compose(v -> close(client2, 2))
    .compose(v -> close(client3, 1))
    .compose(v -> close(client4, 1))
    .compose(v -> close(client5, 1))
    .compose(v -> close(client6, 0))
    .onComplete(onSuccess(v -> testComplete()));

  await();
}
 
Example #12
Source File: SQLExamples.java    From vertx-jdbc-client with Apache License 2.0 5 votes vote down vote up
public void example16(SQLClient client) {
  client.query("SELECT * FROM USERS", ar -> {
    if (ar.succeeded()) {
      if (ar.succeeded()) {
        ResultSet result = ar.result();
      } else {
        // Failed!
      }
      // NOTE that you don't need to worry about
      // the connection management (e.g.: close)
    }
  });
}
 
Example #13
Source File: AbstractDeviceStore.java    From enmasse with Apache License 2.0 5 votes vote down vote up
public AbstractDeviceStore(final SQLClient client, final Tracer tracer, final StatementConfiguration cfg) {
    super(client, tracer, cfg.getStatement("checkConnection"));

    this.client = client;
    this.tracer = tracer;

    this.readRegistrationStatement = cfg
            .getRequiredStatment("readRegistration")
            .validateParameters(
                    "tenant_id",
                    "device_id");
}
 
Example #14
Source File: TableAdapterStore.java    From enmasse with Apache License 2.0 5 votes vote down vote up
public TableAdapterStore(final SQLClient client, final Tracer tracer, final StatementConfiguration cfg) throws IOException {
    super(client, tracer, cfg);
    cfg.dump(log);

    this.findCredentialsStatement = cfg
            .getRequiredStatment("findCredentials")
            .validateParameters(
                    "tenant_id",
                    "type",
                    "auth_id");

}
 
Example #15
Source File: Store.java    From enmasse with Apache License 2.0 5 votes vote down vote up
public Store(final SQLClient client, final Tracer tracer, final StatementConfiguration cfg) {
    super(client, tracer, cfg.getStatement("checkConnection"));
    cfg.dump(log);

    this.client = client;
    this.tracer = tracer;

    this.readStatement = cfg
            .getRequiredStatment("read")
            .validateParameters(
                    "tenant_id",
                    "device_id");

    this.dropTenantStatement = cfg.getRequiredStatment("dropTenant")
            .validateParameters("tenant_id");

    this.updateLastKnownGatewayStatement = cfg.getRequiredStatment("updateLastKnownGateway")
            .validateParameters(
                    "tenant_id",
                    "device_id",
                    "gateway_id");

    this.updateAdapterInstanceStatement = cfg.getRequiredStatment("updateAdapterInstance")
            .validateParameters(
                    "tenant_id",
                    "device_id",
                    "adapter_instance_id");

}
 
Example #16
Source File: MysqlSideFunction.java    From alchemy with Apache License 2.0 5 votes vote down vote up
private SQLClient createClient(MysqlSideProperties mysqlProperties, Vertx vertx) {
    JsonObject mysqlClientConfig = new JsonObject();
    mysqlClientConfig.put("url", mysqlProperties.getUrl()).put("driver_class", MYSQL_DRIVER)
        .put("max_pool_size",
            mysqlProperties.getMaxPoolSize() == null ? DEFAULT_MAX_DB_CONN_POOL_SIZE
                : mysqlProperties.getMaxPoolSize())
        .put("user", mysqlProperties.getUsername()).put("password", mysqlProperties.getPassword());

    return JDBCClient.createNonShared(vertx, mysqlClientConfig);
}
 
Example #17
Source File: JsonAdapterStore.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Create a new instance.
 *
 * @param client The SQL client ot use.
 * @param tracer The tracer to use.
 * @param hierarchical If the JSON store uses a hierarchical model for a flat one.
 * @param cfg The SQL statement configuration.
 */
public JsonAdapterStore(final SQLClient client, final Tracer tracer, final boolean hierarchical, final StatementConfiguration cfg) {
    super(client, tracer, cfg);
    cfg.dump(log);

    this.hierarchical = hierarchical;

    this.findCredentialsStatement = cfg
            .getRequiredStatement("findCredentials")
            .validateParameters(
                    "tenant_id",
                    "type",
                    "auth_id");

}
 
Example #18
Source File: AbstractDeviceStore.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Create a new instance.
 *
 * @param client The SQL client to use.
 * @param tracer The tracer to use.
 * @param cfg The SQL statement configuration.
 */
public AbstractDeviceStore(final SQLClient client, final Tracer tracer, final StatementConfiguration cfg) {
    super(client, tracer, cfg.getStatement("checkConnection"));

    this.client = client;
    this.tracer = tracer;

    this.readRegistrationStatement = cfg
            .getRequiredStatement("readRegistration")
            .validateParameters(
                    "tenant_id",
                    "device_id");

}
 
Example #19
Source File: JsonManagementStore.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Create a new instance.
 *
 * @param client The SQL client to use.
 * @param tracer The tracer to use.
 * @param hierarchical If the JSON store uses a hierarchical model for a flat one.
 * @param cfg The SQL statement configuration.
 */
public JsonManagementStore(final SQLClient client, final Tracer tracer, final boolean hierarchical, final StatementConfiguration cfg) {
    super(client, tracer, cfg);
    cfg.dump(log);

    this.hierarchical = hierarchical;

    this.readCredentialsStatement = cfg
            .getRequiredStatement("readCredentials")
            .validateParameters(
                    "tenant_id",
                    "device_id");

    this.updateCredentialsStatement = cfg
            .getRequiredStatement("updateCredentials")
            .validateParameters(
                    "tenant_id",
                    "device_id",
                    "next_version",
                    "data");

    this.updateCredentialsVersionedStatement = cfg
            .getRequiredStatement("updateCredentialsVersioned")
            .validateParameters(
                    "tenant_id",
                    "device_id",
                    "next_version",
                    "data",
                    "expected_version");

}
 
Example #20
Source File: TableAdapterStore.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Create a new instance.
 *
 * @param client The SQL client ot use.
 * @param tracer The tracer to use.
 * @param cfg The SQL statement configuration.
 */
public TableAdapterStore(final SQLClient client, final Tracer tracer, final StatementConfiguration cfg) {
    super(client, tracer, cfg);
    cfg.dump(log);

    this.findCredentialsStatement = cfg
            .getRequiredStatement("findCredentials")
            .validateParameters(
                    "tenant_id",
                    "type",
                    "auth_id");

}
 
Example #21
Source File: JdbcProperties.java    From enmasse with Apache License 2.0 5 votes vote down vote up
public static SQLClient dataSource(final Vertx vertx, final JdbcProperties dataSourceProperties) {

        /*
         * In the following lines we explicitly set the "provider_class" and **must not** use
         * the existing constant for that.
         *
         * The reason for this is, that the downstream version changes the value of the constant to
         * use Agroal as the connection pool. As C3P0 is the upstream default for Vert.x and Agroal
         * isn't even mentioned upstream, we explicitly set C3P0 here. Without using the (not so) constant.
         */

        final JsonObject config = new JsonObject()
                // set default explicitly: see comment above
                .put("provider_class", "io.vertx.ext.jdbc.spi.impl.C3P0DataSourceProvider")
                .put("url", dataSourceProperties.getUrl())
                .put("user", dataSourceProperties.getUsername())
                .put("password", dataSourceProperties.getPassword());

        if (dataSourceProperties.getDriverClass() != null) {
            config.put("driver_class", dataSourceProperties.getDriverClass());
        }
        if (dataSourceProperties.getMaximumPoolSize() != null) {
            config.put("max_pool_size", dataSourceProperties.getMaximumPoolSize());
        }

        log.info("Creating new SQL client: {} - table: {}", config, dataSourceProperties.getTableName());

        return JDBCClient.createShared(vertx, config);

    }
 
Example #22
Source File: JDBCPoolMetricsTest.java    From vertx-jdbc-client with Apache License 2.0 4 votes vote down vote up
private SQLClient getClient() {
  if (client == null) {
    client = JDBCClient.createShared(vertx, JDBCClientTestBase.config().put("max_pool_size", 10), dataSourceName);
  }
  return client;
}
 
Example #23
Source File: TableManagementStore.java    From hono with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Create a new instance.
 *
 * @param client The SQL client ot use.
 * @param tracer The tracer to use.
 * @param cfg The SQL statement configuration.
 */
public TableManagementStore(final SQLClient client, final Tracer tracer, final StatementConfiguration cfg) {
    super(client, tracer, cfg);
    cfg.dump(log);

    this.readForUpdateStatement = cfg.getRequiredStatement("readForUpdate")
            .validateParameters(
                    "tenant_id",
                    "device_id");
    this.readForUpdateVersionedStatement = cfg.getRequiredStatement("readForUpdateVersioned")
            .validateParameters(
                    "tenant_id",
                    "device_id",
                    "expected_version");

    this.readCredentialsStatement = cfg
            .getRequiredStatement("readCredentials")
            .validateParameters(
                    "tenant_id",
                    "device_id");

    this.insertCredentialEntryStatement = cfg
            .getRequiredStatement("insertCredentialEntry")
            .validateParameters(
                    "tenant_id",
                    "device_id",
                    "type",
                    "auth_id",
                    "data");

    this.deleteAllCredentialsStatement = cfg
            .getRequiredStatement("deleteAllCredentials")
            .validateParameters(
                    "tenant_id",
                    "device_id");

    this.updateDeviceVersionStatement = cfg
            .getRequiredStatement("updateDeviceVersion")
            .validateParameters(
                    "tenant_id",
                    "device_id",
                    "next_version",
                    "expected_version");

}
 
Example #24
Source File: AbstractDeviceManagementStore.java    From hono with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Create a new instance.
 *
 * @param client The SQL client to use.
 * @param tracer The tracer to use.
 * @param cfg The SQL statement configuration.
 */
public AbstractDeviceManagementStore(final SQLClient client, final Tracer tracer, final StatementConfiguration cfg) {
    super(client, tracer, cfg);

    this.createStatement = cfg
            .getRequiredStatement("create")
            .validateParameters(
                    "tenant_id",
                    "device_id",
                    "version",
                    "data");

    this.updateRegistrationStatement = cfg
            .getRequiredStatement("updateRegistration")
            .validateParameters(
                    "tenant_id",
                    "device_id",
                    "next_version",
                    "data");

    this.updateRegistrationVersionedStatement = cfg
            .getRequiredStatement("updateRegistrationVersioned")
            .validateParameters(
                    "tenant_id",
                    "device_id",
                    "next_version",
                    "data",
                    "expected_version");

    this.deleteStatement = cfg
            .getRequiredStatement("delete")
            .validateParameters(
                    "tenant_id",
                    "device_id");

    this.deleteVersionedStatement = cfg
            .getRequiredStatement("deleteVersioned")
            .validateParameters(
                    "tenant_id",
                    "device_id",
                    "expected_version");

    this.dropTenantStatement = cfg
            .getRequiredStatement("dropTenant")
            .validateParameters(
                    "tenant_id");

}
 
Example #25
Source File: JDBCClientImpl.java    From vertx-jdbc-client with Apache License 2.0 4 votes vote down vote up
@Override
public SQLClient getConnection(Handler<AsyncResult<SQLConnection>> handler) {
  getConnection().onComplete(handler);
  return this;
}
 
Example #26
Source File: HikariCPManager.java    From AlipayWechatPlatform with GNU General Public License v3.0 4 votes vote down vote up
@Override
public SQLClient getSQLClient() {
    return this.client;
}
 
Example #27
Source File: AbstractStore.java    From enmasse with Apache License 2.0 4 votes vote down vote up
public AbstractStore(final SQLClient client, final Tracer tracer, final Optional<Statement> checkSql) {
    this.client = client;
    this.tracer = tracer;
    this.checkSql = checkSql.orElseGet(() -> Statement.statement(DEFAULT_CHECK_SQL)).expand();
}
 
Example #28
Source File: AbstractDeviceManagementStore.java    From enmasse with Apache License 2.0 4 votes vote down vote up
public AbstractDeviceManagementStore(final SQLClient client, final Tracer tracer, final StatementConfiguration cfg) throws IOException {
    super(client, tracer, cfg);

    this.createStatement = cfg
            .getRequiredStatment("create")
            .validateParameters(
                    "tenant_id",
                    "device_id",
                    "version",
                    "data");

    this.updateRegistrationStatement = cfg
            .getRequiredStatment("updateRegistration")
            .validateParameters(
                    "tenant_id",
                    "device_id",
                    "next_version",
                    "data");

    this.updateRegistrationVersionedStatement = cfg
            .getRequiredStatment("updateRegistrationVersioned")
            .validateParameters(
                    "tenant_id",
                    "device_id",
                    "next_version",
                    "data",
                    "expected_version");

    this.deleteStatement = cfg
            .getRequiredStatment("delete")
            .validateParameters(
                    "tenant_id",
                    "device_id");

    this.deleteVersionedStatement = cfg
            .getRequiredStatment("deleteVersioned")
            .validateParameters(
                    "tenant_id",
                    "device_id",
                    "expected_version");

    this.dropTenantStatement = cfg
            .getRequiredStatment("dropTenant")
            .validateParameters(
                    "tenant_id");

}
 
Example #29
Source File: AbstractDeviceAdapterStore.java    From enmasse with Apache License 2.0 4 votes vote down vote up
public AbstractDeviceAdapterStore(final SQLClient client, final Tracer tracer, final StatementConfiguration cfg) throws IOException {
    super(client, tracer, cfg);
}
 
Example #30
Source File: TableManagementStore.java    From enmasse with Apache License 2.0 4 votes vote down vote up
public TableManagementStore(final SQLClient client, final Tracer tracer, final StatementConfiguration cfg) throws IOException {
    super(client, tracer, cfg);
    cfg.dump(log);

    this.readForUpdateStatement = cfg.getRequiredStatment("readForUpdate")
            .validateParameters(
                    "tenant_id",
                    "device_id");
    this.readForUpdateVersionedStatement = cfg.getRequiredStatment("readForUpdateVersioned")
            .validateParameters(
                    "tenant_id",
                    "device_id",
                    "expected_version");

    this.readCredentialsStatement = cfg
            .getRequiredStatment("readCredentials")
            .validateParameters(
                    "tenant_id",
                    "device_id");

    this.insertCredentialEntryStatement = cfg
            .getRequiredStatment("insertCredentialEntry")
            .validateParameters(
                    "tenant_id",
                    "device_id",
                    "type",
                    "auth_id",
                    "data");

    this.deleteAllCredentialsStatement = cfg
            .getRequiredStatment("deleteAllCredentials")
            .validateParameters(
                    "tenant_id",
                    "device_id");

    this.updateDeviceVersionStatement = cfg
            .getRequiredStatment("updateDeviceVersion")
            .validateParameters(
                    "tenant_id",
                    "device_id",
                    "next_version",
                    "expected_version");

}