2012-02-13 59 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