3
3
import com .xxdb .data .BasicInt ;
4
4
import org .slf4j .Logger ;
5
5
import org .slf4j .LoggerFactory ;
6
-
7
6
import java .util .ArrayList ;
8
7
import java .util .Objects ;
9
8
import java .util .concurrent .CopyOnWriteArrayList ;
10
9
import java .util .concurrent .atomic .AtomicBoolean ;
11
10
12
11
public class SimpleDBConnectionPool {
13
- private SimpleDBConnectionPoolImpl connectionPool ;
12
+ private volatile SimpleDBConnectionPoolImpl connectionPool ;
14
13
private String hostName ;
15
14
private int port ;
16
15
private String userId ;
@@ -40,6 +39,7 @@ public SimpleDBConnectionPool(SimpleDBConnectionPoolConfig simpleDBConnectionPoo
40
39
this .loadBalance = simpleDBConnectionPoolConfig .isLoadBalance ();
41
40
this .enableHighAvailability = simpleDBConnectionPoolConfig .isEnableHighAvailability ();
42
41
this .highAvailabilitySites = simpleDBConnectionPoolConfig .getHighAvailabilitySites ();
42
+ this .connectionPool = new SimpleDBConnectionPoolImpl ();
43
43
}
44
44
45
45
public DBConnection getConnection () {
@@ -58,41 +58,32 @@ else if (Objects.nonNull(connectionPool)) {
58
58
}
59
59
60
60
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." );
65
63
return connectionPool .getCount (false );
66
64
}
67
65
68
66
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." );
73
69
return connectionPool .getCount (true );
74
70
}
75
71
76
72
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." );
81
75
return connectionPool .getTotalCount ();
82
76
}
83
77
84
78
public void close () {
85
- if (Objects . nonNull ( connectionPool ))
79
+ if (! isClosed ( ))
86
80
connectionPool .close ();
87
81
else
88
82
log .info ("The connection pool is closed." );
89
83
}
90
84
91
85
public boolean isClosed () {
92
- if (Objects .nonNull (connectionPool ))
93
- return connectionPool .isClosed ();
94
- else
95
- return false ;
86
+ return connectionPool .isClosed ();
96
87
}
97
88
98
89
protected class SimpleDBConnectionPoolImpl {
@@ -112,6 +103,7 @@ protected class SimpleDBConnectionPoolImpl {
112
103
poolEntries = new CopyOnWriteArrayList <>(poolEntryArrayList );
113
104
} catch (Exception e ) {
114
105
log .error ("Create connection pool failure, because " + e .getMessage ());
106
+ throw new RuntimeException (e );
115
107
}
116
108
}
117
109
@@ -141,8 +133,10 @@ int getTotalCount() {
141
133
void close () {
142
134
if (!this .isShutdown .getAndSet (true )) {
143
135
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
+ }
146
140
}
147
141
log .info ("Closing the connection pool finished." );
148
142
} else {
0 commit comments