fix: throw exception

This commit is contained in:
tanyawen
2019-12-30 14:53:15 +08:00
parent 0bfa4ff1fa
commit 9a9e1cf9f0
3 changed files with 25 additions and 19 deletions

View File

@@ -13,17 +13,20 @@ public class ConnectionPool {
*/
private final static ConcurrentHashMap<String, ConnectionManager> CP = new ConcurrentHashMap<String, ConnectionManager>();
public static Connection getConnection(InetSocketAddress socketAddress) throws MyException, IOException {
public static Connection getConnection(InetSocketAddress socketAddress) throws MyException {
if (socketAddress == null) {
return null;
}
String key = getKey(socketAddress);
ConnectionManager connectionManager;
synchronized (ConnectionPool.class) {
connectionManager = CP.get(key);
if (connectionManager == null) {
connectionManager = new ConnectionManager(socketAddress);
CP.put(key, connectionManager);
connectionManager = CP.get(key);
if (connectionManager == null) {
synchronized (ConnectionPool.class) {
connectionManager = CP.get(key);
if (connectionManager == null) {
connectionManager = new ConnectionManager(socketAddress);
CP.put(key, connectionManager);
}
}
}
return connectionManager.getConnection();
@@ -60,7 +63,7 @@ public class ConnectionPool {
if (socketAddress == null) {
return null;
}
return String.format("%s:%s", socketAddress.getHostName(), socketAddress.getPort());
return String.format("%s:%s", socketAddress.getHostName().trim(), socketAddress.getPort());
}
@Override