1- package me .zort .sqllib ;
1+ package me .zort .sqllib . pool ;
22
3- import lombok .AccessLevel ;
43import lombok .Data ;
54import lombok .RequiredArgsConstructor ;
5+ import me .zort .sqllib .SQLConnectionBuilder ;
6+ import me .zort .sqllib .SQLDatabaseConnection ;
7+ import me .zort .sqllib .SQLDatabaseConnectionImpl ;
68import org .jetbrains .annotations .NotNull ;
79
810import java .sql .SQLException ;
@@ -39,9 +41,10 @@ public static final class Options {
3941 private final boolean blockWhenExhausted ;
4042
4143 // --***-- Pooled connection caches --***--
42- private final Queue <SQLPooledConnection > freeConnections = new ConcurrentLinkedQueue <>();
43- private final List <SQLPooledConnection > usedConnections = new CopyOnWriteArrayList <>();
44+ private final Queue <PooledSQLDatabaseConnection > freeConnections = new ConcurrentLinkedQueue <>();
45+ private final List <PooledSQLDatabaseConnection > usedConnections = new CopyOnWriteArrayList <>();
4446
47+ @ SuppressWarnings ("unused" )
4548 public SQLConnectionPool (@ NotNull SQLConnectionBuilder from ) {
4649 this (from , new Options ());
4750 }
@@ -68,9 +71,9 @@ public SQLConnectionPool(@NotNull SQLConnectionBuilder from, @NotNull Options po
6871 * @throws SQLException Connection error.
6972 */
7073 @ NotNull
71- public Resource getResource () throws SQLException {
72- freeConnections .removeIf (SQLPooledConnection ::expired );
73- SQLPooledConnection polled = freeConnections .poll ();
74+ public SQLDatabaseConnection getResource () throws SQLException {
75+ freeConnections .removeIf (this ::expired );
76+ PooledSQLDatabaseConnection polled = freeConnections .poll ();
7477 if (polled == null && size () < maxConnections ) {
7578 polled = establishObject ();
7679 } else if (polled == null ) {
@@ -90,29 +93,40 @@ public Resource getResource() throws SQLException {
9093 }
9194 }
9295 usedConnections .add (polled );
93- return new Resource ( this , polled ) ;
96+ return polled ;
9497 }
9598
96- private SQLPooledConnection establishObject () throws SQLException {
97- SQLPooledConnection polled = new SQLPooledConnection (builder .build ());
98- polled .connection .connect ();
99+ private PooledSQLDatabaseConnection establishObject () throws SQLException {
100+ SQLDatabaseConnection polled_ = builder .build ();
101+ if (!(polled_ instanceof PooledSQLDatabaseConnection )) {
102+ throw new SQLException ("Builder does not produce a pooled connection." );
103+ }
104+ PooledSQLDatabaseConnection polled = (PooledSQLDatabaseConnection ) polled_ ;
105+ polled .setAssignedPool (this );
106+ polled .connect ();
99107
100- SQLException error = polled .connection . getLastError ();
108+ SQLException error = polled .getLastError ();
101109 if (error != null ) throw error ;
102110
103- if (polled . connection instanceof SQLDatabaseConnectionImpl ) {
104- ((SQLDatabaseConnectionImpl ) polled . connection ).addErrorHandler (code -> {
111+ if (polled instanceof SQLDatabaseConnectionImpl ) {
112+ ((SQLDatabaseConnectionImpl ) polled ).addErrorHandler (code -> {
105113 // Remove the connection from the pool and disconnect
106114 // on fatal errors.
107115 freeConnections .remove (polled );
108116 usedConnections .remove (polled );
109- polled .connection . disconnect ();
117+ polled .disconnect ();
110118 });
111119 }
112120
113121 return polled ;
114122 }
115123
124+ void releaseObject (PooledSQLDatabaseConnection connection ) {
125+ connection .setLastUsed (System .currentTimeMillis ());
126+ freeConnections .add (connection );
127+ usedConnections .remove (connection );
128+ }
129+
116130 public int size () {
117131 return usedConnections .size () + freeConnections .size ();
118132 }
@@ -122,38 +136,14 @@ public int size() {
122136 * clears the caches.
123137 */
124138 public void close () {
125- usedConnections .forEach (c -> c . connection . disconnect () );
126- freeConnections .forEach (c -> c . connection . disconnect () );
139+ usedConnections .forEach (SQLDatabaseConnection :: disconnect );
140+ freeConnections .forEach (SQLDatabaseConnection :: disconnect );
127141 usedConnections .clear ();
128142 freeConnections .clear ();
129143 }
130144
131- @ RequiredArgsConstructor (access = AccessLevel .PRIVATE )
132- private static final class SQLPooledConnection {
133- private final SQLDatabaseConnection connection ;
134- private long lastUsed = System .currentTimeMillis ();
135-
136- public boolean expired () {
137- return System .currentTimeMillis () - lastUsed > 1000 * 60 * 5 ;
138- }
139- }
140-
141- @ RequiredArgsConstructor (access = AccessLevel .PRIVATE )
142- public static final class Resource implements AutoCloseable {
143-
144- private final SQLConnectionPool pool ;
145- private final SQLPooledConnection connection ;
146-
147- public SQLDatabaseConnection getConnection () {
148- return connection .connection ;
149- }
150-
151- @ Override
152- public void close () {
153- connection .lastUsed = System .currentTimeMillis ();
154- pool .freeConnections .add (connection );
155- pool .usedConnections .remove (connection );
156- }
145+ private boolean expired (PooledSQLDatabaseConnection connection ) {
146+ return System .currentTimeMillis () - connection .getLastUsed () > 1000 * 60 * 5 ;
157147 }
158148
159149}
0 commit comments