@@ -71,7 +71,6 @@ public final class PostgresqlConnectionConfiguration {
71
71
72
72
private final String schema ;
73
73
74
-
75
74
private final String username ;
76
75
77
76
private final SSLConfig sslConfig ;
@@ -221,10 +220,10 @@ public static final class Builder {
221
220
private boolean forceBinary = false ;
222
221
223
222
@ Nullable
224
- private MultipleHostsConfiguration multipleHostsConfiguration ;
223
+ private MultipleHostsConfiguration . Builder multipleHostsConfiguration ;
225
224
226
225
@ Nullable
227
- private SingleHostConfiguration singleHostConfiguration ;
226
+ private SingleHostConfiguration . Builder singleHostConfiguration ;
228
227
229
228
private Map <String , String > options ;
230
229
@@ -287,21 +286,25 @@ public Builder autodetectExtensions(boolean autodetectExtensions) {
287
286
* @return a configured {@link PostgresqlConnectionConfiguration}
288
287
*/
289
288
public PostgresqlConnectionConfiguration build () {
290
-
291
- if (this .singleHostConfiguration != null && this .singleHostConfiguration .getHost () == null && this .singleHostConfiguration .getSocket () == null ) {
292
- throw new IllegalArgumentException ("host or socket must not be null" );
289
+ SingleHostConfiguration singleHostConfiguration = this .singleHostConfiguration != null
290
+ ? this .singleHostConfiguration .build ()
291
+ : null ;
292
+ MultipleHostsConfiguration multipleHostsConfiguration = this .multipleHostsConfiguration != null
293
+ ? this .multipleHostsConfiguration .build ()
294
+ : null ;
295
+ if (singleHostConfiguration == null && multipleHostsConfiguration == null ) {
296
+ throw new IllegalArgumentException ("Either multiple hosts configuration or single host configuration should be provided" );
293
297
}
294
-
295
- if (this .singleHostConfiguration != null && this .singleHostConfiguration .getHost () != null && this .singleHostConfiguration .getSocket () != null ) {
296
- throw new IllegalArgumentException ("Connection must be configured for either host/port or socket usage but not both" );
298
+ if (singleHostConfiguration != null && multipleHostsConfiguration != null ) {
299
+ throw new IllegalArgumentException ("Either multiple hosts configuration or single host configuration should be provided" );
297
300
}
298
301
299
302
if (this .username == null ) {
300
303
throw new IllegalArgumentException ("username must not be null" );
301
304
}
302
305
303
306
return new PostgresqlConnectionConfiguration (this .applicationName , this .autodetectExtensions , this .connectTimeout , this .database , this .extensions , this .forceBinary ,
304
- this .options , this .password , this .schema , this .username , this .createSslConfig (), this . singleHostConfiguration , this . multipleHostsConfiguration );
307
+ this .options , this .password , this .schema , this .username , this .createSslConfig (), singleHostConfiguration , multipleHostsConfiguration );
305
308
}
306
309
307
310
/**
@@ -377,10 +380,9 @@ public Builder forceBinary(boolean forceBinary) {
377
380
public Builder host (String host ) {
378
381
Assert .requireNonNull (host , "host must not be null" );
379
382
if (this .singleHostConfiguration == null ) {
380
- this .singleHostConfiguration = new SingleHostConfiguration (host , DEFAULT_PORT , null );
381
- } else {
382
- this .singleHostConfiguration = new SingleHostConfiguration (host , this .singleHostConfiguration .getPort (), this .singleHostConfiguration .getSocket ());
383
+ this .singleHostConfiguration = SingleHostConfiguration .builder ();
383
384
}
385
+ this .singleHostConfiguration .host (host );
384
386
return this ;
385
387
}
386
388
@@ -418,11 +420,6 @@ public Builder password(@Nullable CharSequence password) {
418
420
return this ;
419
421
}
420
422
421
- public Builder multipleHostsConfiguration (MultipleHostsConfiguration multipleHostsConfiguration ) {
422
- this .multipleHostsConfiguration = Assert .requireNonNull (multipleHostsConfiguration , "multipleHostsConfiguration must not be null" );
423
- return this ;
424
- }
425
-
426
423
/**
427
424
* Configure the schema.
428
425
*
@@ -442,10 +439,9 @@ public Builder schema(@Nullable String schema) {
442
439
*/
443
440
public Builder port (int port ) {
444
441
if (this .singleHostConfiguration == null ) {
445
- this .singleHostConfiguration = new SingleHostConfiguration (null , port , null );
446
- } else {
447
- this .singleHostConfiguration = new SingleHostConfiguration (this .singleHostConfiguration .getHost (), port , this .singleHostConfiguration .getSocket ());
442
+ this .singleHostConfiguration = SingleHostConfiguration .builder ();
448
443
}
444
+ this .singleHostConfiguration .port (port );
449
445
return this ;
450
446
}
451
447
@@ -550,17 +546,94 @@ public Builder username(String username) {
550
546
*/
551
547
public Builder socket (String socket ) {
552
548
Assert .requireNonNull (socket , "host must not be null" );
553
-
554
549
if (this .singleHostConfiguration == null ) {
555
- this .singleHostConfiguration = new SingleHostConfiguration (null , DEFAULT_PORT , socket );
556
- } else {
557
- this .singleHostConfiguration = new SingleHostConfiguration (this .singleHostConfiguration .getHost (), this .singleHostConfiguration .getPort (), socket );
550
+ this .singleHostConfiguration = SingleHostConfiguration .builder ();
558
551
}
552
+ this .singleHostConfiguration .socket (socket );
559
553
560
554
sslMode (SSLMode .DISABLE );
561
555
return this ;
562
556
}
563
557
558
+ /**
559
+ * Allows opening connections to only servers with required state, the allowed values are any, master, slave, secondary, preferSlave and preferSecondary.
560
+ * The master/secondary distinction is currently done by observing if the server allows writes.
561
+ * The value preferSecondary tries to connect to secondary if any are available, otherwise allows falls back to connecting also to master.
562
+ * Default value is any.
563
+ *
564
+ * @param targetServerType target server type
565
+ * @return this {@link Builder}
566
+ * @throws IllegalArgumentException if {@code targetServerType} is {@code null}
567
+ */
568
+ public Builder targetServerType (TargetServerType targetServerType ) {
569
+ if (this .multipleHostsConfiguration == null ) {
570
+ this .multipleHostsConfiguration = MultipleHostsConfiguration .builder ();
571
+ }
572
+ this .multipleHostsConfiguration .targetServerType (targetServerType );
573
+ return this ;
574
+ }
575
+
576
+ /**
577
+ * Controls how long in seconds the knowledge about a host state is cached connection factory. The default value is 10000 milliseconds.
578
+ *
579
+ * @param hostRecheckTime host recheck time in milliseconds
580
+ * @return this {@link Builder}
581
+ */
582
+ public Builder hostRecheckTime (int hostRecheckTime ) {
583
+ if (this .multipleHostsConfiguration == null ) {
584
+ this .multipleHostsConfiguration = MultipleHostsConfiguration .builder ();
585
+ }
586
+ this .multipleHostsConfiguration .hostRecheckTime (hostRecheckTime );
587
+ return this ;
588
+ }
589
+
590
+ /**
591
+ * In default mode (disabled) hosts are connected in the given order. If enabled hosts are chosen randomly from the set of suitable candidates.
592
+ *
593
+ * @param loadBalanceHosts is load balance mode enabled
594
+ * @return this {@link Builder}
595
+ */
596
+ public Builder loadBalanceHosts (boolean loadBalanceHosts ) {
597
+ if (this .multipleHostsConfiguration == null ) {
598
+ this .multipleHostsConfiguration = MultipleHostsConfiguration .builder ();
599
+ }
600
+ this .multipleHostsConfiguration .loadBalanceHosts (loadBalanceHosts );
601
+ return this ;
602
+ }
603
+
604
+ /**
605
+ * Add host with default port to hosts list.
606
+ *
607
+ * @param host the host
608
+ * @return this {@link Builder}
609
+ * @throws IllegalArgumentException if {@code host} is {@code null}
610
+ */
611
+ public Builder addHost (String host ) {
612
+ Assert .requireNonNull (host , "host must not be null" );
613
+ if (this .multipleHostsConfiguration == null ) {
614
+ this .multipleHostsConfiguration = MultipleHostsConfiguration .builder ();
615
+ }
616
+ this .multipleHostsConfiguration .addHost (host );
617
+ return this ;
618
+ }
619
+
620
+ /**
621
+ * Add host to hosts list.
622
+ *
623
+ * @param host the host
624
+ * @param port the port
625
+ * @return this {@link Builder}
626
+ * @throws IllegalArgumentException if {@code host} is {@code null}
627
+ */
628
+ public Builder addHost (String host , int port ) {
629
+ Assert .requireNonNull (host , "host must not be null" );
630
+ if (this .multipleHostsConfiguration == null ) {
631
+ this .multipleHostsConfiguration = MultipleHostsConfiguration .builder ();
632
+ }
633
+ this .multipleHostsConfiguration .addHost (host , port );
634
+ return this ;
635
+ }
636
+
564
637
@ Override
565
638
public String toString () {
566
639
return "Builder{" +
0 commit comments