Skip to content

Commit 394f48b

Browse files
author
chengyitian
committed
AJ-513、AJ-716: add 'tryReconnectNums' params for DBConnection connect method;
1 parent a2b8c5b commit 394f48b

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/com/xxdb/DBConnection.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.net.InetAddress;
66
import java.net.InetSocketAddress;
77
import java.net.Socket;
8+
import java.rmi.RemoteException;
89
import java.security.PublicKey;
910
import java.security.cert.CertificateException;
1011
import java.security.cert.X509Certificate;
@@ -55,6 +56,7 @@ public class DBConnection {
5556
private long runSeqNo_ = 0;
5657
private int[] serverVersion_;
5758
private boolean isReverseStreaming_ = false;
59+
private int tryReconnectNums = -1;
5860

5961
private static final Logger log = LoggerFactory.getLogger(DBConnection.class);
6062

@@ -723,6 +725,14 @@ public boolean connect(String hostName, int port, String userId, String password
723725
return connect(hostName, port, userId, password, initialScript, enableHighAvailability, highAvailabilitySites, reconnect, false);
724726
}
725727

728+
public boolean connect(String hostName, int port, String userId, String password, String initialScript, boolean enableHighAvailability, String[] highAvailabilitySites, boolean reconnect, boolean enableLoadBalance, int tryReconnectNums) throws IOException {
729+
if (tryReconnectNums < 0)
730+
throw new RuntimeException("The param 'tryReconnectNums' cannot less than 0.");
731+
this.tryReconnectNums = tryReconnectNums;
732+
733+
return connect(hostName, port, userId, password, initialScript, enableHighAvailability, highAvailabilitySites, reconnect, enableLoadBalance);
734+
}
735+
726736
public boolean connect(String hostName, int port, String userId, String password, String initialScript, boolean enableHighAvailability, String[] highAvailabilitySites, boolean reconnect, boolean enableLoadBalance) throws IOException {
727737
mutex_.lock();
728738
try {
@@ -882,7 +892,10 @@ private void initConnection() throws IOException{
882892
}
883893

884894
public void switchDataNode(Node node) throws IOException{
895+
int attempt = 0;
885896
do {
897+
attempt ++;
898+
System.out.println("第 " + attempt + " 次尝试!");
886899
if (node.hostName != null && node.hostName.length() > 0){
887900
if (connectNode(node)){
888901
log.info("Switch to node: " + node.hostName + ":" + node.port + " successfully.");
@@ -905,7 +918,11 @@ public void switchDataNode(Node node) throws IOException{
905918
e.printStackTrace();
906919
return;
907920
}
908-
}while (!closed_);
921+
} while (!closed_ && (tryReconnectNums == -1 || attempt < tryReconnectNums));
922+
923+
if (!closed_)
924+
throw new RuntimeException("Connect to " + node.hostName + ":" + node.port + " failed after " + attempt + " reconnect attemps.");
925+
909926
if (initialScript_!=null && initialScript_.length() > 0){
910927
run(initialScript_);
911928
}

0 commit comments

Comments
 (0)