Skip to content

Commit 239792d

Browse files
author
lucaijun
committed
AJ-566: optimize the connection in connection pool can connect twice
1 parent 3b9a832 commit 239792d

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

src/com/xxdb/SimpleDBConnectionPool.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,8 @@ protected class SimpleDBConnectionPoolImpl {
9797
try {
9898
for (int i = 0; i < initialPoolSize; i++) {
9999
PoolEntry poolEntry = new PoolEntry(useSSL, compress, usePython, String.format("DolphinDBConnection_%d", i + 1));
100-
if (poolEntry.connect(hostName, port, userId, password, initialScript, enableHighAvailability, highAvailabilitySites, reconnect, loadBalance)) {
101-
poolEntry.isFirstConnect = false;
102-
poolEntry.isFirstLogin = false;
103-
}else {
100+
if (!poolEntry.entryConnect(hostName, port, userId, password, initialScript, enableHighAvailability, highAvailabilitySites, reconnect, loadBalance))
104101
log.error(String.format("Connection %s connect failure.", poolEntry.connectionName));
105-
}
106102
poolEntryArrayList.add(poolEntry);
107103
}
108104
poolEntries = new CopyOnWriteArrayList<>(poolEntryArrayList);
@@ -162,7 +158,6 @@ boolean isClosed() {
162158
class PoolEntry extends DBConnection {
163159
AtomicBoolean inUse = new AtomicBoolean(false);
164160
volatile boolean isFirstConnect = true;
165-
volatile boolean isFirstLogin = true;
166161
String connectionName;
167162

168163
PoolEntry(boolean useSSL, boolean compress, boolean usePython, String connectionName) {
@@ -183,20 +178,18 @@ public void setLoadBalance(boolean loadBalance) {
183178
throw new RuntimeException("The loadBalance configuration of connection in connection pool can only be set in SimpleDBConnectionPoolConfig.");
184179
}
185180

181+
private boolean entryConnect(String hostName, int port, String userId, String password, String initialScript, boolean enableHighAvailability, String[] highAvailabilitySites, boolean reconnect, boolean enableLoadBalance) throws IOException {
182+
return super.connect(hostName, port, userId, password, initialScript, enableHighAvailability, highAvailabilitySites, reconnect, enableLoadBalance);
183+
}
184+
186185
@Override
187186
public boolean connect(String hostName, int port, String userId, String password, String initialScript, boolean enableHighAvailability, String[] highAvailabilitySites, boolean reconnect, boolean enableLoadBalance) throws IOException {
188-
if (isFirstConnect)
189-
return super.connect(hostName, port, userId, password, initialScript, enableHighAvailability, highAvailabilitySites, reconnect, enableLoadBalance);
190-
else
191-
throw new RuntimeException("The connection in connection pool can only connect by pool.");
187+
throw new RuntimeException("The connection in connection pool can only connect by pool.");
192188
}
193189

194190
@Override
195191
public void login(String userId, String password, boolean enableEncryption) throws IOException {
196-
if (isFirstLogin)
197-
super.login(userId, password, enableEncryption);
198-
else
199-
throw new RuntimeException("The connection in connection pool can only login by pool.");
192+
throw new RuntimeException("The connection in connection pool can only login by pool.");
200193
}
201194

202195
@Override

src/com/xxdb/SimpleDBConnectionPoolConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ protected void validate() {
137137
userId = getNullIfEmpty(userId);
138138
if (Objects.isNull(userId)){
139139
userId = "";
140-
log.warn("Log in needs userId.");
140+
log.warn("Login needs userId.");
141141
}
142142
password = getNullIfEmpty(password);
143143
if (Objects.isNull(password)){
144144
password = "";
145-
log.warn("Log in needs password.");
145+
log.warn("Login needs password.");
146146
}
147147
if (initialPoolSize <= 0) {
148148
initialPoolSize = 5;

0 commit comments

Comments
 (0)