StandardSocketFactory.java
9.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
The MySQL Connector/J is licensed under the terms of the GPLv2
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
this software, see the FOSS License Exception
<http://www.mysql.com/about/legal/licensing/foss-exception.html>.
This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; version 2
of the License.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this
program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
Floor, Boston, MA 02110-1301 USA
*/
package com.mysql.jdbc;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
* Socket factory for vanilla TCP/IP sockets (the standard)
*/
public class StandardSocketFactory implements SocketFactory, SocketMetadata {
public static final String TCP_NO_DELAY_PROPERTY_NAME = "tcpNoDelay";
public static final String TCP_KEEP_ALIVE_DEFAULT_VALUE = "true";
public static final String TCP_KEEP_ALIVE_PROPERTY_NAME = "tcpKeepAlive";
public static final String TCP_RCV_BUF_PROPERTY_NAME = "tcpRcvBuf";
public static final String TCP_SND_BUF_PROPERTY_NAME = "tcpSndBuf";
public static final String TCP_TRAFFIC_CLASS_PROPERTY_NAME = "tcpTrafficClass";
public static final String TCP_RCV_BUF_DEFAULT_VALUE = "0";
public static final String TCP_SND_BUF_DEFAULT_VALUE = "0";
public static final String TCP_TRAFFIC_CLASS_DEFAULT_VALUE = "0";
public static final String TCP_NO_DELAY_DEFAULT_VALUE = "true";
/** The hostname to connect to */
protected String host = null;
/** The port number to connect to */
protected int port = 3306;
/** The underlying TCP/IP socket to use */
protected Socket rawSocket = null;
/** The remaining login time in milliseconds. Initial value set from defined DriverManager.setLoginTimeout() */
protected int loginTimeoutCountdown = DriverManager.getLoginTimeout() * 1000;
/** Time when last Login Timeout check occurred */
protected long loginTimeoutCheckTimestamp = System.currentTimeMillis();
/** Backup original Socket timeout to be restored after handshake */
protected int socketTimeoutBackup = 0;
/**
* Called by the driver after issuing the MySQL protocol handshake and
* reading the results of the handshake.
*
* @throws SocketException
* if a socket error occurs
* @throws IOException
* if an I/O error occurs
*
* @return The socket to use after the handshake
*/
public Socket afterHandshake() throws SocketException, IOException {
resetLoginTimeCountdown();
this.rawSocket.setSoTimeout(this.socketTimeoutBackup);
return this.rawSocket;
}
/**
* Called by the driver before issuing the MySQL protocol handshake. Should
* return the socket instance that should be used during the handshake.
*
* @throws SocketException
* if a socket error occurs
* @throws IOException
* if an I/O error occurs
*
* @return the socket to use before the handshake
*/
public Socket beforeHandshake() throws SocketException, IOException {
resetLoginTimeCountdown();
this.socketTimeoutBackup = this.rawSocket.getSoTimeout();
this.rawSocket.setSoTimeout(getRealTimeout(this.socketTimeoutBackup));
return this.rawSocket;
}
/**
* Create the raw socket.
*
* @param props
* properties available to affect behavior during socket creation.
*/
protected Socket createSocket(Properties props) {
return new Socket();
}
/**
* Configures socket properties based on properties from the connection
* (tcpNoDelay, snd/rcv buf, traffic class, etc).
*
* @param props
* @throws SocketException
* @throws IOException
*/
private void configureSocket(Socket sock, Properties props) throws SocketException, IOException {
sock.setTcpNoDelay(Boolean.valueOf(props.getProperty(TCP_NO_DELAY_PROPERTY_NAME, TCP_NO_DELAY_DEFAULT_VALUE)).booleanValue());
String keepAlive = props.getProperty(TCP_KEEP_ALIVE_PROPERTY_NAME, TCP_KEEP_ALIVE_DEFAULT_VALUE);
if (keepAlive != null && keepAlive.length() > 0) {
sock.setKeepAlive(Boolean.valueOf(keepAlive).booleanValue());
}
int receiveBufferSize = Integer.parseInt(props.getProperty(TCP_RCV_BUF_PROPERTY_NAME, TCP_RCV_BUF_DEFAULT_VALUE));
if (receiveBufferSize > 0) {
sock.setReceiveBufferSize(receiveBufferSize);
}
int sendBufferSize = Integer.parseInt(props.getProperty(TCP_SND_BUF_PROPERTY_NAME, TCP_SND_BUF_DEFAULT_VALUE));
if (sendBufferSize > 0) {
sock.setSendBufferSize(sendBufferSize);
}
int trafficClass = Integer.parseInt(props.getProperty(TCP_TRAFFIC_CLASS_PROPERTY_NAME, TCP_TRAFFIC_CLASS_DEFAULT_VALUE));
if (trafficClass > 0) {
sock.setTrafficClass(trafficClass);
}
}
/**
* @see com.mysql.jdbc.SocketFactory#createSocket(Properties)
*/
public Socket connect(String hostname, int portNumber, Properties props) throws SocketException, IOException {
if (props != null) {
this.host = hostname;
this.port = portNumber;
String localSocketHostname = props.getProperty("localSocketAddress");
InetSocketAddress localSockAddr = null;
if (localSocketHostname != null && localSocketHostname.length() > 0) {
localSockAddr = new InetSocketAddress(InetAddress.getByName(localSocketHostname), 0);
}
String connectTimeoutStr = props.getProperty("connectTimeout");
int connectTimeout = 0;
if (connectTimeoutStr != null) {
try {
connectTimeout = Integer.parseInt(connectTimeoutStr);
} catch (NumberFormatException nfe) {
throw new SocketException("Illegal value '" + connectTimeoutStr + "' for connectTimeout");
}
}
if (this.host != null) {
InetAddress[] possibleAddresses = InetAddress.getAllByName(this.host);
if (possibleAddresses.length == 0) {
throw new SocketException("No addresses for host");
}
// save last exception to propagate to caller if connection fails
SocketException lastException = null;
// Need to loop through all possible addresses. Name lookup may return multiple addresses including IPv4 and IPv6 addresses. Some versions of
// MySQL don't listen on the IPv6 address so we try all addresses.
for (int i = 0; i < possibleAddresses.length; i++) {
try {
this.rawSocket = createSocket(props);
configureSocket(this.rawSocket, props);
InetSocketAddress sockAddr = new InetSocketAddress(possibleAddresses[i], this.port);
// bind to the local port if not using the ephemeral port
if (localSockAddr != null) {
this.rawSocket.bind(localSockAddr);
}
this.rawSocket.connect(sockAddr, getRealTimeout(connectTimeout));
break;
} catch (SocketException ex) {
lastException = ex;
resetLoginTimeCountdown();
this.rawSocket = null;
}
}
if (this.rawSocket == null && lastException != null) {
throw lastException;
}
resetLoginTimeCountdown();
return this.rawSocket;
}
}
throw new SocketException("Unable to create socket");
}
public boolean isLocallyConnected(com.mysql.jdbc.ConnectionImpl conn) throws SQLException {
return SocketMetadata.Helper.isLocallyConnected(conn);
}
/**
* Decrements elapsed time since last reset from login timeout count down.
*
* @throws SocketException
* If the login timeout is reached or exceeded.
*/
protected void resetLoginTimeCountdown() throws SocketException {
if (this.loginTimeoutCountdown > 0) {
long now = System.currentTimeMillis();
this.loginTimeoutCountdown -= now - this.loginTimeoutCheckTimestamp;
if (this.loginTimeoutCountdown <= 0) {
throw new SocketException(Messages.getString("Connection.LoginTimeout"));
}
this.loginTimeoutCheckTimestamp = now;
}
}
/**
* Validates the connection/socket timeout that must really be used.
*
* @param expectedTimeout
* The timeout to validate.
* @return The timeout to be used.
*/
protected int getRealTimeout(int expectedTimeout) {
if (this.loginTimeoutCountdown > 0 && (expectedTimeout == 0 || expectedTimeout > this.loginTimeoutCountdown)) {
return this.loginTimeoutCountdown;
}
return expectedTimeout;
}
}