Documentation Home
MySQL Connector/J 8.0 Developer Guide
Related Documentation Download this Manual
PDF (US Ltr) - 0.5Mb
PDF (A4) - 0.5Mb
HTML Download (TGZ) - 125.2Kb
HTML Download (Zip) - 145.5Kb

10.1 Connection Compression Using X DevAPI

Staring form release 8.0.20, Connector/J supports data compression for X DevAPI connections, when working with MySQL Server 8.0.19 and later. General details about this feature can be found in Connection Compression with X Plugin. On how to configure connection compression support for Connector/J, see the descriptions for the connection properties xdevapi.compression and xdevapi.compression-algorithm in Section 6.3, “Configuration Properties”.

Out of all the compression algorithms now supported by MySQL 8.0 for X DevAPI connections, Connector/J provides out-of-the-box support for Deflate only; this is because none of the other compression algorithms (LZ4 and zstd for now) are natively supported by the existing JREs. To support those algorithms, the client application must provide implementations for the corresponding deflate and inflate operations in the form of an OutputStream and an InputStream object, respectively. The easiest way to accomplish this is by using a third-party library such as the Apache Commons Compress library, which supports LZ4 and zstd.

The connection option xdevapi.compression-algorithm allows users to configure Connector/J to use any compression algorithm that is supported by MySQL Server, as long as there is a Java implementation for that algorithm. The option takes a list of triplets separated by commas, and each triplet in turn contains the following elements, also separated by commas:

  • The compression algorithm name, indicated by the identifier used by the server (see Connection Compression with X Plugin).

  • A fully-qualified name of a class implementing the interface that will be used to inflate data compressed with the named algorithm.

  • A fully-qualified name of a class implementing the interface that will be used to deflate data using the named algorithm.

Here is an example that sets up the support for the algorithms lz4_message and zstd_stream using the Apache Commons Compress library:

String connStr = "jdbc:mysql://johndoe:secret@localhost:33060/mydb?" 
    + "xdevapi.compression-algorithm=" 
    + "lz4_message," // LZ4 triplet 
    + FramedLZ4CompressorInputStream.class.getName() + "," 
    + FramedLZ4CompressorOutputStream.class.getName() + "," 
    + "zstd_stream," // zstd triplet 
    + ZstdCompressorInputStream.class.getName() + "," 
    + ZstdCompressorOutputStream.class.getName(); 
SessionFactory sessFact = new SessionFactory(); 
Session sess = sessFact.getSession(connStr); 
Collection col = sess.getDefaultSchema().getCollection("myCollection"); 
// (...) 

With the connection string in the example, Connector/J negotiates a compression algorithm following the priority recommended by X DevAPI: trying first zstd, then LZ4, and finally Deflate. The final choice depends on what algorithms are enabled on the server. By default, because compression is not required, if the negotiation fails, the connection will not be compressed, but the client will still be able to communicate with the server; however, if the connection property xdevapi.compression is set to required, the connection attempt fails with an error if the negotiation for a compression algorithm fails.