[英]Opening connections to the two mysql databases at the same time
I have a java JDBC program,which opens connection to mysql database and do some operations. 我有一个Java JDBC程序,该程序打开与mysql数据库的连接并进行一些操作。
I want to connect to two databases, one in local machine and another in remote machine. 我想连接到两个数据库,一个在本地计算机上,另一个在远程计算机上。
I am able to connect one at a time, but I want to open both the connections at the same time, at one shot. 我可以一次连接一个,但是我想一次打开两个连接。
This is my java program: 这是我的java程序:
public class DatabaseOperations {
Connection con = null;
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String password = "root";
//tring url = "jdbc:mysql://192.168.2.26:3306/time_entries_test";
String dbName = "time_entries_test";
String connect1 = "jdbc:mysql://192.168.2.26:3306/" + dbName + "?user=" + user + "&password=" + password + "&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
String connect2 = "jdbc:mysql://localhost:3306/" + dbName + "?user=" + user + "&password=" + password + "&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
public Connection createConnection() {
try {
Class.forName(driver);
//con = DriverManager.getConnection(connect2);
con = DriverManager.getConnection(connect2);
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return con;
}
}
Here connect1 and connect2 are two url strings to which I want to open connections. 这里的connect1和connect2是我要打开连接的两个URL字符串。
You can't have single con
reference to two database connections. 你不能有一个
con
参考两个数据库连接。
You definitely need to maintain two different references. 您肯定需要维护两个不同的引用。
Connection con1 = DriverManger.getConnection( connect1 );
Connection con2 = DriverManger.getConnection( connect2 );
No alternative. 别无选择。
Update 1 : 更新1 :
how do i return both con1 and con2,so that i can use both there in my method to do operations??
我如何同时返回con1和con2,以便可以在我的方法中同时使用它们进行操作?
Solution 1 : Create multiple methods that returns local and remote connections. 解决方案1 :创建返回本地和远程连接的多个方法。
public Connection getLocalConnection() {
...
Connection localCon = DriverManger.getConnection( connect1 );
...
return localCon;
}
public Connection getRemoteConnection() {
...
Connection remoteCon = DriverManger.getConnection( connect2 );
...
return remoteCon;
}
Solution 2 : If you want to generate both connection on every call and return them, you better use a list object to return.. 解决方案2 :如果要在每次调用时都生成两个连接并返回它们,则最好使用list对象返回。
public List<Connection> createConnection() {
...
Connection localCon = DriverManger.getConnection( connect1 );
Connection remoteCon = DriverManger.getConnection( connect2 );
...
List<Connection> connectionsList = new ArrayList<Connection>( 2 );
connectionsList.add( localCon );
connectionsList.add( remoteCon );
...
return connectionsList;
}
I prefer to use Solution 1
, because I am not sure every time you will be requiring both connections. 我更喜欢使用
Solution 1
,因为我不确定每次您都需要两个连接。 At times you may be checking some data in only one database. 有时您可能只在一个数据库中检查某些数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.