What Is a SyncResolver? JDBC

SyncResolver(javax.sql.rowset.spi.SyncResolver) is a “synchronized resolver” rowset , which extends the RowSet interface. According to the J2SE 5.0 documentation, SyncResolver

. . . defines a framework that allows applications to use a manual decision tree to decide what should be done when a synchronization conflict occurs. Although it is not mandatory for applications to resolve synchronization conflicts manually, this framework provides the means to delegate to the application when conflicts arise.Note that a conflict is a situation where the RowSet object’s original values for a row do not match the values in the data source, which indicates that the data source row has been modified since the last synchronization.Note also that a RowSet object’s original values are the values it had just prior to the last synchronization, which are not necessarily its initial values.

According to the J2SE 5.0 documentation, a SyncResolver object is a specialized RowSet object that implements the SyncResolver interface. It may operate as either a connected RowSet object (an implementation of the JdbcRowSet interface) or a disconnected RowSet object (an implementation of the CachedRowSet interface or one of its subinterfaces). The reference implementation for SyncResolver implements the CachedRowSet interface, but other implementations may choose to implement the JdbcRowSet interface to satisfy particular needs.

What is a synchronization conflict and when does it occur? When you change (update, insert, or delete) your RowSet object and you want to commit changes back to your data source, if there are problems, then these will be reported back to the client code. These problems are called “synchronization conflicts” (because you are trying to synchronize your rowset with data source data). Four types of conflicts (defined as static int) have been identified by the SyncResolver, and I have listed them here (for details on these types of conflicts, refer to the JDBC 3.0 Specification and consult the documentation for the driver you are working with):

  • static int DELETE_ROW_CONFLICT: Indicates that a conflict occurred while the RowSet object was attempting to delete a row in the data source.
  • static int INSERT_ROW_CONFLICT: Indicates that a conflict occurred while the RowSet object was attempting to insert a row into the data source.
  • static int NO_ROW_CONFLICT: Indicates that no conflict occurred while the RowSet object was attempting to update, delete, or insert a row in the data source.
  • static int UPDATE_ROW_CONFLICT: Indicates that a conflict occurred while the RowSet object was attempting to update a row in the data source.

According to the J2SE 5.0 documentation, the reference implementation provides a means by which an application can choose to resolve conflicts on a case-by-case basis. After the writer (a class that implements the RowSetWriter interface) finds all the conflicts, it throws a SyncProviderException object. An application can catch that exception and use it to create a SyncResolver object, a specialized kind of RowSet object. A SyncResolver object mirrors the RowSet object experiencing the conflicts, having the same number of rows and columns; however, it contains only the data that is in conflict. The SyncResolver object retrieves each conflict value in a row, comparing the value in the data source with the value in the RowSet object. After a decision is made as to which value should persist for each conflict in the row, the SyncResolver object sets those values with the method setResolvedValue. The SyncResolver object then goes to the next conflict and repeats the process until there are no more conflicts.

The following example (adapted from the J2SE 5.0 documentation) shows how to use a SyncResolver object:

import javax.sql.rowSet.CachedRowSet;
import javax.sql.rowset.spi.SyncResolver
import javax.sql.rowset.spi.SyncProviderException;
. . .
CachedRowSet crs = …;
try {
. . .
. . .
catch (SyncProviderException spe) {
SyncResolver resolver = spe.getSyncResolver();
If (resolver == null) {
// vendor has not implemented SyncResolver
// not much can be done here.
else {
// vendor has implemented SyncResolver
Object crsValue; // value in the RowSet object
Object resolverValue: // value in the SyncResolver object
Object resolvedValue: // value to be persisted
while(resolver.nextConflict()) {
if(resolver.getStatus() == SyncResolver.UPDATE_ROW_CONFLICT) {
int row = resolver.getRow();
int colCount = crs.getMetaData().getColumnCount();
for(int j = 1; j <= colCount; j++) {
if (resolver.getConflictValue(j) != null) {
crsValue = crs.getObject(j);
resolverValue = resolver.getConflictValue(j);
. . .
// compare crsValue and resolverValue to determine
// which should be the resolved value (the value to persist)
resolvedValue = crsValue;
resolver.setResolvedValue(j, resolvedValue);
} // while
} //else

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JDBC Topics