Vertx JDBC的工作原理 [英] Vertx JDBC how it works under the hood

查看:197
本文介绍了Vertx JDBC的工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Vertx 3个月了,但是现在我想知道非阻塞Vertx JDBC是如何工作的,例如

I have been using Vertx for 3 month, but now I wonder how non blocking Vertx JDBC works,for example

private void selectEndedMatches(){
        this.jdbcClient.getConnection(conn->{
            if(conn.failed()){
                log.error("Can't get Vertx connection",conn.cause());
            } else{
                final SQLConnection connection = conn.result();
                connection.queryWithParams("select matchid from get5_stats_matches where matchid > ? and end_time is not null",new JsonArray().add(this.lastMatchId),this::endedMatches);
                connection.close();
            }
        });
    }


private void endedMatches(final AsyncResult<ResultSet> rs) {
        if(rs.failed()){
            log.error("Can't make select statement from JdbcVerticle",rs.cause());
        } else{
            final List<JsonArray> results = rs.result().getResults();
            final List<Integer> endedMatches = new ArrayList<>(results.size());
            for (final JsonArray result : results) {
                endedMatches.add(result.getInteger(0));
            }

        }
    }

在这里,我们提供了一个回调,当我们的select语句将从数据库返回结果时将执行该回调,但是它是如何工作的.

Here we provide a callback that will be executed when our select statement will return a result from DB, but how it works.

我在文档https://vertx.io/docs/vertx-jdbc-client/java/

我认为:

Vertx使用一个工作线程来执行select语句以不阻塞事件循环线程.但是在这种情况下,每个sql查询都需要一个单独的线程来执行. 但是,如果Vertx不使用任何单独的线程来执行查询,在这种情况下,事件循环如何知道何时结果来自数据库,使用线程就非常简单,事件循环可以检查jdbc查询使用的线程的当前状态,并且如果状态已准备就绪,则意味着事件循环应执行回调

Vertx use one of the worker threads to do select statement to not block Event loop thread.But in this case each sql query need a separate thread to execute. But what if Vertx doesn't use any separate thread to execute query, in this case how event loop know when result came from DB, using threads it's pretty simple, Event loop can check current state of thread that is used by jdbc query, and if state is ready it's mean that Event loop should to execute Call back

我正确吗?

推荐答案

通常来说,您是正确的.
您可以自己在引擎盖下看到:

In general, you're correct.
You can see under the hood yourself:

方法queryWithParams()调用execute():

public SQLConnection queryWithParams(String sql, JsonArray params, Handler<AsyncResult<ResultSet>> resultHandler) {
    new JDBCQuery(vertx, helper, options, ctx, sql, params).execute(conn, statementsQueue, resultHandler);
    return this;
  }

And execute() looks like this:

public void execute(Connection conn, TaskQueue statementsQueue, Handler<AsyncResult<T>> resultHandler) {
    ctx.executeBlocking(future -> handle(conn, future), statementsQueue, resultHandler);
}

您可能想知道ctx的来源.在

You may wonder where does ctx comes from. It's in JDBCClientImpl:

public SQLClient getConnection(Handler<AsyncResult<SQLConnection>> handler) {
    Context ctx = vertx.getOrCreateContext();
    getConnection(ctx, ar -> ctx.runOnContext(v -> handler.handle(ar)));
    return this;
  }

您的查询由普通的

And your query is executed by plain old ExecutorService

这篇关于Vertx JDBC的工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆