简体   繁体   中英

Is jdbctemplate.batchupdate multithreaded or concurrent?

I have developed an application in spring boot latest version with Informix database. has some tasks that I want to execute them in parallel. I have the following question and problems.

Does jdbcTemplate.batchupdate() parallelize queries via threads, run them concurrently via asynchronous programming or just execute them one by one and sequentially?

private  String query1, query2, query3;

public void executeQuery(JdbcTemplate jdbctemplate) {
      jdbctemplate.batchupdate(query1, query2, query3)
}

I do excut them in thread but I see no difference in performance. Any idea why?

private void executeInThread(){
ExecutorService sommutExecutorService = Executors.newCachedThreadPool();
        final CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> jdbcTemplate.update(query1), sommutExecutorService);
        final CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> jdbcTemplate.update(query2), sommutExecutorService);
        final CompletableFuture<Integer> future3 = CompletableFuture.supplyAsync(() -> jdbcTemplate.update(query3), sommutExecutorService);

        try {
            CompletableFuture.allOf(future1, future2, future3).thenRun(() -> execute()).get();
        } catch (InterruptedException | ExecutionException e) {
            log(e.getMessage());
        }finally {
            sommutExecutorService.shutdown();
        }
}

Does jdbcTemplate.batchupdate() parallelize queries via threads?

No. It uses JDBC batch updates to submit multiple SQL statements as a batch.

The performance benefit comes from reducing the communication overheads, not from (client-side) parallelism.

If you execute a sequence of single SQL update statements N times, the client-side steps are something like this:

  1. JDBC execute call
  2. Send one SQL statement to server
  3. Wait while the database processes the SQL
  4. Receive response from server containing one count
  5. JDBC execute call returns
  6. Go to step 1... until you have done this N times.

Bottlenecks here are, sending the SQL, waiting for the database to process the request and receiving the response, and doing all of these N times.

If you execute multiple SQL update statements as a batch

  1. JDBC executeBatch call
  2. Send N SQL statements to server as one request
  3. Wait while the database processes all N SQL statements
  4. Receive response from server containing all N counts
  5. JDBC executeBatch call returns

There are still bottlenecks. However:

  • It is faster to send a 1 large message than N small messages containing the same SQL statements or the counts. This is because:

    • the network packets will contain more useful information,
    • the granularity of acknowledgement is coarser, and therefore
    • network round trip delays are squashed.
  • The database can potentially process the multiple SQL statements in parallel.

  • Since the database receives a large number of statements in the batch, it can potentially schedule them more efficient.


By contrast, if you were to run a number of client-side threads each with their own JDBC connection, and each sending single SQL statements.

  • You don't get the network efficiency... because each JDBC connection will use a separate TCP/IP connection

  • The database will be able to process SQL is parallel

  • The database won't be able to schedule the statements as well because it won't be able to see "what is coming next" on any connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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