Connector/J can receive information on
client session state
changes tracked by the server if the tracking has been
enabled on the server. The reception of the information is
enabled by setting the Connector/J connection property
trackSessionState
to true
(default value is false
for the property).
When the function is enabled, information on session state
changes received from the server are stored inside the
SessionStateChanges
object, accessible
through a ServerSessionStateController
and
its getSessionStateChanges()
method:
ServerSessionStateChanges ssc =
MysqlConnection.getServerSessionStateController().getSessionStateChanges();
In SessionStateChanges
is a list of
SessoinStateChange
objects, accessible by the
getSessionStateChangesList()
method:
List<SessionStateChange> sscList = ssc.getSessionStateChangesList();
Each SessionStateChange
has the fields
type
and values
,
accessible by the getType()
and
getValues()
methods. The types and their
corresponding values are described below:
Table 6.23 SessionStateChange Type and Values
Type | Number of Values in the value List | Values |
---|---|---|
SESSION_TRACK_SYSTEM_VARIABLES |
2 | The name of the changed system variable and its new value |
SESSION_TRACK_SCHEMA |
1 | The new schema name |
SESSION_TRACK_STATE_CHANGE |
1 | "1" or "0" |
SESSION_TRACK_GTIDS |
1 | List of GTIDs as reported by server |
SESSION_TRACK_TRANSACTION_CHARACTERISTICS |
1 | Transaction characteristics statement |
SESSION_TRACK_TRANSACTION_STATE |
1 | Transaction state record |
Connector/J receives changes only from the most recent OK packet
sent by the server. With
getSessionStateChanges()
, some changes
returned by the intermediate queries issued by Connector/J could
be missed. However, the session state change information can
also be received using a
SessionStateChangesListener
, which has to be
registered with a
ServerSessionStateController
using the
addSessionStateChangesListener()
method. The
following example implements
SessionStateChangesListener
in a class, which
also provides a method to print the change information:
class SSCListener implements SessionStateChangesListener {
ServerSessionStateChanges changes = null;
public void handleSessionStateChanges(ServerSessionStateChanges ch) {
this.changes = ch;
for (SessionStateChange change : ch.getSessionStateChangesList()) {
printChange(change);
}
}
private void printChange(SessionStateChange change) {
System.out.print(change.getType() + " == > ");
int pos = 0;
if (change.getType() == ServerSessionStateController.SESSION_TRACK_SYSTEM_VARIABLES) {
// There are two values with this change type, the system variable name and its new value
System.out.print(change.getValues().get(pos++) + "=");
}
System.out.println(change.getValues().get(pos));
}
}
SessionStateChangesListener listener = new SSCListener();
MysqlConnection.getServerSessionStateController().addSessionStateChangesListener(listener);
With a registered
SessionStateChangesListener
, users have
access to all intermediate results, though the listener might
slow down the delivery of query results. That is because the
listener is invoked immediately after the OK packet is consumed
by Connector/J, before the ResultSet
is
constructed.