简体   繁体   English

使用JDBC Java连接多个MySQL服务器

[英]Connect multiple MySQL server using JDBC Java

I have 10 different MySQL servers with the same schema design. 我有10个具有相同架构设计的不同MySQL服务器。 Now I have to query each server and merge results in java which is same as UNION ALL in SQL. 现在,我必须查询每个服务器并在Java中合并结果,这与SQL中的UNION ALL相同。

All DB servers return the same data format, and I tried sequential call of all server but it takes more time. 所有数据库服务器都返回相同的数据格式,我尝试顺序调用所有服务器,但是这需要更多时间。

How can I speed up the process? 我如何加快这一过程?

Scenario 情境

在此处输入图片说明

This probably your sequential logic: 这可能是您的顺序逻辑:

  1. List<YourObject> arr = new ArrayList<YourObject>();
  2. X = 1. X = 1。
  3. Select * from X. 从X选择*。
  4. add results to arr. 将结果添加到arr。
  5. X++ until no more databases. X ++,直到没有更多数据库为止。

I can think of a solution that might consume less time: 我可以想到一个可能耗时更少的解决方案:

How about this logic: 这个逻辑怎么样:

you can cut the sequential into threads. 您可以将序列切成线程。 each thread will connect to a different server. 每个线程将连接到不同的服务器。 once you have result from a server lock the arr and add the results. 从服务器获得结果后,请锁定arr并添加结果。

  1. Create a Connection thread to each Server. 创建到每个服务器的连接线程。
  2. Run Threads to connect and wait for result. 运行线程以连接并等待结果。
  3. Each incoming result add to a merged array. 每个传入结果都添加到合并数组中。 (don't forget to lock global result array). (不要忘记锁定全局结果数组)。

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

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