Skip to content

Commit b914880

Browse files
author
chengyitian
committed
AJ-740、AJ-741: fix issue about try reconnect nums for enableHighAvailability when init connect;
1 parent 5c9db68 commit b914880

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

src/com/xxdb/DBConnection.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ private boolean connect()throws IOException{
191191
socket_.connect(new InetSocketAddress(hostName_,port_), 3000);
192192
}
193193
} catch (ConnectException ex) {
194-
log.error("com.xxdb.DBConnection.DBConnectionImpl.connect() has exception. Current node hostName: " + this.hostName_ + ", port: " + this.port_);
195194
log.error("Connect to " + this.hostName_ + ":" + this.port_ + " failed.");
196195
throw ex;
197196
}
@@ -771,19 +770,45 @@ public boolean connect(String hostName, int port, String userId, String password
771770

772771
Node connectedNode = new Node();
773772
BasicTable bt = null;
773+
774774
while (!closed_) {
775+
int totalConnectAttemptNums = this.tryReconnectNums * nodes_.size();
776+
int attempt = 0;
775777
while (!conn_.isConnected() && !closed_) {
776-
for (Node one : nodes_) {
777-
if (connectNode(one)) {
778-
connectedNode = one;
779-
break;
778+
if (this.tryReconnectNums > 0) {
779+
// finite try to connect.
780+
for (Node one : nodes_) {
781+
attempt ++;
782+
// System.out.println("Current init connect node: " + one.hostName + ":" + one.port);
783+
if (connectNode(one)) {
784+
connectedNode = one;
785+
break;
786+
}
787+
788+
try {
789+
Thread.sleep(100);
790+
} catch (Exception e){
791+
e.printStackTrace();
792+
return false;
793+
}
780794
}
781795

782-
try {
783-
Thread.sleep(100);
784-
} catch (Exception e){
785-
e.printStackTrace();
796+
if (attempt == totalConnectAttemptNums)
786797
return false;
798+
} else {
799+
// infinite try to connect.
800+
for (Node one : nodes_) {
801+
if (connectNode(one)) {
802+
connectedNode = one;
803+
break;
804+
}
805+
806+
try {
807+
Thread.sleep(100);
808+
} catch (Exception e){
809+
e.printStackTrace();
810+
return false;
811+
}
787812
}
788813
}
789814
}

0 commit comments

Comments
 (0)