1818 */
1919package org .neo4j .driver .internal .net .pooling ;
2020
21- import java .util .ArrayList ;
2221import java .util .Collections ;
23- import java .util .List ;
2422import java .util .Set ;
2523import java .util .concurrent .BlockingQueue ;
2624import java .util .concurrent .ConcurrentHashMap ;
4038 */
4139public class BlockingPooledConnectionQueue
4240{
43- public static final String LOG_NAME = "ConnectionQueue" ;
44-
4541 /** The backing queue, keeps track of connections currently in queue */
4642 private final BlockingQueue <PooledConnection > queue ;
4743 private final Logger logger ;
@@ -69,7 +65,9 @@ public boolean offer( PooledConnection pooledConnection )
6965 acquiredConnections .remove ( pooledConnection );
7066 boolean offer = queue .offer ( pooledConnection );
7167 // not added back to the queue, dispose of the connection
72- if (!offer ) {
68+ if ( !offer )
69+ {
70+ trace ( "Queue is at capacity. Offered connection will be disposed." );
7371 pooledConnection .dispose ();
7472 }
7573 if (isTerminating .get ()) {
@@ -89,12 +87,16 @@ public boolean offer( PooledConnection pooledConnection )
8987 */
9088 public PooledConnection acquire ( Supplier <PooledConnection > supplier )
9189 {
92-
9390 PooledConnection connection = queue .poll ();
9491 if ( connection == null )
9592 {
93+ trace ( "No idle connections. Creating new connection." );
9694 connection = supplier .get ();
9795 }
96+ else
97+ {
98+ trace ( "Acquired and idle connection." );
99+ }
98100 acquiredConnections .add ( connection );
99101
100102 if (isTerminating .get ()) {
@@ -105,11 +107,6 @@ public PooledConnection acquire( Supplier<PooledConnection> supplier )
105107 return connection ;
106108 }
107109
108- public List <PooledConnection > toList ()
109- {
110- return new ArrayList <>( queue );
111- }
112-
113110 public boolean isEmpty ()
114111 {
115112 return queue .isEmpty ();
@@ -140,6 +137,8 @@ public void terminate()
140137 {
141138 if ( isTerminating .compareAndSet ( false , true ) )
142139 {
140+ trace ( "Initiating connection queue termination." );
141+
143142 while ( !queue .isEmpty () )
144143 {
145144 PooledConnection idleConnection = queue .poll ();
@@ -170,6 +169,17 @@ private void disposeSafely( PooledConnection connection )
170169
171170 private static Logger createLogger ( BoltServerAddress address , Logging logging )
172171 {
173- return new DelegatingLogger ( logging .getLog ( LOG_NAME ), address .toString () );
172+ Logger log = logging .getLog ( BlockingPooledConnectionQueue .class .getSimpleName () );
173+ return new DelegatingLogger ( log , address .toString () );
174+ }
175+
176+ private void trace ( String message )
177+ {
178+ // Call to activeConnections is costly. This if block is to avoid that.
179+ if ( logger .isTraceEnabled () )
180+ {
181+ logger .trace ( "%s ActiveConnections %s IdleConnections %s" ,
182+ message , activeConnections (), queue .size () );
183+ }
174184 }
175185}
0 commit comments