并行处理多个数据库查询,针对单个客户端请求 [英] Multiple database queries in parallel, for a single client request

查看:231
本文介绍了并行处理多个数据库查询,针对单个客户端请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了完成来自用户的某些请求,在我的应用程序中,我从一个方法发出多个数据库查询,但是当前它们是按顺序执行的.因此,应用程序被阻止,直到它收到上一个查询的响应/数据为止,然后继续下一个查询.这不是我很喜欢的东西.我想发出并行查询.

To complete certain requests from the user, in my application, I am issuing multiple DB queries from a single method, but they are currently being executed sequentially & thus the application is blocked until the time it has received the response/data for the previous query, then proceeding to next query. This is not something I like much. I would like to issue parallel queries.

在发出查询后,我还要做其他工作(而不是在之前的查询响应之前被阻止)&在获得每个查询的响应后,我想执行一个特定于每个查询数据的代码块.这样做的方法是什么?

Also after issuing queries I would like to do some other work, (instead of being blocked till previous queries response) & on getting the response for each query I would like to execute a code block specific to each query's data. What is the way to do this ?

我的数据库API确实提供了连接池.

My DB API does provide connection pooling.

我对Java多线程有点了解.

I'm just a little bit familiar with Java multithreading.

Using:-
------
Java 1.6
Cassandra 1.1 Database with Hector

推荐答案

我有类似的任务/问题.为了获得完整的构建结果,我需要发送一些针对不同服务的请求(很少在REST上,对于Thrift的请求很少),为了减少延迟,我需要并行发送.我的想法是使用java.util.concurrent.Future创建简单的聚合管理器,该管理器将一起创建许多请求,并将等待最后一次检索到的响应并返回所有需要的数据.在更高级的解决方案中,该管理器可以在其他查​​询期间生成/合并最终结果,但是此解决方案可能不是线程安全的.

I have the similar task/issue. For complete build result I need to send few requests for few different service (few on REST, few on Thrift), for decrease latency I need to sent it in parallel. My idea is using java.util.concurrent.Future, make simple aggregation manager , which create many requests together and will be wait last retrieved response and return all needed data. In more advanced solution this manager can make/combine final result during other queries, but this solution can be not thread safe.

这篇关于并行处理多个数据库查询,针对单个客户端请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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