5
5
import java .net .InetAddress ;
6
6
import java .net .InetSocketAddress ;
7
7
import java .net .Socket ;
8
+ import java .rmi .RemoteException ;
8
9
import java .security .PublicKey ;
9
10
import java .security .cert .CertificateException ;
10
11
import java .security .cert .X509Certificate ;
@@ -55,6 +56,7 @@ public class DBConnection {
55
56
private long runSeqNo_ = 0 ;
56
57
private int [] serverVersion_ ;
57
58
private boolean isReverseStreaming_ = false ;
59
+ private int tryReconnectNums = -1 ;
58
60
59
61
private static final Logger log = LoggerFactory .getLogger (DBConnection .class );
60
62
@@ -723,6 +725,14 @@ public boolean connect(String hostName, int port, String userId, String password
723
725
return connect (hostName , port , userId , password , initialScript , enableHighAvailability , highAvailabilitySites , reconnect , false );
724
726
}
725
727
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
+
726
736
public boolean connect (String hostName , int port , String userId , String password , String initialScript , boolean enableHighAvailability , String [] highAvailabilitySites , boolean reconnect , boolean enableLoadBalance ) throws IOException {
727
737
mutex_ .lock ();
728
738
try {
@@ -882,7 +892,10 @@ private void initConnection() throws IOException{
882
892
}
883
893
884
894
public void switchDataNode (Node node ) throws IOException {
895
+ int attempt = 0 ;
885
896
do {
897
+ attempt ++;
898
+ System .out .println ("第 " + attempt + " 次尝试!" );
886
899
if (node .hostName != null && node .hostName .length () > 0 ){
887
900
if (connectNode (node )){
888
901
log .info ("Switch to node: " + node .hostName + ":" + node .port + " successfully." );
@@ -905,7 +918,11 @@ public void switchDataNode(Node node) throws IOException{
905
918
e .printStackTrace ();
906
919
return ;
907
920
}
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
+
909
926
if (initialScript_ !=null && initialScript_ .length () > 0 ){
910
927
run (initialScript_ );
911
928
}
0 commit comments