84
84
* <li>{@link RedisSentinelConfiguration}</li>
85
85
* <li>{@link RedisClusterConfiguration}</li>
86
86
* </ul>
87
+ * <p>
88
+ * This connection factory must be {@link #afterPropertiesSet() initialized} prior to {@link #getConnection obtaining
89
+ * connections}.
87
90
*
88
91
* @author Costin Leau
89
92
* @author Jennifer Hickey
@@ -124,6 +127,9 @@ public class LettuceConnectionFactory
124
127
125
128
private @ Nullable ClusterCommandExecutor clusterCommandExecutor ;
126
129
130
+ private boolean initialized ;
131
+ private boolean destroyed ;
132
+
127
133
/**
128
134
* Constructs a new {@link LettuceConnectionFactory} instance with default settings.
129
135
*/
@@ -293,6 +299,8 @@ public void afterPropertiesSet() {
293
299
EXCEPTION_TRANSLATION );
294
300
}
295
301
302
+ this .initialized = true ;
303
+
296
304
if (getEagerInitialization () && getShareNativeConnection ()) {
297
305
initConnection ();
298
306
}
@@ -329,6 +337,8 @@ public void destroy() {
329
337
log .warn ("Cannot properly close cluster command executor" , ex );
330
338
}
331
339
}
340
+
341
+ this .destroyed = true ;
332
342
}
333
343
334
344
private void dispose (LettuceConnectionProvider connectionProvider ) {
@@ -351,6 +361,8 @@ private void dispose(LettuceConnectionProvider connectionProvider) {
351
361
*/
352
362
public RedisConnection getConnection () {
353
363
364
+ assertInitialized ();
365
+
354
366
if (isClusterAware ()) {
355
367
return getClusterConnection ();
356
368
}
@@ -368,6 +380,8 @@ public RedisConnection getConnection() {
368
380
@ Override
369
381
public RedisClusterConnection getClusterConnection () {
370
382
383
+ assertInitialized ();
384
+
371
385
if (!isClusterAware ()) {
372
386
throw new InvalidDataAccessApiUsageException ("Cluster is not configured!" );
373
387
}
@@ -437,6 +451,8 @@ protected LettuceClusterConnection doCreateLettuceClusterConnection(
437
451
@ Override
438
452
public LettuceReactiveRedisConnection getReactiveConnection () {
439
453
454
+ assertInitialized ();
455
+
440
456
if (isClusterAware ()) {
441
457
return getReactiveClusterConnection ();
442
458
}
@@ -453,6 +469,8 @@ public LettuceReactiveRedisConnection getReactiveConnection() {
453
469
@ Override
454
470
public LettuceReactiveRedisClusterConnection getReactiveClusterConnection () {
455
471
472
+ assertInitialized ();
473
+
456
474
if (!isClusterAware ()) {
457
475
throw new InvalidDataAccessApiUsageException ("Cluster is not configured!" );
458
476
}
@@ -481,6 +499,8 @@ public void initConnection() {
481
499
*/
482
500
public void resetConnection () {
483
501
502
+ assertInitialized ();
503
+
484
504
Optionals .toStream (Optional .ofNullable (connection ), Optional .ofNullable (reactiveConnection ))
485
505
.forEach (SharedConnection ::resetConnection );
486
506
@@ -496,6 +516,8 @@ public void resetConnection() {
496
516
*/
497
517
public void validateConnection () {
498
518
519
+ assertInitialized ();
520
+
499
521
getOrCreateSharedConnection ().validateConnection ();
500
522
getOrCreateSharedReactiveConnection ().validateConnection ();
501
523
}
@@ -801,6 +823,7 @@ public void setClientName(@Nullable String clientName) {
801
823
*/
802
824
@ Nullable
803
825
public AbstractRedisClient getNativeClient () {
826
+ assertInitialized ();
804
827
return this .client ;
805
828
}
806
829
@@ -1167,6 +1190,11 @@ private RedisURI getSentinelRedisURI() {
1167
1190
return redisUri ;
1168
1191
}
1169
1192
1193
+ private void assertInitialized () {
1194
+ Assert .state (this .initialized , "LettuceConnectionFactory was not initialized through afterPropertiesSet()" );
1195
+ Assert .state (!this .destroyed , "LettuceConnectionFactory was destroyed and cannot be used anymore" );
1196
+ }
1197
+
1170
1198
private static void applyToAll (RedisURI source , Consumer <RedisURI > action ) {
1171
1199
1172
1200
action .accept (source );
@@ -1214,6 +1242,9 @@ private void applyAuthentication(RedisURI.Builder builder) {
1214
1242
1215
1243
@ Override
1216
1244
public RedisSentinelConnection getSentinelConnection () {
1245
+
1246
+ assertInitialized ();
1247
+
1217
1248
return new LettuceSentinelConnection (connectionProvider );
1218
1249
}
1219
1250
0 commit comments