📄 syncresolver.java
字号:
/* * @(#)SyncResolver.java 1.5 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package javax.sql.rowset.spi;import javax.sql.RowSet;import java.sql.SQLException;/** * 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. * <p> * Note that a conflict is a situation where the <code>RowSet</code> 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 <code>RowSet</code> object's original values are the values it had just prior to the * the last synchronization, which are not necessarily its initial values. * <p> * * <H2>Description of a <code>SyncResolver</code> Object</H2> * * A <code>SyncResolver</code> object is a specialized <code>RowSet</code> object * that implements the <code>SyncResolver</code> interface. * It <b>may</b> operate as either a connected <code>RowSet</code> object (an * implementation of the <code>JdbcRowSet</code> interface) or a connected * <code>RowSet</code> object (an implementation of the * <code>CachedRowSet</code> interface or one of its subinterfaces). For information * on the subinterfaces, see the * <a href="../package-summary.html"><code>javax.sql.rowset</code></a> package * description. The reference implementation for <code>SyncResolver</code> implements * the <code>CachedRowSet</code> interface, but other implementations * may choose to implement the <code>JdbcRowSet</code> interface to satisfy * particular needs. * <P> * After an application has attempted to synchronize a <code>RowSet</code> object with * the data source (by calling the <code>CachedRowSet</code> * method <code>acceptChanges</code>), and one or more conflicts have been found, * a rowset's <code>SyncProvider</code> object creates an instance of * <code>SyncResolver</code>. This new <code>SyncResolver</code> object has * the same number of rows and columns as the * <code>RowSet</code> object that was attempting the synchronization. The * <code>SyncResolver</code> object contains the values from the data source that caused * the conflict(s) and <code>null</code> for all other values. * In addition, it contains information about each conflict. * <P> * * <H2>Getting and Using a <code>SyncResolver</code> Object</H2> * * When the method <code>acceptChanges</code> encounters conflicts, the * <code>SyncProvider</code> object creates a <code>SyncProviderException</code> * object and sets it with the new <code>SyncResolver</code> object. The method * <code>acceptChanges</code> will throw this exception, which * the application can then catch and use to retrieve the * <code>SyncResolver</code> object it contains. The following code snippet uses the * <code>SyncProviderException</code> method <code>getSyncResolver</code> to get * the <code>SyncResolver</code> object <i>resolver</i>. * <PRE> * } catch (SyncProviderException spe) { * SyncResolver resolver = spe.getSyncResolver(); * ... * } * </PRE> * <P> * With <i>resolver</i> in hand, an application can use it to get the information * it contains about the conflict or conflicts. A <code>SyncResolver</code> object * such as <i>resolver</i> keeps * track of the conflicts for each row in which there is a conflict. It also places a * lock on the table or tables affected by the rowset's command so that no more * conflicts can occur while the current conflicts are being resolved. * <P> * The following kinds of information can be obtained from a <code>SyncResolver</code> * object: * <P> * <LI>What operation was being attempted when a conflict occurred<BR> * The <code>SyncProvider</code> interface defines four constants * describing states that may occur. Three * constants describe the type of operation (update, delete, or insert) that a * <code>RowSet</code> object was attempting to perform when a conflict was discovered, * and the fourth indicates that there is no conflict. * These constants are the possible return values when a <code>SyncResolver</code> object * calls the method <code>getStatus</code>. * <PRE> * int operation = resolver.getStatus(); * </PRE> * <P> * <LI>The value in the data source that caused a conflict<BR> * A conflict exists when a value that a <code>RowSet</code> object has changed * and is attempting to write to the data source * has also been changed in the data source since the last synchronization. An * application can call the <code>SyncResolver</code> method * <code>getConflictValue</code > to retrieve the * value in the data source that is the cause of the conflict because the values in a * <code>SyncResolver</code> object are the conflict values from the data source. * <PRE> * java.lang.Object conflictValue = resolver.getConflictValue(2); * </PRE> * Note that the column in <i>resolver</i> can be designated by the column number, * as is done in the preceding line of code, or by the column name. * </UL> * <P> * With the information retrieved from the methods <code>getStatus</code> and * <code>getConflictValue</code>, the application may make a determination as to * which value should be persisted in the data source. The application then calls the * <code>SyncResolver</code> method <code>setResolvedValue</code>, which sets the value * to be persisted in the <code>RowSet</code> object and also in the data source. * <PRE> * resolver.setResolvedValue("DEPT", 8390426); * </PRE> * In the preceding line of code, * the column name designates the column in the <code>RowSet</code> object * that is to be set with the given value. The column number can also be used to * designate the column. * <P> * An application calls the method <code>setResolvedValue</code> after it has * resolved all of the conflicts in the current conflict row and repeats this process * for each conflict row in the <code>SyncResolver</code> object. * <P> * * <H2>Navigating a <code>SyncResolver</code> Object</H2> * * Because a <code>SyncResolver</code> object is a <code>RowSet</code> object, an * application can use all of the <code>RowSet</code> methods for moving the cursor * to navigate a <code>SyncResolver</code> object. For example, an application can * use the <code>RowSet</code> method <code>next</code> to get to each row and then * call the <code>SyncResolver</code> method <code>getStatus</code> to see if the row * contains a conflict. In a row with one or more conflicts, the application can * iterate through the columns to find any non-null values, which will be the values * from the data source that are in conflict. * <P> * To make it easier to navigate a <code>SyncResolver</code> object, especially when * there are large numbers of rows with no conflicts, the <code>SyncResolver</code> * interface defines the methods <code>nextConflict</code> and * <code>previousConflict</code>, which move only to rows * that contain at least one conflict value. Then an application can call the * <code>SyncResolver</code> method <code>getConflictValue</code>, supplying it * with the column number, to get the conflict value itself. The code fragment in the * next section gives an example. * * <H2>Code Example</H2> * * The following code fragment demonstrates how a disconnected <code>RowSet</code> * object <i>crs</i> might attempt to synchronize itself with the * underlying data source and then resolve the conflicts. In the <code>try</code> * block, <i>crs</i> calls the method <code>acceptChanges</code>, passing it the * <code>Connection</code> object <i>con</i>. If there are no conflicts, the * changes in <i>crs</i> are simply written to the data source. However, if there * is a conflict, the method <code>acceptChanges</code> throws a * <code>SyncProviderException</code> object, and the * <code>catch</code> block takes effect. In this example, which * illustrates one of the many ways a <code>SyncResolver</code> object can be used, * the <code>SyncResolver</code> method <code>nextConflict</code> is used in a * <code>while</code> loop. The loop will end when <code>nextConflict</code> returns * <code>false</code>, which will occur when there are no more conflict rows in the * <code>SyncResolver</code> object <i>resolver</i>. In This particular code fragment, * <i>resolver</i> looks for rows that have update conflicts (rows with the status * <code>SyncResolver.UPDATE_ROW_CONFLICT</code>), and the rest of this code fragment * executes only for rows where conflicts occurred because <i>crs</i> was attempting an * update. * <P> * After the cursor for <i>resolver</i> has moved to the next conflict row that * has an update conflict, the method <code>getRow</code> indicates the number of the * current row, and * the cursor for the <code>CachedRowSet</code> object <i>crs</i> is moved to * the comparable row in <i>crs</i>. By iterating * through the columns of that row in both <i>resolver</i> and <i>crs</i>, the conflicting * values can be retrieved and compared to decide which one should be persisted. In this * code fragment, the value in <i>crs</i> is the one set as the resolved value, which means * that it will be used to overwrite the conflict value in the data source. * * <PRE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -