2012-02-13 67 views
0

我想要做的是查询N个数据库,并通过JDBC将结果存储在特定于每个数据库的文件中。我正在考虑并行执行查询,并且正在通过线程池进行思考,但这是可扩展的吗?有没有更好的方法(演员模型)?JDBC查询多个数据库

谢谢。

回答

1

是的,它是可扩展的。总是有更好的方法,但你需要确保最简单的方法适合你的需求。如果没有,那就寻求更好的方法。

另一种方法根本不需要很复杂。

// 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()); 
+0

我已经使用ExecutorService和ExecutorCompletionService实现了一个小概念证明,它似乎工作正常。从你的角度来看,还有哪些其他方法? – Radu 2012-02-14 07:31:24

+0

发现这个项目很有趣,首先看http://code.google.com/p/guzz/ – Radu 2012-02-15 13:36:55