简体   繁体   中英

JDBC query multiple databases

What I want to do is query N databases and store the results in a file specific to each database via JDBC. I was thinking of doing the queries in parallel and was thinking via thread pool, but is that scalable? Is there a better approach(actor-model)?

Thanks.

Yes, it is scalable. There are always better approaches, but you need to be sure that the simplest one suits your needs. If it doesn't then seek for better approaches.

An alternative approach doesn't have to be complicated at all.

// initialize an executor service
ExecutorService executor = Executors.newCachedThreadPool();

// externalize the access to every database in a method declared in a class
// that implements Runnable
class Oracle implements Runnable {
  @Override
  public void run() {
    // load the driver
    // prepare the statement
    // get the connection
    // execute the statement and get the results
    // save the results to a file
  }
}

// execute every db access withing the executor
executor.execute(new Oracle());
executor.execute(new SqlServer());
executor.execute(new MySql());

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