Skip to content

Commit 9a3f7bc

Browse files
author
lucaijun
committed
AJ-542: add simple connection pool with parameter verification
1 parent c4739db commit 9a3f7bc

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

src/com/xxdb/SimpleDBConnectionPool.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,11 @@ public class SimpleDBConnectionPool {
2727
private static final Logger log = LoggerFactory.getLogger(DBConnection.class);
2828

2929
public SimpleDBConnectionPool(SimpleDBConnectionPoolConfig simpleDBConnectionPoolConfig) {
30-
if (Objects.isNull(simpleDBConnectionPoolConfig.getHostName()))
31-
throw new RuntimeException("The host name can not be null.");
30+
simpleDBConnectionPoolConfig.validate();
3231
this.hostName = simpleDBConnectionPoolConfig.getHostName();
33-
if (simpleDBConnectionPoolConfig.getPort() <= 0)
34-
throw new RuntimeException("The port needs to be greater than 0.");
3532
this.port = simpleDBConnectionPoolConfig.getPort();
36-
if (Objects.isNull(simpleDBConnectionPoolConfig.getUserId()))
37-
throw new RuntimeException("The user Id can not be null.");
3833
this.userId = simpleDBConnectionPoolConfig.getUserId();
39-
if (Objects.isNull(simpleDBConnectionPoolConfig.getPassword()))
40-
throw new RuntimeException("The password can not be null.");
4134
this.password = simpleDBConnectionPoolConfig.getPassword();
42-
if (simpleDBConnectionPoolConfig.getInitialPoolSize() <= 0)
43-
throw new RuntimeException("The number of connection pools needs to be greater than 0.");
4435
this.initialPoolSize = simpleDBConnectionPoolConfig.getInitialPoolSize();
4536
this.initialScript = simpleDBConnectionPoolConfig.getInitialScript();
4637
this.compress = simpleDBConnectionPoolConfig.isCompress();

src/com/xxdb/SimpleDBConnectionPoolConfig.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.xxdb;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import java.util.Objects;
6+
37
public class SimpleDBConnectionPoolConfig {
48
private String hostName;
59
private int port;
@@ -13,6 +17,7 @@ public class SimpleDBConnectionPoolConfig {
1317
private boolean loadBalance = false;
1418
private boolean enableHighAvailability = false;
1519
private String[] highAvailabilitySites = null;
20+
private static final Logger log = LoggerFactory.getLogger(DBConnection.class);
1621

1722
public SimpleDBConnectionPoolConfig() {
1823
}
@@ -30,6 +35,8 @@ public int getPort() {
3035
}
3136

3237
public void setPort(int port) {
38+
if (port <= 0)
39+
throw new RuntimeException("The port should be positive.");
3340
this.port = port;
3441
}
3542

@@ -54,6 +61,8 @@ public int getInitialPoolSize() {
5461
}
5562

5663
public void setInitialPoolSize(int initialPoolSize) {
64+
if (initialPoolSize <= 0)
65+
throw new RuntimeException("The number of connection pools should be positive.");
5766
this.initialPoolSize = initialPoolSize;
5867
}
5968

@@ -112,6 +121,28 @@ public String[] getHighAvailabilitySites() {
112121
public void setHighAvailabilitySites(String[] highAvailabilitySites) {
113122
this.highAvailabilitySites = highAvailabilitySites;
114123
}
124+
125+
public void validate() {
126+
hostName = getNullIfEmpty(hostName);
127+
if (Objects.isNull(hostName)) {
128+
hostName = "localhost";
129+
log.warn("HostName not set, use the default value 'localhost'");
130+
}
131+
if (port <= 0)
132+
throw new RuntimeException("The port should be positive.");
133+
userId = getNullIfEmpty(userId);
134+
if (Objects.isNull(userId))
135+
log.warn("Logging in needs userId.");
136+
password = getNullIfEmpty(password);
137+
if (Objects.isNull(password))
138+
log.warn("Logging in needs password.");
139+
if (initialPoolSize <= 0)
140+
throw new RuntimeException("The number of connection pools should be positive.");
141+
}
142+
143+
private static String getNullIfEmpty(String text) {
144+
return text == null ? null : (text.trim().isEmpty() ? null : text.trim());
145+
}
115146
}
116147

117148

0 commit comments

Comments
 (0)