0
# JDBC High Availability
1
2
Load balancing, replication, and failover support for high-availability MySQL deployments. These features enable applications to maintain connectivity and performance across multiple database servers.
3
4
## Capabilities
5
6
### Load-Balanced Connections
7
8
Load-balanced connection support for distributing queries across multiple MySQL servers.
9
10
```java { .api }
11
package com.mysql.cj.jdbc.ha;
12
13
public interface LoadBalancedConnection extends com.mysql.cj.jdbc.JdbcConnection {
14
// Host management
15
boolean addHost(String host) throws SQLException;
16
void removeHost(String host) throws SQLException;
17
void removeHostWhenNotInUse(String host) throws SQLException;
18
19
// Connection testing
20
void ping(boolean allConnections) throws SQLException;
21
}
22
23
public class LoadBalancedMySQLConnection extends MultiHostMySQLConnection
24
implements LoadBalancedConnection {
25
// Implementation of load-balanced connection
26
}
27
28
public class LoadBalancedConnectionProxy extends MultiHostConnectionProxy
29
implements InvocationHandler {
30
// Proxy for managing load-balanced connections
31
32
public LoadBalancedConnectionProxy(ConnectionUrl connectionUrl) throws SQLException;
33
34
// Host management
35
public synchronized void addHost(String host) throws SQLException;
36
public synchronized void removeHost(String host) throws SQLException;
37
public synchronized void removeHostWhenNotInUse(String host) throws SQLException;
38
39
// Connection balancing
40
public synchronized void ping(boolean allIsReadOnly) throws SQLException;
41
}
42
```
43
44
Usage:
45
46
```java
47
// URL format: jdbc:mysql:loadbalance://host1:port,host2:port,host3:port/database
48
String url = "jdbc:mysql:loadbalance://server1:3306,server2:3306,server3:3306/mydb" +
49
"?loadBalanceStrategy=random" +
50
"&loadBalanceAutoCommitStatementThreshold=5";
51
52
Connection conn = DriverManager.getConnection(url, "root", "password");
53
54
// Cast to LoadBalancedConnection for management
55
LoadBalancedConnection lbConn = conn.unwrap(LoadBalancedConnection.class);
56
57
// Add new host to rotation
58
lbConn.addHost("server4:3306");
59
60
// Remove host from rotation
61
lbConn.removeHost("server2:3306");
62
63
// Remove host when not in use (graceful removal)
64
lbConn.removeHostWhenNotInUse("server3:3306");
65
66
// Test all connections
67
lbConn.ping(false);
68
69
// Use connection normally
70
Statement stmt = conn.createStatement();
71
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
72
// Results come from load-balanced server
73
```
74
75
### Load Balance Strategies
76
77
Pluggable strategies for selecting which server to use for each connection.
78
79
```java { .api }
80
package com.mysql.cj.jdbc.ha;
81
82
public interface BalanceStrategy {
83
// Initialize strategy
84
void init(Connection conn, Properties props) throws SQLException;
85
86
// Destroy strategy
87
void destroy();
88
89
// Pick connection from available hosts
90
Connection pickConnection(
91
InvocationHandler proxy,
92
List<String> configuredHosts,
93
Map<String, JdbcConnection> liveConnections,
94
long[] responseTimes,
95
int numRetries
96
) throws SQLException;
97
}
98
99
public class RandomBalanceStrategy implements BalanceStrategy {
100
// Randomly selects from available hosts
101
public void init(Connection conn, Properties props) throws SQLException;
102
public void destroy();
103
public Connection pickConnection(
104
InvocationHandler proxy,
105
List<String> configuredHosts,
106
Map<String, JdbcConnection> liveConnections,
107
long[] responseTimes,
108
int numRetries
109
) throws SQLException;
110
}
111
112
public class BestResponseTimeBalanceStrategy implements BalanceStrategy {
113
// Selects host with best response time
114
public void init(Connection conn, Properties props) throws SQLException;
115
public void destroy();
116
public Connection pickConnection(
117
InvocationHandler proxy,
118
List<String> configuredHosts,
119
Map<String, JdbcConnection> liveConnections,
120
long[] responseTimes,
121
int numRetries
122
) throws SQLException;
123
}
124
125
public class SequentialBalanceStrategy implements BalanceStrategy {
126
// Selects hosts sequentially in order
127
public void init(Connection conn, Properties props) throws SQLException;
128
public void destroy();
129
public Connection pickConnection(
130
InvocationHandler proxy,
131
List<String> configuredHosts,
132
Map<String, JdbcConnection> liveConnections,
133
long[] responseTimes,
134
int numRetries
135
) throws SQLException;
136
}
137
138
public class ServerAffinityStrategy implements BalanceStrategy {
139
// Maintains affinity to specific servers
140
public void init(Connection conn, Properties props) throws SQLException;
141
public void destroy();
142
public Connection pickConnection(
143
InvocationHandler proxy,
144
List<String> configuredHosts,
145
Map<String, JdbcConnection> liveConnections,
146
long[] responseTimes,
147
int numRetries
148
) throws SQLException;
149
}
150
```
151
152
Usage:
153
154
```java
155
// Random strategy (default)
156
String url = "jdbc:mysql:loadbalance://server1:3306,server2:3306/mydb" +
157
"?loadBalanceStrategy=random";
158
159
// Best response time strategy
160
String url2 = "jdbc:mysql:loadbalance://server1:3306,server2:3306/mydb" +
161
"?loadBalanceStrategy=bestResponseTime";
162
163
// Sequential strategy
164
String url3 = "jdbc:mysql:loadbalance://server1:3306,server2:3306/mydb" +
165
"?loadBalanceStrategy=com.mysql.cj.jdbc.ha.SequentialBalanceStrategy";
166
167
// Server affinity strategy
168
String url4 = "jdbc:mysql:loadbalance://server1:3306,server2:3306/mydb" +
169
"?loadBalanceStrategy=serverAffinity" +
170
"&serverAffinityOrder=server2,server1";
171
172
Connection conn = DriverManager.getConnection(url, "root", "password");
173
```
174
175
### Load Balance Exception Checkers
176
177
Pluggable checkers to determine if exceptions should trigger failover.
178
179
```java { .api }
180
package com.mysql.cj.jdbc.ha;
181
182
public interface LoadBalanceExceptionChecker {
183
// Initialize checker
184
void init(Connection conn, Properties props) throws SQLException;
185
186
// Destroy checker
187
void destroy();
188
189
// Check if exception should trigger failover
190
boolean shouldExceptionTriggerFailover(SQLException ex);
191
}
192
193
public class StandardLoadBalanceExceptionChecker implements LoadBalanceExceptionChecker {
194
// Standard exception checker
195
public void init(Connection conn, Properties props) throws SQLException;
196
public void destroy();
197
public boolean shouldExceptionTriggerFailover(SQLException ex);
198
}
199
200
public class NdbLoadBalanceExceptionChecker implements LoadBalanceExceptionChecker {
201
// Exception checker for MySQL Cluster (NDB)
202
public void init(Connection conn, Properties props) throws SQLException;
203
public void destroy();
204
public boolean shouldExceptionTriggerFailover(SQLException ex);
205
}
206
```
207
208
Usage:
209
210
```java
211
// Use standard exception checker
212
String url = "jdbc:mysql:loadbalance://server1:3306,server2:3306/mydb" +
213
"?loadBalanceExceptionChecker=" +
214
"com.mysql.cj.jdbc.ha.StandardLoadBalanceExceptionChecker";
215
216
// Use NDB exception checker for MySQL Cluster
217
String url2 = "jdbc:mysql:loadbalance://server1:3306,server2:3306/mydb" +
218
"?loadBalanceExceptionChecker=" +
219
"com.mysql.cj.jdbc.ha.NdbLoadBalanceExceptionChecker";
220
221
Connection conn = DriverManager.getConnection(url, "root", "password");
222
```
223
224
### Load Balance Auto-Commit Interceptor
225
226
Interceptor for handling auto-commit in load-balanced environments.
227
228
```java { .api }
229
package com.mysql.cj.jdbc.ha;
230
231
public class LoadBalancedAutoCommitInterceptor implements QueryInterceptor {
232
// Manages transaction boundaries in load-balanced connections
233
// Ensures consistency when autoCommit=true
234
235
public QueryInterceptor init(MysqlConnection conn, Properties props, Log log);
236
237
public <T extends Resultset> T preProcess(Supplier<String> sql, Query interceptedQuery);
238
239
public boolean executeTopLevelOnly();
240
241
public void destroy();
242
243
public <T extends Resultset> T postProcess(
244
Supplier<String> sql,
245
Query interceptedQuery,
246
T originalResultSet,
247
ServerSession serverSession
248
);
249
}
250
```
251
252
Usage:
253
254
```java
255
// Automatically enabled for load-balanced connections
256
// Configure via URL parameters
257
String url = "jdbc:mysql:loadbalance://server1:3306,server2:3306/mydb" +
258
"?loadBalanceAutoCommitStatementThreshold=5" +
259
"?loadBalanceAutoCommitStatementRegex=^SELECT.*";
260
261
Connection conn = DriverManager.getConnection(url, "root", "password");
262
```
263
264
### Replication Connections
265
266
Replication-aware connections that route reads to replicas and writes to source.
267
268
```java { .api }
269
package com.mysql.cj.jdbc.ha;
270
271
public interface ReplicationConnection extends com.mysql.cj.jdbc.JdbcConnection {
272
// Get connection group ID
273
long getConnectionGroupId();
274
275
// Get current active connection
276
JdbcConnection getCurrentConnection();
277
278
// Get source connection (for writes)
279
JdbcConnection getSourceConnection();
280
281
// Promote replica to source
282
void promoteReplicaToSource(String host) throws SQLException;
283
284
// Remove source from pool
285
void removeSourceHost(String host) throws SQLException;
286
void removeSourceHost(String host, boolean waitUntilNotInUse) throws SQLException;
287
288
// Check if host is a source
289
boolean isHostSource(String host);
290
291
// Get replica connection (for reads)
292
JdbcConnection getReplicaConnection();
293
294
// Add replica to pool
295
void addReplicaHost(String host) throws SQLException;
296
297
// Remove replica from pool
298
void removeReplica(String host) throws SQLException;
299
void removeReplica(String host, boolean closeGently) throws SQLException;
300
301
// Check if host is a replica
302
boolean isHostReplica(String host);
303
}
304
305
public class ReplicationMySQLConnection extends MultiHostMySQLConnection
306
implements ReplicationConnection {
307
// Implementation of replication connection
308
}
309
310
public class ReplicationConnectionProxy extends MultiHostConnectionProxy
311
implements InvocationHandler {
312
// Proxy for managing replication connections
313
314
public ReplicationConnectionProxy(ConnectionUrl connectionUrl) throws SQLException;
315
316
// Host management
317
public synchronized void promoteReplicaToSource(String host) throws SQLException;
318
public synchronized void removeReplica(String host) throws SQLException;
319
public synchronized void removeSourceHost(String host) throws SQLException;
320
public synchronized void addReplicaHost(String host) throws SQLException;
321
public synchronized void addSourceHost(String host) throws SQLException;
322
}
323
```
324
325
Usage:
326
327
```java
328
// URL format: jdbc:mysql:replication://source1:3306,replica1:3306,replica2:3306/database
329
String url = "jdbc:mysql:replication://" +
330
"source:3306,replica1:3306,replica2:3306/mydb" +
331
"?loadBalanceStrategy=random";
332
333
Connection conn = DriverManager.getConnection(url, "root", "password");
334
335
// Cast to ReplicationConnection for management
336
ReplicationConnection replConn = conn.unwrap(ReplicationConnection.class);
337
338
// Default: routes writes to source, reads to replicas
339
conn.setAutoCommit(false);
340
Statement stmt = conn.createStatement();
341
342
// This goes to source
343
stmt.executeUpdate("INSERT INTO users (name) VALUES ('Alice')");
344
345
// Set read-only to route to replicas
346
conn.setReadOnly(true);
347
348
// This goes to replica
349
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
350
while (rs.next()) {
351
System.out.println(rs.getString("name"));
352
}
353
354
// Promote replica to source (e.g., after source failure)
355
replConn.promoteReplicaToSource("replica1:3306");
356
357
// Add new replica
358
replConn.addReplicaHost("replica3:3306");
359
360
// Remove replica
361
replConn.removeReplica("replica2:3306");
362
363
conn.commit();
364
conn.close();
365
```
366
367
### Replication Connection Groups
368
369
Management of replication connection groups for dynamic reconfiguration.
370
371
```java { .api }
372
package com.mysql.cj.jdbc.ha;
373
374
public class ReplicationConnectionGroup {
375
// Constructor
376
public ReplicationConnectionGroup(String groupName);
377
378
// Get group name
379
public String getGroupName();
380
381
// Get source hosts
382
public Collection<String> getSourceHosts();
383
384
// Get replica hosts
385
public Collection<String> getReplicaHosts();
386
387
// Add source host
388
public void addSourceHost(String hostPortPair);
389
390
// Add replica host
391
public void addReplicaHost(String hostPortPair);
392
393
// Remove source host
394
public void removeSourceHost(String hostPortPair);
395
396
// Remove replica host
397
public void removeReplicaHost(String hostPortPair);
398
399
// Promote replica to source
400
public void promoteReplica(String hostPortPair);
401
402
// Get active connections count
403
public int getConnectionCount();
404
405
// Get active connections count for source
406
public long getConnectionCountWithHostAsSource(String hostPortPair);
407
408
// Get active connections count for replica
409
public long getConnectionCountWithHostAsReplica(String hostPortPair);
410
411
// Get number of replica hosts
412
public int getReplicaHostsSize();
413
414
// Get number of source hosts
415
public int getSourceHostsSize();
416
}
417
418
public class ReplicationConnectionGroupManager {
419
// Get connection group by name
420
public static ReplicationConnectionGroup getConnectionGroup(String groupName);
421
422
// Get all connection groups
423
public static Collection<ReplicationConnectionGroup> getConnectionGroups();
424
425
// Add replica host to group
426
public static void addReplicaHost(String groupName, String hostPortPair)
427
throws SQLException;
428
429
// Add source host to group
430
public static void addSourceHost(String groupName, String hostPortPair)
431
throws SQLException;
432
433
// Remove replica host from group
434
public static void removeReplicaHost(String groupName, String hostPortPair)
435
throws SQLException;
436
437
// Remove replica host from group and wait
438
public static void removeReplicaHost(String groupName, String hostPortPair,
439
boolean closeGently) throws SQLException;
440
441
// Promote replica to source
442
public static void promoteReplicaToSource(String groupName, String hostPortPair)
443
throws SQLException;
444
445
// Remove source host from group
446
public static void removeSourceHost(String groupName, String hostPortPair)
447
throws SQLException;
448
449
// Remove source host from group and wait
450
public static void removeSourceHost(String groupName, String hostPortPair,
451
boolean closeGently) throws SQLException;
452
453
// Get number of source hosts
454
public static int getSourceHostsCount(String groupName);
455
456
// Get number of replica hosts
457
public static int getReplicaHostsCount(String groupName);
458
459
// Get number of active connections
460
public static long getActiveConnectionCount(String groupName);
461
462
// Get number of active source connections
463
public static long getActiveSourceConnectionCount(String groupName, String hostPortPair);
464
465
// Get number of active replica connections
466
public static long getActiveReplicaConnectionCount(String groupName, String hostPortPair);
467
}
468
```
469
470
Usage:
471
472
```java
473
// Create connection with group name
474
String url = "jdbc:mysql:replication://source:3306,replica1:3306/mydb" +
475
"?replicationConnectionGroup=mygroup";
476
477
Connection conn = DriverManager.getConnection(url, "root", "password");
478
479
// Manage group dynamically
480
ReplicationConnectionGroup group =
481
ReplicationConnectionGroupManager.getConnectionGroup("mygroup");
482
483
// Add new replica
484
ReplicationConnectionGroupManager.addReplicaHost("mygroup", "replica2:3306");
485
486
// Promote replica to source (e.g., during failover)
487
ReplicationConnectionGroupManager.promoteReplicaToSource("mygroup", "replica1:3306");
488
489
// Remove failed source
490
ReplicationConnectionGroupManager.removeSourceHost("mygroup", "source:3306", true);
491
492
// Get connection counts
493
long sourceConnections =
494
ReplicationConnectionGroupManager.getActiveSourceConnectionCount(
495
"mygroup", "replica1:3306"
496
);
497
System.out.println("Active source connections: " + sourceConnections);
498
```
499
500
### Connection Groups
501
502
Generic connection group management for load-balanced connections.
503
504
```java { .api }
505
package com.mysql.cj.jdbc.ha;
506
507
public class ConnectionGroup {
508
// Constructor
509
public ConnectionGroup(String groupName);
510
511
// Get group name
512
public String getGroupName();
513
514
// Get initial hosts
515
public Collection<String> getInitialHosts();
516
517
// Get initial hosts size
518
public int getInitialHostsSize();
519
520
// Get active hosts
521
public Collection<String> getActiveHosts();
522
523
// Get active hosts size
524
public int getActiveHostsSize();
525
526
// Get closed hosts
527
public Collection<String> getClosedHosts();
528
529
// Add host to group
530
public void addHost(String hostPortPair, boolean forExisting);
531
532
// Remove host from group
533
public void removeHost(String hostPortPair);
534
535
// Get connection count for host
536
public long getConnectionCount(String hostPortPair);
537
538
// Get total connection count
539
public long getTotalConnectionCount();
540
}
541
542
public class ConnectionGroupManager {
543
// Register connection group
544
public static synchronized void registerConnectionGroup(String groupName,
545
ConnectionGroup group);
546
547
// Get connection group
548
public static ConnectionGroup getConnectionGroup(String groupName);
549
550
// Get all connection groups
551
public static Collection<ConnectionGroup> getConnectionGroups();
552
553
// Add host to group
554
public static void addHost(String groupName, String hostPortPair, boolean forExisting);
555
556
// Remove host from group
557
public static void removeHost(String groupName, String hostPortPair) throws SQLException;
558
559
// Get connection count for host
560
public static long getConnectionCountForHost(String groupName, String hostPortPair);
561
562
// Get total connection count for group
563
public static long getActiveConnectionCount(String groupName);
564
565
// Get number of active hosts
566
public static int getActiveHostsCount(String groupName);
567
}
568
```
569
570
Usage:
571
572
```java
573
// Create load-balanced connection with group
574
String url = "jdbc:mysql:loadbalance://server1:3306,server2:3306/mydb" +
575
"?loadBalanceConnectionGroup=lbgroup";
576
577
Connection conn = DriverManager.getConnection(url, "root", "password");
578
579
// Manage connection group
580
ConnectionGroup group = ConnectionGroupManager.getConnectionGroup("lbgroup");
581
582
// Add host to existing connections
583
ConnectionGroupManager.addHost("lbgroup", "server3:3306", true);
584
585
// Remove host from group
586
ConnectionGroupManager.removeHost("lbgroup", "server2:3306");
587
588
// Get statistics
589
long hostConnections =
590
ConnectionGroupManager.getConnectionCountForHost("lbgroup", "server1:3306");
591
long totalConnections = ConnectionGroupManager.getActiveConnectionCount("lbgroup");
592
int activeHosts = ConnectionGroupManager.getActiveHostsCount("lbgroup");
593
594
System.out.println("Connections to server1: " + hostConnections);
595
System.out.println("Total connections: " + totalConnections);
596
System.out.println("Active hosts: " + activeHosts);
597
```
598
599
### Failover Connections
600
601
Automatic failover support for connection reliability.
602
603
```java { .api }
604
package com.mysql.cj.jdbc.ha;
605
606
public class FailoverConnectionProxy extends MultiHostConnectionProxy
607
implements InvocationHandler {
608
// Proxy for managing failover connections
609
610
public FailoverConnectionProxy(ConnectionUrl connectionUrl) throws SQLException;
611
612
// Failover occurs automatically on connection failure
613
// Tries each configured host in sequence
614
// Transparently reconnects when needed
615
}
616
```
617
618
Usage:
619
620
```java
621
// URL format: jdbc:mysql://primary:3306,secondary:3306/database
622
String url = "jdbc:mysql://primary:3306,secondary:3306/mydb" +
623
"?autoReconnect=true" +
624
"&failOverReadOnly=false";
625
626
Connection conn = DriverManager.getConnection(url, "root", "password");
627
628
// Failover happens automatically on connection failure
629
try {
630
Statement stmt = conn.createStatement();
631
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
632
// If primary fails, automatically fails over to secondary
633
} catch (SQLException e) {
634
// Handle persistent failure
635
}
636
```
637
638
### Multi-Host Base Classes
639
640
Base classes for multi-host connection implementations.
641
642
```java { .api }
643
package com.mysql.cj.jdbc.ha;
644
645
public abstract class MultiHostConnectionProxy implements InvocationHandler {
646
// Base proxy class for multi-host connections
647
648
protected ConnectionUrl connectionUrl;
649
protected List<String> hostsList;
650
651
public MultiHostConnectionProxy(ConnectionUrl connectionUrl) throws SQLException;
652
653
// Invoke method on current connection
654
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable;
655
656
// Execute on current connection
657
protected synchronized Object invokeMore(Object proxy, Method method, Object[] args)
658
throws Throwable;
659
660
// Deal with invocation exceptions
661
protected void dealWithInvocationException(InvocationTargetException e) throws Throwable;
662
}
663
664
public class MultiHostMySQLConnection implements JdbcConnection {
665
// Base connection class for multi-host connections
666
667
protected MultiHostConnectionProxy thisAsProxy;
668
669
public MultiHostMySQLConnection(MultiHostConnectionProxy proxy);
670
671
// Implements all JdbcConnection methods by delegating to proxy
672
// Provides common functionality for load-balanced and replication connections
673
}
674
```
675
676
### JMX Management Beans
677
678
JMX MBeans for monitoring and managing connection groups.
679
680
```java { .api }
681
package com.mysql.cj.jdbc.jmx;
682
683
public interface LoadBalanceConnectionGroupManagerMBean {
684
// JMX interface for load balance connection group management
685
686
// Get active host count
687
int getActiveHostCount(String groupName);
688
689
// Get total connection count
690
long getTotalConnectionCount(String groupName);
691
692
// Get connection count for specific host
693
long getActiveConnectionCount(String groupName, String host);
694
695
// Add host to group
696
void addHost(String groupName, String host, boolean forExisting);
697
698
// Remove host from group
699
void removeHost(String groupName, String host) throws SQLException;
700
}
701
702
public class LoadBalanceConnectionGroupManager implements LoadBalanceConnectionGroupManagerMBean {
703
// JMX implementation for load balance connection group management
704
705
public int getActiveHostCount(String groupName);
706
public long getTotalConnectionCount(String groupName);
707
public long getActiveConnectionCount(String groupName, String host);
708
public void addHost(String groupName, String host, boolean forExisting);
709
public void removeHost(String groupName, String host) throws SQLException;
710
}
711
712
public interface ReplicationGroupManagerMBean {
713
// JMX interface for replication group management
714
715
// Add replica host
716
void addReplicaHost(String groupName, String host) throws SQLException;
717
718
// Remove replica host
719
void removeReplicaHost(String groupName, String host) throws SQLException;
720
721
// Promote replica to source
722
void promoteReplicaToSource(String groupName, String host) throws SQLException;
723
724
// Remove source host
725
void removeSourceHost(String groupName, String host) throws SQLException;
726
727
// Get source host count
728
int getSourceHostsCount(String groupName);
729
730
// Get replica host count
731
int getReplicaHostsCount(String groupName);
732
733
// Get active source connection count
734
long getActiveSourceConnectionCount(String groupName, String host);
735
736
// Get active replica connection count
737
long getActiveReplicaConnectionCount(String groupName, String host);
738
739
// Get total active connection count
740
long getActiveConnectionCount(String groupName);
741
}
742
743
public class ReplicationGroupManager implements ReplicationGroupManagerMBean {
744
// JMX implementation for replication group management
745
746
public void addReplicaHost(String groupName, String host) throws SQLException;
747
public void removeReplicaHost(String groupName, String host) throws SQLException;
748
public void promoteReplicaToSource(String groupName, String host) throws SQLException;
749
public void removeSourceHost(String groupName, String host) throws SQLException;
750
public int getSourceHostsCount(String groupName);
751
public int getReplicaHostsCount(String groupName);
752
public long getActiveSourceConnectionCount(String groupName, String host);
753
public long getActiveReplicaConnectionCount(String groupName, String host);
754
public long getActiveConnectionCount(String groupName);
755
}
756
```
757
758
Usage with JMX:
759
760
```java
761
// Register MBeans with JMX server
762
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
763
764
// Register load balance manager
765
LoadBalanceConnectionGroupManager lbManager = new LoadBalanceConnectionGroupManager();
766
ObjectName lbName = new ObjectName(
767
"com.mysql.cj.jdbc.jmx:type=LoadBalanceConnectionGroupManager"
768
);
769
mbs.registerMBean(lbManager, lbName);
770
771
// Register replication manager
772
ReplicationGroupManager replManager = new ReplicationGroupManager();
773
ObjectName replName = new ObjectName(
774
"com.mysql.cj.jdbc.jmx:type=ReplicationGroupManager"
775
);
776
mbs.registerMBean(replManager, replName);
777
778
// Now can manage via JMX console or programmatically
779
// Example: Add host via JMX
780
mbs.invoke(
781
lbName,
782
"addHost",
783
new Object[]{"mygroup", "newserver:3306", true},
784
new String[]{"java.lang.String", "java.lang.String", "boolean"}
785
);
786
```
787