6
6
import java .util .concurrent .atomic .AtomicReference ;
7
7
import java .util .concurrent .locks .Lock ;
8
8
import java .util .concurrent .locks .ReentrantLock ;
9
+ import java .util .function .Predicate ;
9
10
import java .util .function .Supplier ;
10
11
11
12
import org .apache .commons .pool2 .BasePooledObjectFactory ;
60
61
* </pre>
61
62
*
62
63
* @author Mark Paluch
64
+ * @author dae won
63
65
* @since 4.3
64
66
*/
65
67
public abstract class ConnectionPoolSupport {
@@ -69,7 +71,8 @@ private ConnectionPoolSupport() {
69
71
70
72
/**
71
73
* Creates a new {@link GenericObjectPool} using the {@link Supplier}. Allocated instances are wrapped and must not be
72
- * returned with {@link ObjectPool#returnObject(Object)}.
74
+ * returned with {@link ObjectPool#returnObject(Object)}. By default, connections are validated by checking their
75
+ * {@link StatefulConnection#isOpen()} method.
73
76
*
74
77
* @param connectionSupplier must not be {@code null}.
75
78
* @param config must not be {@code null}.
@@ -78,30 +81,67 @@ private ConnectionPoolSupport() {
78
81
*/
79
82
public static <T extends StatefulConnection <?, ?>> GenericObjectPool <T > createGenericObjectPool (
80
83
Supplier <T > connectionSupplier , GenericObjectPoolConfig <T > config ) {
81
- return createGenericObjectPool (connectionSupplier , config , true );
84
+ return createGenericObjectPool (connectionSupplier , config , true , ( c ) -> c . isOpen () );
82
85
}
83
86
84
87
/**
85
- * Creates a new {@link GenericObjectPool} using the {@link Supplier}.
88
+ * Creates a new {@link GenericObjectPool} using the {@link Supplier}. Allocated instances are wrapped and must not be
89
+ * returned with {@link ObjectPool#returnObject(Object)}.
90
+ *
91
+ * @param connectionSupplier must not be {@code null}.
92
+ * @param config must not be {@code null}.
93
+ * @param validationPredicate a {@link Predicate} to help validate connections
94
+ * @param <T> connection type.
95
+ * @return the connection pool.
96
+ */
97
+ public static <T extends StatefulConnection <?, ?>> GenericObjectPool <T > createGenericObjectPool (
98
+ Supplier <T > connectionSupplier , GenericObjectPoolConfig <T > config , Predicate <T > validationPredicate ) {
99
+ return createGenericObjectPool (connectionSupplier , config , true , validationPredicate );
100
+ }
101
+
102
+ /**
103
+ * Creates a new {@link GenericObjectPool} using the {@link Supplier}. By default, connections are validated by checking
104
+ * their {@link StatefulConnection#isOpen()} method.
86
105
*
87
106
* @param connectionSupplier must not be {@code null}.
88
107
* @param config must not be {@code null}.
89
108
* @param wrapConnections {@code false} to return direct connections that need to be returned to the pool using
90
- * {@link ObjectPool#returnObject(Object)}. {@code true} to return wrapped connection that are returned to the pool
109
+ * {@link ObjectPool#returnObject(Object)}. {@code true} to return wrapped connections that are returned to the pool
91
110
* when invoking {@link StatefulConnection#close()}.
92
111
* @param <T> connection type.
93
112
* @return the connection pool.
94
113
*/
95
114
@ SuppressWarnings ("unchecked" )
96
115
public static <T extends StatefulConnection <?, ?>> GenericObjectPool <T > createGenericObjectPool (
97
116
Supplier <T > connectionSupplier , GenericObjectPoolConfig <T > config , boolean wrapConnections ) {
117
+ return createGenericObjectPool (connectionSupplier , config , wrapConnections , (c ) -> c .isOpen ());
118
+ }
119
+
120
+ /**
121
+ * Creates a new {@link GenericObjectPool} using the {@link Supplier}.
122
+ *
123
+ * @param connectionSupplier must not be {@code null}.
124
+ * @param config must not be {@code null}.
125
+ * @param wrapConnections {@code false} to return direct connections that need to be returned to the pool using
126
+ * {@link ObjectPool#returnObject(Object)}. {@code true} to return wrapped connections that are returned to the pool
127
+ * when invoking {@link StatefulConnection#close()}.
128
+ * @param validationPredicate a {@link Predicate} to help validate connections
129
+ * @param <T> connection type.
130
+ * @return the connection pool.
131
+ */
132
+ @ SuppressWarnings ("unchecked" )
133
+ public static <T extends StatefulConnection <?, ?>> GenericObjectPool <T > createGenericObjectPool (
134
+ Supplier <T > connectionSupplier , GenericObjectPoolConfig <T > config , boolean wrapConnections ,
135
+ Predicate <T > validationPredicate ) {
98
136
99
137
LettuceAssert .notNull (connectionSupplier , "Connection supplier must not be null" );
100
138
LettuceAssert .notNull (config , "GenericObjectPoolConfig must not be null" );
139
+ LettuceAssert .notNull (validationPredicate , "Connection validator must not be null" );
101
140
102
141
AtomicReference <Origin <T >> poolRef = new AtomicReference <>();
103
142
104
- GenericObjectPool <T > pool = new GenericObjectPool <T >(new RedisPooledObjectFactory <T >(connectionSupplier ), config ) {
143
+ GenericObjectPool <T > pool = new GenericObjectPool <T >(
144
+ new RedisPooledObjectFactory <>(connectionSupplier , validationPredicate ), config ) {
105
145
106
146
@ Override
107
147
public T borrowObject () throws Exception {
@@ -144,20 +184,38 @@ public void returnObject(T obj) {
144
184
*
145
185
* @param connectionSupplier must not be {@code null}.
146
186
* @param wrapConnections {@code false} to return direct connections that need to be returned to the pool using
147
- * {@link ObjectPool#returnObject(Object)}. {@code true} to return wrapped connection that are returned to the pool
187
+ * {@link ObjectPool#returnObject(Object)}. {@code true} to return wrapped connections that are returned to the pool
148
188
* when invoking {@link StatefulConnection#close()}.
149
189
* @param <T> connection type.
150
190
* @return the connection pool.
151
191
*/
152
192
@ SuppressWarnings ("unchecked" )
153
193
public static <T extends StatefulConnection <?, ?>> SoftReferenceObjectPool <T > createSoftReferenceObjectPool (
154
194
Supplier <T > connectionSupplier , boolean wrapConnections ) {
195
+ return createSoftReferenceObjectPool (connectionSupplier , wrapConnections , (c ) -> c .isOpen ());
196
+ }
197
+
198
+ /**
199
+ * Creates a new {@link SoftReferenceObjectPool} using the {@link Supplier}.
200
+ *
201
+ * @param connectionSupplier must not be {@code null}.
202
+ * @param wrapConnections {@code false} to return direct connections that need to be returned to the pool using
203
+ * {@link ObjectPool#returnObject(Object)}. {@code true} to return wrapped connections that are returned to the pool
204
+ * when invoking {@link StatefulConnection#close()}.
205
+ * @param validationPredicate a {@link Predicate} to help validate connections
206
+ * @param <T> connection type.
207
+ * @return the connection pool.
208
+ */
209
+ @ SuppressWarnings ("unchecked" )
210
+ public static <T extends StatefulConnection <?, ?>> SoftReferenceObjectPool <T > createSoftReferenceObjectPool (
211
+ Supplier <T > connectionSupplier , boolean wrapConnections , Predicate <T > validationPredicate ) {
155
212
156
213
LettuceAssert .notNull (connectionSupplier , "Connection supplier must not be null" );
157
214
158
215
AtomicReference <Origin <T >> poolRef = new AtomicReference <>();
159
216
160
- SoftReferenceObjectPool <T > pool = new SoftReferenceObjectPool <T >(new RedisPooledObjectFactory <>(connectionSupplier )) {
217
+ SoftReferenceObjectPool <T > pool = new SoftReferenceObjectPool <T >(
218
+ new RedisPooledObjectFactory <>(connectionSupplier , validationPredicate )) {
161
219
162
220
private final Lock lock = new ReentrantLock ();
163
221
@@ -200,8 +258,11 @@ private static class RedisPooledObjectFactory<T extends StatefulConnection<?, ?>
200
258
201
259
private final Supplier <T > connectionSupplier ;
202
260
203
- RedisPooledObjectFactory (Supplier <T > connectionSupplier ) {
261
+ private final Predicate <T > validationPredicate ;
262
+
263
+ RedisPooledObjectFactory (Supplier <T > connectionSupplier , Predicate <T > validationPredicate ) {
204
264
this .connectionSupplier = connectionSupplier ;
265
+ this .validationPredicate = validationPredicate ;
205
266
}
206
267
207
268
@ Override
@@ -221,7 +282,7 @@ public PooledObject<T> wrap(T obj) {
221
282
222
283
@ Override
223
284
public boolean validateObject (PooledObject <T > p ) {
224
- return p .getObject (). isOpen ( );
285
+ return this . validationPredicate . test ( p .getObject ());
225
286
}
226
287
227
288
}
0 commit comments