Skip to content

Commit 399fdf8

Browse files
author
chengyitian
committed
AJ-764、AJ-765: support new param 'connectTimeout'、'readTimeout' for DBConnection connect();
1 parent 8b11391 commit 399fdf8

File tree

1 file changed

+52
-16
lines changed

1 file changed

+52
-16
lines changed

src/com/xxdb/DBConnection.java

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public class DBConnection {
5050
private List<Node> nodes_ = new ArrayList<>();
5151
private Random nodeRandom_ = new Random();
5252
private int connTimeout_ = 0;
53+
private int connectTimeout_ = 0;
54+
private int readTimeout_ = 0;
5355
private boolean closed_ = false;
5456
private boolean loadBalance_ = false;
5557
private String runClientId_ = null;
@@ -147,6 +149,8 @@ private class DBConnectionImpl{
147149
private boolean compress_ = false;
148150
private boolean ifUrgent_ = false;
149151
private int connTimeout_ = 0;
152+
private int connectTimeout_ = 0;
153+
private int readTimeout_ = 0;
150154
private ExtendedDataInput in_;
151155
private ExtendedDataOutput out_;
152156
private boolean remoteLittleEndian_;
@@ -168,35 +172,49 @@ private DBConnectionImpl(boolean asynTask, boolean sslEnable, boolean compress,
168172
this.lock_ = new ReentrantLock();
169173
}
170174

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{
172176
this.hostName_ = hostName;
173177
this.port_ = port;
174178
this.userId_ = userId;
175179
this.pwd_ = password;
176180
this.connTimeout_ = connTimeout;
181+
this.connectTimeout_ = connectTimeout;
182+
this.readTimeout_ = readTimeout;
177183
return connect();
178184
}
179185

180-
private boolean connect()throws IOException{
186+
private boolean connect() throws IOException {
181187
this.isConnected_ = false;
182188

183189
try {
184-
if(sslEnable_)
190+
if (sslEnable_)
185191
socket_ = getSSLSocketFactory().createSocket();
186192
else
187193
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) {
194205
log.error("Connect to " + this.hostName_ + ":" + this.port_ + " failed.");
195206
throw ex;
196207
}
197-
if (this.connTimeout_ > 0) {
208+
209+
// set 'readTimeout' param to setSoTimeout
210+
if (this.connTimeout_ > 0 && this.readTimeout_ == 0)
198211
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+
200218
socket_.setKeepAlive(true);
201219
socket_.setTcpNoDelay(true);
202220
out_ = new LittleEndianDataOutputStream(new BufferedOutputStream(socket_.getOutputStream()));
@@ -664,16 +682,34 @@ public boolean connect(String hostName, int port, int timeout) throws IOExceptio
664682
return connect(hostName, port, "", "", null, false, null);
665683
}
666684

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+
667691
public boolean connect(String hostName, int port, int timeout, boolean reconnect) throws IOException {
668692
this.connTimeout_ = timeout;
669693
return connect(hostName, port, "", "", null, false, null, reconnect);
670694
}
671695

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+
672702
public boolean connect(String hostName, int port, int timeout, boolean reconnect, int tryReconnectNums) throws IOException {
673703
this.connTimeout_ = timeout;
674704
return connect(hostName, port, "", "", null, false, null, reconnect, tryReconnectNums);
675705
}
676706

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+
677713
public boolean connect(String hostName, int port, String initialScript) throws IOException {
678714
return connect(hostName, port, "", "", initialScript, false, null);
679715
}
@@ -974,12 +1010,12 @@ public void switchDataNode(Node node) throws IOException{
9741010
}
9751011
}
9761012

977-
public boolean connectNode(Node node) throws IOException{
1013+
public boolean connectNode(Node node) throws IOException {
9781014
log.info("Connect to " + node.hostName + ":" + node.port + ".");
9791015
while (!closed_){
9801016
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) {
9831019
if (isConnected()){
9841020
Node tmpNode = new Node();
9851021
tmpNode.hostName = node.hostName;
@@ -993,11 +1029,12 @@ else if (type == ExceptionType.ET_NODENOTAVAIL)
9931029
else
9941030
throw e;
9951031
}
996-
}else {
1032+
} else {
9971033
log.error(e.getMessage());
9981034
return false;
9991035
}
10001036
}
1037+
10011038
try {
10021039
Thread.sleep(100);
10031040
}catch (Exception e){
@@ -1009,7 +1046,6 @@ else if (type == ExceptionType.ET_NODENOTAVAIL)
10091046
}
10101047

10111048
public ExceptionType parseException(String msg, Node node){
1012-
log.info("com.xxdb.DBConnection.parseException msg: " + msg);
10131049
if(msg==null){
10141050
node.hostName = "";
10151051
node.port = 0;

0 commit comments

Comments
 (0)