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. Offered connection will be disposed." );
73
71
pooledConnection .dispose ();
74
72
}
75
73
if (isTerminating .get ()) {
@@ -89,12 +87,16 @@ public boolean offer( PooledConnection pooledConnection )
89
87
*/
90
88
public PooledConnection acquire ( Supplier <PooledConnection > supplier )
91
89
{
92
-
93
90
PooledConnection connection = queue .poll ();
94
91
if ( connection == null )
95
92
{
93
+ trace ( "No idle connections. Creating new connection." );
96
94
connection = supplier .get ();
97
95
}
96
+ else
97
+ {
98
+ trace ( "Acquired and idle connection." );
99
+ }
98
100
acquiredConnections .add ( connection );
99
101
100
102
if (isTerminating .get ()) {
@@ -105,11 +107,6 @@ public PooledConnection acquire( Supplier<PooledConnection> supplier )
105
107
return connection ;
106
108
}
107
109
108
- public List <PooledConnection > toList ()
109
- {
110
- return new ArrayList <>( queue );
111
- }
112
-
113
110
public boolean isEmpty ()
114
111
{
115
112
return queue .isEmpty ();
@@ -140,6 +137,8 @@ public void terminate()
140
137
{
141
138
if ( isTerminating .compareAndSet ( false , true ) )
142
139
{
140
+ trace ( "Initiating connection queue termination." );
141
+
143
142
while ( !queue .isEmpty () )
144
143
{
145
144
PooledConnection idleConnection = queue .poll ();
@@ -170,6 +169,17 @@ private void disposeSafely( PooledConnection connection )
170
169
171
170
private static Logger createLogger ( BoltServerAddress address , Logging logging )
172
171
{
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
+ }
174
184
}
175
185
}
0 commit comments