简体   繁体   English

Vertx JDBC如何在引擎盖下工作

[英]Vertx JDBC how it works under the hood

I have been using Vertx for 3 month, but now I wonder how non blocking Vertx JDBC works,for example 我一直在使用Vertx 3个月,但现在我想知道非阻塞Vertx JDBC是如何工作的

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));
            }

        }
    }

Here we provide a callback that will be executed when our select statement will return a result from DB, but how it works. 这里我们提供一个回调函数,当我们的select语句从DB返回结果时将执行它,但它是如何工作的。

I didn't find an answer in docs https://vertx.io/docs/vertx-jdbc-client/java/ 我没有在docs中找到答案https://vertx.io/docs/vertx-jdbc-client/java/

In my opinion: 在我看来:

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. Vertx使用其中一个工作线程来执行select语句而不阻塞事件循环线程。但在这种情况下,每个sql查询都需要一个单独的线程来执行。 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 但是如果Vertx不使用任何单独的线程来执行查询,在这种情况下事件循环如何知道结果来自DB,使用线程非常简单,事件循环可以检查jdbc查询使用的线程的当前状态,以及如果状态已准备就绪,则意味着Event循环应该执行回调

Am i correct? 我对么?

In general, you're correct. 一般来说,你是对的。
You can see under the hood yourself: 你可以自己看一下:

Method queryWithParams() calls execute() : 方法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: execute()看起来像这样:

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

You may wonder where does ctx comes from. 你可能想知道ctx来自哪里。 It's in JDBCClientImpl : 它在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 并且您的查询由普通的旧ExecutorService

how nonblocking Vertx JDBC works? 如何非阻塞Vertx JDBC工作? To Know this answer you need to go depth of the vertx doc and theory of non blocking. 要知道这个答案,你需要深入研究vertx doc和非阻塞理论。

Let's talk with respect to your code. 我们来谈谈您的代码。

connection.close(); connection.close()时;

This line of code is not required. 这行代码不是必需的。 Vertx will take care of it as per their documentation. Vertx将按照他们的文档处理它。

With vertx when we execute query using JDBC,We can do following two things: 使用JDBC执行查询时使用vertx,我们可以执行以下两项操作:

  1. Open connection, Execute multiple queries sequentially and the finally close the connection, which is blocking code 打开连接,按顺序执行多个查询,最后关闭连接,这是阻塞代码
  2. Open connection, execute single query and then close the connection,which is headache way of coding if we didn't take care closing connection seriously. 打开连接,执行单个查询然后关闭连接,如果我们没有认真关注连接,这是令人头疼的编码方式。

Now let's analyze the how nonblocking Vertx JDBC works. 现在让我们分析一下非阻塞Vertx JDBC的工作原理。 If we look more closure to point 2,ie Open connection, execute single query and then close the connection . 如果我们看到更多关闭到第2点,即打开连接,执行单个查询然后关闭连接 If we do it in parallel and take care of closing connection seriously which already take care by vertx ,then we have achieved the not exactly but approx non-blocking query execution 如果我们并行完成并且负责关闭已经由vertx处理的连接,那么我们已经实现了不完全但非阻塞的查询执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM