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.
The following discussion describes both ways of providing
information. In the examples,
the Fabric connection object, created as shown in
Section 8.9.2, “Loading the Driver and Requesting a Fabric Connection”.
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 needs 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.
// provide the server group as a connection property Connection conn = DriverManager.getConnection( "jdbc:mysql:fabric://fabrichost:32274/database?fabricServerGroup=myGroup");
If we connect without specifying a server group, or want to change it later, we can use the
JDBC4FabricMySQLConnectioninterface to set the
server group name.
JDBC4FabricMySQLConnection conn; // connection initialization here conn.setServerGroupName("myGroup");
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
employeestable. The driver chooses a server group based on this shard table.Note
The format of the shard table is usually given as
// provide the shard table as a connection property Connection conn = DriverManager.getConnection( "jdbc:mysql:fabric://fabrichost:32274/database?fabricShardTable=employees.employees");
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
departmentstables. 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.
JDBC4FabricMySQLConnection conn; // connection initialization here conn.setShardKey("40"); // work with data related to shard key = 40
In summary, it is necessary to provide the name of a server group or a shard table and possibly shard key to access a database server.