8.9.3 Providing Information to Choose a MySQL Server

If you create a Fabric connection without providing any
information about which data to access, the connection cannot
function. To access a database, you must provide the driver with
one of the following:

The name of a high-availability group
known by the MySQL Fabric instance to which you've connected.
In such a group, one server is the master (the primary) and
the others are slaves (secondaries).

A shard table, and optionally a
shard key, to guide Connector/J in
selecting a high-availability group.

One or more query tables to guide the
connector in selecting a server group. Query tables can
reference only a single shard mapping or the query is
rejected. A shard mapping can include several tables but they
must be sharded on the same index.

Providing the name of a high-availability group specifies that we
know exactly the set of database servers with which to interact.
We can do this in two ways.

The simplest method is to include the name of the server group
in the connection string. This is useful if a connection will
need to access data only in that server group. It is also
possible to set the name of the server group in this way
initially and to change it programatically later.

Providing shard information avoids the need
to choose a high-availability group manually and permits
Connector/J to do so based on information from the MySQL Fabric
server.

Shard tables and shard keys can also be given as connection properties if
desirable. Here we say that we want to access the
employees table. The driver chooses
a server group based on this shard table.

Alternatively, query tables can be
provided to the connection before creating a statement. The
following example sets up the connection to perform a join
between the employees and
departments tables. Details on how
to provide the shard key are given in the next step.

JDBC4FabricMySQLConnection conn;
// provide the set of query tables to the connection
conn.addQueryTable("departments");
conn.addQueryTable("employees");

In many cases, you want to work with different sets of data at
different times. You can specify the shard key to change the set of data to be accessible.