33import com .xxdb .data .BasicInt ;
44import org .slf4j .Logger ;
55import org .slf4j .LoggerFactory ;
6-
76import java .util .ArrayList ;
87import java .util .Objects ;
98import java .util .concurrent .CopyOnWriteArrayList ;
109import java .util .concurrent .atomic .AtomicBoolean ;
1110
1211public 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 {
0 commit comments