@@ -50,6 +50,8 @@ public class DBConnection {
50
50
private List <Node > nodes_ = new ArrayList <>();
51
51
private Random nodeRandom_ = new Random ();
52
52
private int connTimeout_ = 0 ;
53
+ private int connectTimeout_ = 0 ;
54
+ private int readTimeout_ = 0 ;
53
55
private boolean closed_ = false ;
54
56
private boolean loadBalance_ = false ;
55
57
private String runClientId_ = null ;
@@ -147,6 +149,8 @@ private class DBConnectionImpl{
147
149
private boolean compress_ = false ;
148
150
private boolean ifUrgent_ = false ;
149
151
private int connTimeout_ = 0 ;
152
+ private int connectTimeout_ = 0 ;
153
+ private int readTimeout_ = 0 ;
150
154
private ExtendedDataInput in_ ;
151
155
private ExtendedDataOutput out_ ;
152
156
private boolean remoteLittleEndian_ ;
@@ -168,35 +172,49 @@ private DBConnectionImpl(boolean asynTask, boolean sslEnable, boolean compress,
168
172
this .lock_ = new ReentrantLock ();
169
173
}
170
174
171
- private boolean connect (String hostName , int port , String userId , String password , int connTimeout ) throws IOException {
175
+ private boolean connect (String hostName , int port , String userId , String password , int connTimeout , int connectTimeout , int readTimeout ) throws IOException {
172
176
this .hostName_ = hostName ;
173
177
this .port_ = port ;
174
178
this .userId_ = userId ;
175
179
this .pwd_ = password ;
176
180
this .connTimeout_ = connTimeout ;
181
+ this .connectTimeout_ = connectTimeout ;
182
+ this .readTimeout_ = readTimeout ;
177
183
return connect ();
178
184
}
179
185
180
- private boolean connect ()throws IOException {
186
+ private boolean connect () throws IOException {
181
187
this .isConnected_ = false ;
182
188
183
189
try {
184
- if (sslEnable_ )
190
+ if (sslEnable_ )
185
191
socket_ = getSSLSocketFactory ().createSocket ();
186
192
else
187
193
socket_ = new Socket ();
188
- if (this .connTimeout_ > 0 ){
189
- socket_ .connect (new InetSocketAddress (hostName_ ,port_ ), connTimeout_ );
190
- }else {
191
- socket_ .connect (new InetSocketAddress (hostName_ ,port_ ), 3000 );
192
- }
193
- } catch (ConnectException ex ) {
194
+
195
+ // set 'connectTimeout' param to connect()
196
+ if (this .connTimeout_ > 0 && this .connectTimeout_ == 0 )
197
+ socket_ .connect (new InetSocketAddress (hostName_ , port_ ), connTimeout_ );
198
+ else if (this .connTimeout_ > 0 && this .connectTimeout_ > 0 )
199
+ socket_ .connect (new InetSocketAddress (hostName_ , port_ ), readTimeout_ );
200
+ else if (this .connTimeout_ == 0 && this .connectTimeout_ > 0 )
201
+ socket_ .connect (new InetSocketAddress (hostName_ , port_ ), readTimeout_ );
202
+ else if (this .connTimeout_ == 0 && this .connectTimeout_ == 0 )
203
+ socket_ .connect (new InetSocketAddress (hostName_ , port_ ), 3000 );
204
+ } catch (IOException ex ) {
194
205
log .error ("Connect to " + this .hostName_ + ":" + this .port_ + " failed." );
195
206
throw ex ;
196
207
}
197
- if (this .connTimeout_ > 0 ) {
208
+
209
+ // set 'readTimeout' param to setSoTimeout
210
+ if (this .connTimeout_ > 0 && this .readTimeout_ == 0 )
198
211
socket_ .setSoTimeout (this .connTimeout_ );
199
- }
212
+ else if (this .connTimeout_ > 0 && this .readTimeout_ > 0 )
213
+ socket_ .setSoTimeout (this .readTimeout_ );
214
+ else if (this .connTimeout_ == 0 && this .readTimeout_ > 0 )
215
+ socket_ .setSoTimeout (this .readTimeout_ );
216
+
217
+
200
218
socket_ .setKeepAlive (true );
201
219
socket_ .setTcpNoDelay (true );
202
220
out_ = new LittleEndianDataOutputStream (new BufferedOutputStream (socket_ .getOutputStream ()));
@@ -664,16 +682,34 @@ public boolean connect(String hostName, int port, int timeout) throws IOExceptio
664
682
return connect (hostName , port , "" , "" , null , false , null );
665
683
}
666
684
685
+ public boolean connect (String hostName , int port , int connectTimeout , int readTimeout ) throws IOException {
686
+ this .connectTimeout_ = connectTimeout ;
687
+ this .readTimeout_ = readTimeout ;
688
+ return connect (hostName , port , "" , "" , null , false , null );
689
+ }
690
+
667
691
public boolean connect (String hostName , int port , int timeout , boolean reconnect ) throws IOException {
668
692
this .connTimeout_ = timeout ;
669
693
return connect (hostName , port , "" , "" , null , false , null , reconnect );
670
694
}
671
695
696
+ public boolean connect (String hostName , int port , int connectTimeout , int readTimeout , boolean reconnect ) throws IOException {
697
+ this .connectTimeout_ = connectTimeout ;
698
+ this .readTimeout_ = readTimeout ;
699
+ return connect (hostName , port , "" , "" , null , false , null , reconnect );
700
+ }
701
+
672
702
public boolean connect (String hostName , int port , int timeout , boolean reconnect , int tryReconnectNums ) throws IOException {
673
703
this .connTimeout_ = timeout ;
674
704
return connect (hostName , port , "" , "" , null , false , null , reconnect , tryReconnectNums );
675
705
}
676
706
707
+ public boolean connect (String hostName , int port , int connectTimeout , int readTimeout , boolean reconnect , int tryReconnectNums ) throws IOException {
708
+ this .connectTimeout_ = connectTimeout ;
709
+ this .readTimeout_ = readTimeout ;
710
+ return connect (hostName , port , "" , "" , null , false , null , reconnect , tryReconnectNums );
711
+ }
712
+
677
713
public boolean connect (String hostName , int port , String initialScript ) throws IOException {
678
714
return connect (hostName , port , "" , "" , initialScript , false , null );
679
715
}
@@ -974,12 +1010,12 @@ public void switchDataNode(Node node) throws IOException{
974
1010
}
975
1011
}
976
1012
977
- public boolean connectNode (Node node ) throws IOException {
1013
+ public boolean connectNode (Node node ) throws IOException {
978
1014
log .info ("Connect to " + node .hostName + ":" + node .port + "." );
979
1015
while (!closed_ ){
980
1016
try {
981
- return conn_ .connect (node .hostName , node .port , uid_ , pwd_ , connTimeout_ );
982
- }catch (Exception e ){
1017
+ return conn_ .connect (node .hostName , node .port , uid_ , pwd_ , connTimeout_ , connectTimeout_ , readTimeout_ );
1018
+ } catch (Exception e ) {
983
1019
if (isConnected ()){
984
1020
Node tmpNode = new Node ();
985
1021
tmpNode .hostName = node .hostName ;
@@ -993,11 +1029,12 @@ else if (type == ExceptionType.ET_NODENOTAVAIL)
993
1029
else
994
1030
throw e ;
995
1031
}
996
- }else {
1032
+ } else {
997
1033
log .error (e .getMessage ());
998
1034
return false ;
999
1035
}
1000
1036
}
1037
+
1001
1038
try {
1002
1039
Thread .sleep (100 );
1003
1040
}catch (Exception e ){
@@ -1009,7 +1046,6 @@ else if (type == ExceptionType.ET_NODENOTAVAIL)
1009
1046
}
1010
1047
1011
1048
public ExceptionType parseException (String msg , Node node ){
1012
- log .info ("com.xxdb.DBConnection.parseException msg: " + msg );
1013
1049
if (msg ==null ){
1014
1050
node .hostName = "" ;
1015
1051
node .port = 0 ;
0 commit comments