@@ -132,6 +132,7 @@ public X509Certificate[] getAcceptedIssuers() {
132
132
133
133
private GtidSet gtidSet ;
134
134
private final Object gtidSetAccessLock = new Object ();
135
+ private boolean gtidSetFallbackToPurged ;
135
136
136
137
private EventDeserializer eventDeserializer = new EventDeserializer ();
137
138
@@ -298,12 +299,15 @@ public String getGtidSet() {
298
299
299
300
/**
300
301
* @param gtidSet GTID set (can be an empty string).
301
- * <p>NOTE #1: Any value but null will switch BinaryLogClient into a GTID mode (in which case GTID set will be
302
- * updated with each incoming GTID event) as well as set binlogFilename to "" (empty string) (meaning
303
- * BinaryLogClient will request events "outside of the set" <u>starting from the oldest known binlog</u>).
302
+ * <p>NOTE #1: Any value but null will switch BinaryLogClient into a GTID mode (this will also set binlogFilename
303
+ * to "" (provided it's null) forcing MySQL to send events starting from the oldest known binlog (keep in mind
304
+ * that connection will fail if gtid_purged is anything but empty (unless
305
+ * {@link #setGtidSetFallbackToPurged(boolean)} is set to true))).
304
306
* <p>NOTE #2: {@link #setBinlogFilename(String)} and {@link #setBinlogPosition(long)} can be used to specify the
305
307
* exact position from which MySQL server should start streaming events (taking into account GTID set).
308
+ * <p>NOTE #3: GTID set is automatically updated with each incoming GTID event (provided GTID mode is on).
306
309
* @see #getGtidSet()
310
+ * @see #setGtidSetFallbackToPurged(boolean)
307
311
*/
308
312
public void setGtidSet (String gtidSet ) {
309
313
if (gtidSet != null && this .binlogFilename == null ) {
@@ -314,6 +318,21 @@ public void setGtidSet(String gtidSet) {
314
318
}
315
319
}
316
320
321
+ /**
322
+ * @see #setGtidSetFallbackToPurged(boolean)
323
+ */
324
+ public boolean isGtidSetFallbackToPurged () {
325
+ return gtidSetFallbackToPurged ;
326
+ }
327
+
328
+ /**
329
+ * @param gtidSetFallbackToPurged true if gtid_purged should be used as a fallback when gtidSet is set to "" and
330
+ * MySQL server has purged some of the binary logs, false otherwise (default).
331
+ */
332
+ public void setGtidSetFallbackToPurged (boolean gtidSetFallbackToPurged ) {
333
+ this .gtidSetFallbackToPurged = gtidSetFallbackToPurged ;
334
+ }
335
+
317
336
/**
318
337
* @return true if "keep alive" thread should be automatically started (default), false otherwise.
319
338
* @see #setKeepAlive(boolean)
@@ -474,6 +493,13 @@ public void connect() throws IOException {
474
493
GreetingPacket greetingPacket = receiveGreeting ();
475
494
authenticate (greetingPacket );
476
495
connectionId = greetingPacket .getThreadId ();
496
+ if ("" .equals (binlogFilename )) {
497
+ synchronized (gtidSetAccessLock ) {
498
+ if (gtidSet != null && "" .equals (gtidSet .toString ()) && gtidSetFallbackToPurged ) {
499
+ gtidSet = new GtidSet (fetchGtidPurged ());
500
+ }
501
+ }
502
+ }
477
503
if (binlogFilename == null ) {
478
504
fetchBinlogFilenameAndPosition ();
479
505
}
@@ -800,6 +826,15 @@ public boolean isConnected() {
800
826
return connected ;
801
827
}
802
828
829
+ private String fetchGtidPurged () throws IOException {
830
+ channel .write (new QueryCommand ("show global variables like 'gtid_purged'" ));
831
+ ResultSetRowPacket [] resultSet = readResultSet ();
832
+ if (resultSet .length != 0 ) {
833
+ return resultSet [0 ].getValue (1 ).toUpperCase ();
834
+ }
835
+ return "" ;
836
+ }
837
+
803
838
private void fetchBinlogFilenameAndPosition () throws IOException {
804
839
ResultSetRowPacket [] resultSet ;
805
840
channel .write (new QueryCommand ("show master status" ));
0 commit comments