1
1
2
2
package io .helidon .benchmark .nima .models ;
3
3
4
- import java .util .concurrent .locks .ReentrantLock ;
4
+ import java .util .concurrent .locks .ReadWriteLock ;
5
+ import java .util .concurrent .locks .ReentrantReadWriteLock ;
5
6
import java .util .logging .Logger ;
6
7
7
8
import io .helidon .config .Config ;
@@ -13,7 +14,7 @@ class PgClientConnectionPoolArray extends PgClientConnectionPool {
13
14
14
15
private final int connections ;
15
16
private final PgClientConnection [] connectionArray ;
16
- private final ReentrantLock lock = new ReentrantLock ();
17
+ private final ReadWriteLock lock = new ReentrantReadWriteLock ();
17
18
18
19
PgClientConnectionPoolArray (Vertx vertx , PgConnectOptions options , Config config ) {
19
20
super (vertx , options , config );
@@ -29,20 +30,23 @@ class PgClientConnectionPoolArray extends PgClientConnectionPool {
29
30
@ Override
30
31
public PgClientConnection clientConnection () {
31
32
int index = Thread .currentThread ().hashCode () % connections ;
32
- PgClientConnection connection = connectionArray [index ];
33
- if (connection == null ) {
34
- try {
35
- lock .lock ();
36
- connection = connectionArray [index ];
37
- if (connection == null ) {
38
- connection = newConnection ();
39
- connectionArray [index ] = connection ;
33
+ if (connectionArray [index ] == null ) {
34
+ lock .readLock ().lock ();
35
+ if (connectionArray [index ] == null ) {
36
+ lock .readLock ().unlock ();
37
+ lock .writeLock ().lock ();
38
+ try {
39
+ if (connectionArray [index ] == null ) {
40
+ connectionArray [index ] = newConnection ();
41
+ }
42
+ } finally {
43
+ lock .writeLock ().unlock ();
40
44
}
41
- } finally {
42
- lock .unlock ();
45
+ } else {
46
+ lock .readLock (). unlock ();
43
47
}
44
48
}
45
- return connection ;
49
+ return connectionArray [ index ] ;
46
50
}
47
51
48
52
@ Override
0 commit comments