18
18
*/
19
19
package org .neo4j .driver .internal .net .pooling ;
20
20
21
- import java .util .ArrayList ;
22
21
import java .util .Collections ;
23
- import java .util .List ;
24
22
import java .util .Set ;
25
23
import java .util .concurrent .BlockingQueue ;
26
24
import java .util .concurrent .ConcurrentHashMap ;
40
38
*/
41
39
public class BlockingPooledConnectionQueue
42
40
{
43
- public static final String LOG_NAME = "ConnectionQueue" ;
44
-
45
41
/** The backing queue, keeps track of connections currently in queue */
46
42
private final BlockingQueue <PooledConnection > queue ;
47
43
private final Logger logger ;
@@ -69,7 +65,9 @@ public boolean offer( PooledConnection pooledConnection )
69
65
acquiredConnections .remove ( pooledConnection );
70
66
boolean offer = queue .offer ( pooledConnection );
71
67
// 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" );
73
71
pooledConnection .dispose ();
74
72
}
75
73
if (isTerminating .get ()) {
@@ -93,8 +91,13 @@ public PooledConnection acquire( Supplier<PooledConnection> supplier )
93
91
PooledConnection connection = queue .poll ();
94
92
if ( connection == null )
95
93
{
94
+ trace ( "No Idle connections. Creating new connection." );
96
95
connection = supplier .get ();
97
96
}
97
+ else
98
+ {
99
+ trace ( "Acquiring connection." );
100
+ }
98
101
acquiredConnections .add ( connection );
99
102
100
103
if (isTerminating .get ()) {
@@ -105,11 +108,6 @@ public PooledConnection acquire( Supplier<PooledConnection> supplier )
105
108
return connection ;
106
109
}
107
110
108
- public List <PooledConnection > toList ()
109
- {
110
- return new ArrayList <>( queue );
111
- }
112
-
113
111
public boolean isEmpty ()
114
112
{
115
113
return queue .isEmpty ();
@@ -140,6 +138,8 @@ public void terminate()
140
138
{
141
139
if ( isTerminating .compareAndSet ( false , true ) )
142
140
{
141
+ trace ( "Initiating connection queue termination." );
142
+
143
143
while ( !queue .isEmpty () )
144
144
{
145
145
PooledConnection idleConnection = queue .poll ();
@@ -170,6 +170,17 @@ private void disposeSafely( PooledConnection connection )
170
170
171
171
private static Logger createLogger ( BoltServerAddress address , Logging logging )
172
172
{
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
+ }
174
185
}
175
186
}
0 commit comments