Skip to content

Commit b813565

Browse files
author
lucaijun
committed
AJ-557: fix connection pool cannot create without userId and password
1 parent 11c40a6 commit b813565

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

src/com/xxdb/SimpleDBConnectionPool.java

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
import com.xxdb.data.BasicInt;
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
6-
76
import java.util.ArrayList;
87
import java.util.Objects;
98
import java.util.concurrent.CopyOnWriteArrayList;
109
import java.util.concurrent.atomic.AtomicBoolean;
1110

1211
public class SimpleDBConnectionPool {
13-
private SimpleDBConnectionPoolImpl connectionPool;
12+
private volatile SimpleDBConnectionPoolImpl connectionPool;
1413
private String hostName;
1514
private int port;
1615
private String userId;
@@ -40,6 +39,7 @@ public SimpleDBConnectionPool(SimpleDBConnectionPoolConfig simpleDBConnectionPoo
4039
this.loadBalance = simpleDBConnectionPoolConfig.isLoadBalance();
4140
this.enableHighAvailability = simpleDBConnectionPoolConfig.isEnableHighAvailability();
4241
this.highAvailabilitySites = simpleDBConnectionPoolConfig.getHighAvailabilitySites();
42+
this.connectionPool = new SimpleDBConnectionPoolImpl();
4343
}
4444

4545
public DBConnection getConnection() {
@@ -58,41 +58,32 @@ else if (Objects.nonNull(connectionPool)) {
5858
}
5959

6060
public int getActiveConnectionsCount() {
61-
if (Objects.isNull(connectionPool))
62-
return 0;
63-
if (connectionPool.isClosed())
64-
return 0;
61+
if (isClosed())
62+
throw new RuntimeException("The connection pool has been closed.");
6563
return connectionPool.getCount(false);
6664
}
6765

6866
public int getIdleConnectionsCount() {
69-
if (Objects.isNull(connectionPool))
70-
return initialPoolSize;
71-
if (connectionPool.isClosed())
72-
return 0;
67+
if (isClosed())
68+
throw new RuntimeException("The connection pool has been closed.");
7369
return connectionPool.getCount(true);
7470
}
7571

7672
public int getTotalConnectionsCount() {
77-
if (Objects.isNull(connectionPool))
78-
return initialPoolSize;
79-
if (connectionPool.isClosed())
80-
return 0;
73+
if (isClosed())
74+
throw new RuntimeException("The connection pool has been closed.");
8175
return connectionPool.getTotalCount();
8276
}
8377

8478
public void close() {
85-
if (Objects.nonNull(connectionPool))
79+
if (!isClosed())
8680
connectionPool.close();
8781
else
8882
log.info("The connection pool is closed.");
8983
}
9084

9185
public boolean isClosed() {
92-
if (Objects.nonNull(connectionPool))
93-
return connectionPool.isClosed();
94-
else
95-
return false;
86+
return connectionPool.isClosed();
9687
}
9788

9889
protected class SimpleDBConnectionPoolImpl {
@@ -112,6 +103,7 @@ protected class SimpleDBConnectionPoolImpl {
112103
poolEntries = new CopyOnWriteArrayList<>(poolEntryArrayList);
113104
} catch (Exception e) {
114105
log.error("Create connection pool failure, because " + e.getMessage());
106+
throw new RuntimeException(e);
115107
}
116108
}
117109

@@ -141,8 +133,10 @@ int getTotalCount() {
141133
void close() {
142134
if (!this.isShutdown.getAndSet(true)) {
143135
log.info("Closing the connection pool......");
144-
for (PoolEntry poolEntry : poolEntries) {
145-
poolEntry.release();
136+
if (Objects.nonNull(poolEntries)){
137+
for (PoolEntry poolEntry : poolEntries) {
138+
poolEntry.release();
139+
}
146140
}
147141
log.info("Closing the connection pool finished.");
148142
} else {

src/com/xxdb/SimpleDBConnectionPoolConfig.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,15 @@ public void validate() {
135135
log.warn("Invalid port, use the default value 8848.");
136136
}
137137
userId = getNullIfEmpty(userId);
138-
if (Objects.isNull(userId))
138+
if (Objects.isNull(userId)){
139+
userId = "";
139140
log.warn("Logging in needs userId.");
141+
}
140142
password = getNullIfEmpty(password);
141-
if (Objects.isNull(password))
143+
if (Objects.isNull(password)){
144+
password = "";
142145
log.warn("Logging in needs password.");
146+
}
143147
if (initialPoolSize <= 0) {
144148
initialPoolSize = 5;
145149
log.warn("The number of connection pools is invalid, use the default value 5.");

0 commit comments

Comments
 (0)