Java jdbctemplate.batchupdate是多线程的还是并发的?
我已经用Informix数据库在spring boot最新版本中开发了一个应用程序。有一些我想并行执行的任务。我有以下问题Java jdbctemplate.batchupdate是多线程的还是并发的?,java,multithreading,spring-boot,jdbctemplate,Java,Multithreading,Spring Boot,Jdbctemplate,我已经用Informix数据库在spring boot最新版本中开发了一个应用程序。有一些我想并行执行的任务。我有以下问题 jdbcTemplate.batchupdate()是通过线程并行化查询,还是通过异步编程并发运行查询,还是只是逐个顺序执行查询 private String query1, query2, query3; public void executeQuery(JdbcTemplate jdbctemplate) { jdbctemplate.batchupda
jdbcTemplate.batchupdate()
是通过线程并行化查询,还是通过异步编程并发运行查询,还是只是逐个顺序执行查询
private String query1, query2, query3;
public void executeQuery(JdbcTemplate jdbctemplate) {
jdbctemplate.batchupdate(query1, query2, query3)
}
我确实在线程中执行它们,但我看不到性能上的差异。
知道为什么吗?
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();
}
}
private void executeInThread(){
ExecutorService sommutExecutorService=Executors.newCachedThreadPool();
final CompletableFuture-future1=CompletableFuture.supplyAsync(()->jdbcTemplate.update(query1),sommutExecutorService);
final CompletableFuture-future2=CompletableFuture.supplyAsync(()->jdbcTemplate.update(query2),sommutExecutorService);
final CompletableFuture-future3=CompletableFuture.supplyAsync(()->jdbcTemplate.update(query3),sommutExecutorService);
试一试{
CompletableFuture.allOf(future1、future2、future3)。然后运行(()->execute()).get();
}捕获(中断异常|执行异常e){
日志(如getMessage());
}最后{
sommutExecutorService.shutdown();
}
}
jdbcTemplate.batchupdate()是否通过线程并行化查询
不可以。它使用JDBC批量更新将多个SQL语句作为一个批提交
性能优势来自于减少通信开销,而不是(客户端)并行性
如果执行单个SQL update语句序列N次,客户端步骤如下:
- 发送1条大消息比发送N条包含相同SQL语句或计数的小消息要快。这是因为:
- 网络数据包将包含更多有用的信息
- 确认的粒度较粗,因此
- 网络往返延迟被压缩
- 数据库可能并行处理多个SQL语句
- 由于数据库在批处理中接收大量语句,因此它可以潜在地更高效地调度它们
相反,如果要运行多个客户端线程,每个线程都有自己的JDBC连接,并且每个线程都发送单个SQL语句
- 您无法获得网络效率。。。因为每个JDBC连接将使用单独的TCP/IP连接
- 数据库将能够并行处理SQL
- 数据库也无法调度语句,因为它无法在任何连接上看到“下一步是什么”
- 发送1条大消息比发送N条包含相同SQL语句或计数的小消息要快。这是因为:
- 网络数据包将包含更多有用的信息
- 确认的粒度较粗,因此
- 网络往返延迟被压缩
- 数据库可能并行处理多个SQL语句
- 由于数据库在批处理中接收大量语句,因此它可以潜在地更高效地调度它们
相反,如果要运行多个客户端线程,每个线程都有自己的JDBC连接,并且每个线程都发送单个SQL语句
- 您无法获得网络效率。。。因为每个JDBC连接将使用单独的TCP/IP连接
- 数据库将能够并行处理SQL
- 数据库也无法调度语句,因为它无法在任何连接上看到“下一步是什么”