简体   繁体   English

建立多个jdbc数据库连接

[英]making multiple jdbc database connections

Please take a look at the following code at the following link: 请通过以下链接查看以下代码:

 Connection connMain = DriverManager.getConnection("jdbc:mysql://XX.XX.X.XXX:3306/test","myusername","mypassword");

Connection connRemote = DriverManager.getConnection("jdbc:mysql://" + CurrRemoteIPAddress + ":3306/test","myusername1","mypassword1"); 

http://ideone.com/28GaFh http://ideone.com/28GaFh

Right now, as shown in the code, I am extracting some information from one database (located at IP address XX.XX.X.XXX) and inserting it at another database ( located at IP address defined by CurrRemoteIPAddress which is currently set to AA.YY.Z.PPP on line #30 in my code) 现在,如代码所示,我正在从一个数据库(位于IP地址XX.XX.X.XXX)中提取一些信息,并将其插入另一个数据库(位于CurrRemoteIPAddress定义的IP地址中,该数据库当前设置为AA)我的代码中第30行的.YY.Z.PPP)

The above stuff is working perfectly fine. 上面的东西工作得很好。

Now, I have 6 more databases besides the one located at AA.YY.Z.PPP(let's call it as db1 for discussing purpose, db2,db3,...,db6) where I would like to insert the same extracted information from XX.XX.X.XXX. 现在,除了位于AA.YY.Z.PPP的数据库外,我还有6个数据库(为了讨论目的,我们将其称为db1,db2,db3,...,db6),我想从中插入相同的提取信息XX.XX.X.XXX。

I have to keep following points in my mind while doing this: a) If a connection to one database get's lost, I should move onto next one. 在执行此操作时,我必须牢记以下几点:a)如果丢失了与一个数据库的连接,则应转到下一个数据库。 In an ideal case, I should be able to insert the information into all the six databases(db1 to db6). 在理想情况下,我应该能够将信息插入所有六个数据库(db1至db6)。

I am wondering, should I define new connection string for new each of the six databases? 我想知道,是否应该为六个数据库中的每个数据库定义新的连接字符串?

Can I define it in same try catch? 我可以在同一try catch中定义它吗?

Please advise 请指教

I would create a collection of connections (for db1 to db6) and then iterate over this, invoking the same insertion logic. 我将创建一个连接集合(从db1到db6),然后对此进行迭代,并调用相同的插入逻辑。

You could catch any exception which is the result of failing to connect to a database, log it and simply move onto the next one. 您可能会捕获到任何由于连接数据库失败而导致的异常,将其记录下来并仅移至下一个。

// step 1 create connection to database db1
// step 2 if successful, add to collection
// step 3 repeat steps 1 and 2 for databases db2 to db6
// step 4 retrieve data from source database
// step 5 for each connection in collection, insert into database
// step 6 close each connection in collection

Although there may be better solutions, I would work with an array of Connection objects. 尽管可能有更好的解决方案,但我将使用一系列Connection对象。 That way you could iterate on them until you get what you need: 这样,您可以迭代它们,直到获得所需的内容:

Connection connMain = DriverManager.getConnection(
    "jdbc:mysql://XX.XX.X.XXX:3306/test","myusername","mypassword");
Connection[] connections = new Connection[10]; // Assuming you need ten connections
connections[0] = DriverManager.getConnection(
    "jdbc:mysql://" + CurrRemoteIPAddress + ":3306/db1","myusername1","mypassword1"); 
connections[1] = DriverManager.getConnection(
    "jdbc:mysql://" + CurrRemoteIPAddress + ":3306/db2","myusername1","mypassword1"); 
// Add as many as you need

About your comment: How to catch an exception in a connection? 关于您的评论:如何在连接中捕获异常?

At some point you will have to retrieve data from your databases. 在某些时候,您将不得不从数据库中检索数据。 So that is the place where you catch the exception. 这就是您捕获异常的地方。 A dummy example: 一个虚拟的例子:

Statement stmt;
ResultSet rs;
boolean done = false;
int i = 0;
while(!done) {
    try {
        stmt = connections[i].createStatement();
        rs = stmt.executeQuery("select * from aTable");
        rs.beforeFirst();
        while(rs.next()) {
            // Do whatever you need with the result set
        }
        done = true;
    } catch(SQLException e) {
        // Catch the exception and try with the next connection
        i++;
    }
}

This is a general idea. 这是一个总的想法。 You'll need to write your own solution, but this is a possible approach. 您需要编写自己的解决方案,但这是一种可能的方法。

I am wondering, should I define new connection string for new each of the six databases? 我想知道,是否应该为六个数据库中的每个数据库定义新的连接字符串?

Yes - you'll want to have all the connections open at the same time. 是的-您需要同时打开所有连接。

Can I define it in same try catch? 我可以在同一try catch中定义它吗?

No. Each connection should be created in it's own try/catch so if one fails the other will still work. 否。每个连接都应在自己的try / catch中创建,因此,如果一个连接失败,另一个连接仍然可以工作。

Please advise 请指教

Okay, assuming you want this to run somewhat fast... 好吧,假设您希望它运行得更快...

  • Create a class just for holding the data you want to copy. 创建一个仅用于保存要复制的数据的类。 This is your DTO. 这是您的DTO。
  • Create 6 queues, one for each destination DB 创建6个队列,每个目标数据库一个
  • Start 6 threads, each one should connect to the a different target DB. 启动6个线程,每个线程应连接到不同的目标DB。 Have it read from it's queue and execute the insert statement. 让它从队列中读取并执行insert语句。
  • Have the main thread select from the source database, create and populate a DTO, and put it on each queue. 让主线程从源数据库中选择,创建并填充一个DTO,并将其放在每个队列中。 Loop until you're done 循环播放直到完成
  • Wait until all the other threads are finished. 等待所有其他线程完成。

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

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