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. Connection will be disposed" );
7371 pooledConnection .dispose ();
7472 }
7573 if (isTerminating .get ()) {
@@ -93,8 +91,13 @@ public PooledConnection acquire( Supplier<PooledConnection> supplier )
9391 PooledConnection connection = queue .poll ();
9492 if ( connection == null )
9593 {
94+ trace ( "No Idle connections. Creating new connection." );
9695 connection = supplier .get ();
9796 }
97+ else
98+ {
99+ trace ( "Acquiring connection." );
100+ }
98101 acquiredConnections .add ( connection );
99102
100103 if (isTerminating .get ()) {
@@ -105,11 +108,6 @@ public PooledConnection acquire( Supplier<PooledConnection> supplier )
105108 return connection ;
106109 }
107110
108- public List <PooledConnection > toList ()
109- {
110- return new ArrayList <>( queue );
111- }
112-
113111 public boolean isEmpty ()
114112 {
115113 return queue .isEmpty ();
@@ -140,6 +138,8 @@ public void terminate()
140138 {
141139 if ( isTerminating .compareAndSet ( false , true ) )
142140 {
141+ trace ( "Initiating connection queue termination." );
142+
143143 while ( !queue .isEmpty () )
144144 {
145145 PooledConnection idleConnection = queue .poll ();
@@ -170,6 +170,17 @@ private void disposeSafely( PooledConnection connection )
170170
171171 private static Logger createLogger ( BoltServerAddress address , Logging logging )
172172 {
173- return new DelegatingLogger ( logging .getLog ( LOG_NAME ), address .toString () );
173+ Logger log = logging .getLog ( BlockingPooledConnectionQueue .class .getSimpleName () );
174+ return new DelegatingLogger ( log , address .toString () );
175+ }
176+
177+ private void trace ( String message )
178+ {
179+ //Call to activeConnections is costly. This if block is to avoid that.
180+ if ( logger .isTraceEnabled () )
181+ {
182+ logger .trace ( "%s ActiveConnections %s IdleConnections %s" ,
183+ message , activeConnections (), queue .size () );
184+ }
174185 }
175186}
0 commit comments