APIdia Launch Partner
Top Description Fields Constructors Methods
com.sun.rowset.internal

public Class CachedRowSetWriter

extends Object
implements TransactionalWriter, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, javax.sql.rowset.spi.TransactionalWriter, javax.sql.RowSetWriter
Imports
java.sql.*, javax.sql.*, java.util.*, java.io.*, com.sun.rowset.*, java.text.MessageFormat, javax.sql.rowset.*, javax.sql.rowset.serial.SQLInputImpl, .SerialArray, .SerialBlob, .SerialClob, .SerialStruct, javax.sql.rowset.spi.*

The facility called on internally by the RIOptimisticProvider implementation to propagate changes back to the data source from which the rowset got its data.

A CachedRowSetWriter object, called a writer, has the public method writeData for writing modified data to the underlying data source. This method is invoked by the rowset internally and is never invoked directly by an application. A writer also has public methods for setting and getting the CachedRowSetReader object, called a reader, that is associated with the writer. The remainder of the methods in this class are private and are invoked internally, either directly or indirectly, by the method writeData.

Typically the SyncFactory manages the RowSetReader and the RowSetWriter implementations using SyncProvider objects. Standard JDBC RowSet implementations provide an object instance of this writer by invoking the SyncProvider.getRowSetWriter() method.

Author
Jonathan Bruce
Version
0.2
See Also
javax.sql.rowset.spi.SyncProvider, javax.sql.rowset.spi.SyncFactory, javax.sql.rowset.spi.SyncFactoryException

Field Summary

Modifier and TypeField and Description
private int
callerColumnCount

The number of columns in the CachedRowSet object for which this CachedRowSetWriter object is the writer.

private ResultSetMetaData
callerMd

The ResultSetMetaData object that contains information about the columns in the CachedRowSet object for which this CachedRowSetWriter object is the writer.

private transient Connection
con

The Connection object that this writer will use to make a connection to the data source to which it will write data.

private CachedRowSetImpl
crsResolve

This CachedRowSet will hold the conflicting values retrieved from the db and hold it.

private String
deleteCmd

The SQL DELETE command that this writer will call internally to delete a row in the rowset's underlying data source.

private String
deleteWhere

The SQL WHERE clause the writer will use for delete statements in the PreparedStatement object it sends to the underlying data source.

private int
iChangedValsInDbAndCRS

This will check whether the same field value has changed both in database and CachedRowSet.

private int
iChangedValsinDbOnly

This will hold the number of cols for which the values have changed only in database.

private String
insertCmd

The SQL INSERT INTO command that this writer will internally use to insert data into the rowset's underlying data source.

private int[]
keyCols

An array containing the column numbers of the columns that are needed to uniquely identify a row in the CachedRowSet object for which this CachedRowSetWriter object is the writer.

private Object[]
params

An array of the parameters that should be used to set the parameter placeholders in a PreparedStatement object that this writer will execute.

private CachedRowSetReader
reader

The CachedRowSetReader object that has been set as the reader for the CachedRowSet object for which this CachedRowSetWriter object is the writer.

private JdbcRowSetResourceBundle
private String
selectCmd

The SQL SELECT command that this writer will call internally.

pack-priv static final long
private ArrayList<Integer>
status

This ArrayList will hold the values of SyncResolver.*

private String
updateCmd

The SQL UPDATE command that this writer will call internally to write data to the rowset's underlying data source.

private String
updateWhere

The SQL WHERE clause the writer will use for update statements in the PreparedStatement object it sends to the underlying data source.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private void
buildKeyDesc(CachedRowSet
the CachedRowSet object for which this CachedRowSetWriter object is the writer
crs
)

Assigns to the given CachedRowSet object's params field an array whose length equals the number of columns needed to uniquely identify a row in the rowset.

private String

Returns:

a String object with the fully qualified name of the table from which this writer's rowset was derived
buildTableName
(DatabaseMetaData
a DatabaseMetaData object that contains metadata about this writer's CachedRowSet object
dbmd
,
String
a String object with the rowset's catalog name
catalog
,
String schema, String
a String object with the name of the table from which this writer's rowset was derived
table
)

Returns a fully qualified table name built from the given catalog and table names.

private String

Returns:

a WHERE clause of the form "WHERE columnName = ? AND columnName = ? AND columnName = ? ..."
buildWhereClause
(String
a String object that is an empty string ("")
whereClause
,
ResultSet
a ResultSet object that can be used to access the CachedRowSet object's data
rs
)

Constructs an SQL WHERE clause using the given string as a starting point.

public void
commit()

Implements javax.sql.rowset.spi.TransactionalWriter.commit.

Makes permanent all changes that have been performed by the acceptChanges method since the last call to either the commit or rollback methods.

public void
commit(CachedRowSetImpl crs, boolean updateRowset)

private boolean

Returns:

false if the deletion was successful, which means that there was no conflict; true otherwise
deleteOriginalRow
(CachedRowSet crs, CachedRowSetImpl crsRes)

Deletes the row in the underlying data source that corresponds to a row that has been deleted in the given CachedRowSet object and returns false if the deletion was successful.

public CachedRowSetReader
getReader()

Gets the reader for this writer.

private void
initSQLStatements(CachedRowSet
a CachedRowSet object for which this CachedRowSetWriter object is the writer
caller
)

Composes a SELECT, UPDATE, INSERT, and DELETE statement that can be used by this writer to write data to the data source backing the given CachedRowSet object.

private boolean

Returns:

false to indicate that the insertion was successful; true otherwise
insertNewRow
(CachedRowSet
the CachedRowSet object that has had a row inserted and to whose underlying data source the row will be inserted
crs
,
PreparedStatement
the PreparedStatement object that will be used to execute the insertion
pstmt
,
CachedRowSetImpl crsRes)

Inserts a row that has been inserted into the given CachedRowSet object into the data source from which the rowset is derived, returning false if the insertion was successful.

private boolean

Returns:

true if found, false otherwise (auto generated key)
isPKNameValid
(String
- Primary Key to validate
pk
,
ResultSetMetaData
- ResultSetMetadata for the RowSet
rsmd
)

Validate whether the Primary Key is known to the CachedRowSet.

private void
public void
rollback()

Implements javax.sql.rowset.spi.TransactionalWriter.rollback.

Undoes all changes made in the current transaction.

public void
rollback(Savepoint s)

Implements javax.sql.rowset.spi.TransactionalWriter.rollback.

Undoes all changes made in the current transaction made prior to the given Savepoint object.

public void
setReader(CachedRowSetReader reader)

Sets the reader for this writer to the given reader.

private boolean

Returns:

false if the update to the underlying data source is successful; true otherwise
updateOriginalRow
(CachedRowSet crs)

Updates the given CachedRowSet object's underlying data source so that updates to the rowset are reflected in the original data source, and returns false if the update was successful.

pack-priv void
public boolean

Returns:

true if changes to the rowset were successfully written to the rowset's underlying data source; false otherwise
writeData
(RowSetInternal caller)

Implements javax.sql.RowSetWriter.writeData.

Propagates changes in the given RowSet object back to its underlying data source and returns true if successful.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

callerColumnCountback to summary
private int callerColumnCount

The number of columns in the CachedRowSet object for which this CachedRowSetWriter object is the writer.

callerMdback to summary
private ResultSetMetaData callerMd

The ResultSetMetaData object that contains information about the columns in the CachedRowSet object for which this CachedRowSetWriter object is the writer.

Annotations
@SuppressWarnings:serial
conback to summary
private transient Connection con

The Connection object that this writer will use to make a connection to the data source to which it will write data.

crsResolveback to summary
private CachedRowSetImpl crsResolve

This CachedRowSet will hold the conflicting values retrieved from the db and hold it.

deleteCmdback to summary
private String deleteCmd

The SQL DELETE command that this writer will call internally to delete a row in the rowset's underlying data source.

deleteWhereback to summary
private String deleteWhere

The SQL WHERE clause the writer will use for delete statements in the PreparedStatement object it sends to the underlying data source.

iChangedValsInDbAndCRSback to summary
private int iChangedValsInDbAndCRS

This will check whether the same field value has changed both in database and CachedRowSet.

iChangedValsinDbOnlyback to summary
private int iChangedValsinDbOnly

This will hold the number of cols for which the values have changed only in database.

insertCmdback to summary
private String insertCmd

The SQL INSERT INTO command that this writer will internally use to insert data into the rowset's underlying data source. The method initSQLStatements builds this command with a question mark parameter placeholder for each column in the rowset.

keyColsback to summary
private int[] keyCols

An array containing the column numbers of the columns that are needed to uniquely identify a row in the CachedRowSet object for which this CachedRowSetWriter object is the writer.

paramsback to summary
private Object[] params

An array of the parameters that should be used to set the parameter placeholders in a PreparedStatement object that this writer will execute.

Annotations
@SuppressWarnings:serial
readerback to summary
private CachedRowSetReader reader

The CachedRowSetReader object that has been set as the reader for the CachedRowSet object for which this CachedRowSetWriter object is the writer.

resBundleback to summary
private JdbcRowSetResourceBundle resBundle
selectCmdback to summary
private String selectCmd

The SQL SELECT command that this writer will call internally. The method initSQLStatements builds this command by supplying the words "SELECT" and "FROM," and using metadata to get the table name and column names .

serialVersionUIDback to summary
pack-priv static final long serialVersionUID
statusback to summary
private ArrayList<Integer> status

This ArrayList will hold the values of SyncResolver.*

updateCmdback to summary
private String updateCmd

The SQL UPDATE command that this writer will call internally to write data to the rowset's underlying data source. The method initSQLStatements builds this String object.

updateWhereback to summary
private String updateWhere

The SQL WHERE clause the writer will use for update statements in the PreparedStatement object it sends to the underlying data source.

Constructor Detail

CachedRowSetWriterback to summary
public CachedRowSetWriter()

Method Detail

buildKeyDescback to summary
private void buildKeyDesc(CachedRowSet crs) throws SQLException

Assigns to the given CachedRowSet object's params field an array whose length equals the number of columns needed to uniquely identify a row in the rowset. The array is given values by the method buildWhereClause.

If the CachedRowSet object's keyCols field has length 0 or is null, the array is set with the column number of every column in the rowset. Otherwise, the array in the field keyCols is set with only the column numbers of the columns that are required to form a unique identifier for a row.

Parameters
crs:CachedRowSet

the CachedRowSet object for which this CachedRowSetWriter object is the writer

Exceptions
SQLException:
if a database access error occurs
buildTableNameback to summary
private String buildTableName(DatabaseMetaData dbmd, String catalog, String schema, String table) throws SQLException

Returns a fully qualified table name built from the given catalog and table names. The given metadata object is used to get the proper order and separator.

Parameters
dbmd:DatabaseMetaData

a DatabaseMetaData object that contains metadata about this writer's CachedRowSet object

catalog:String

a String object with the rowset's catalog name

table:String

a String object with the name of the table from which this writer's rowset was derived

Returns:String

a String object with the fully qualified name of the table from which this writer's rowset was derived

Exceptions
SQLException:
if a database access error occurs
buildWhereClauseback to summary
private String buildWhereClause(String whereClause, ResultSet rs) throws SQLException

Constructs an SQL WHERE clause using the given string as a starting point. The resulting clause will contain a column name and " = ?" for each key column, that is, each column that is needed to form a unique identifier for a row in the rowset. This WHERE clause can be added to a PreparedStatement object that updates, inserts, or deletes a row.

This method uses the given result set to access values in the CachedRowSet object that called this writer. These values are used to build the array of parameters that will serve as replacements for the "?" parameter placeholders in the PreparedStatement object that is sent to the CachedRowSet object's underlying data source.

Parameters
whereClause:String

a String object that is an empty string ("")

rs:ResultSet

a ResultSet object that can be used to access the CachedRowSet object's data

Returns:String

a WHERE clause of the form "WHERE columnName = ? AND columnName = ? AND columnName = ? ..."

Exceptions
SQLException:
if a database access error occurs
commitback to summary
public void commit() throws SQLException

Implements javax.sql.rowset.spi.TransactionalWriter.commit.

Doc from javax.sql.rowset.spi.TransactionalWriter.commit.

Makes permanent all changes that have been performed by the acceptChanges method since the last call to either the commit or rollback methods. This method should be used only when auto-commit mode has been disabled.

Exceptions
SQLException:
if a database access error occurs or the Connection object within this CachedRowSet object is in auto-commit mode
commitback to summary
public void commit(CachedRowSetImpl crs, boolean updateRowset) throws SQLException
deleteOriginalRowback to summary
private boolean deleteOriginalRow(CachedRowSet crs, CachedRowSetImpl crsRes) throws SQLException

Deletes the row in the underlying data source that corresponds to a row that has been deleted in the given CachedRowSet object and returns false if the deletion was successful.

This method is called internally by this writer's writeData method when a row in the rowset has been deleted. The values in the deleted row are the same as those that are stored in the original row of the given CachedRowSet object. If the values in the original row differ from the row in the underlying data source, the row in the data source is not deleted, and deleteOriginalRow returns true to indicate that there was a conflict.

Returns:boolean

false if the deletion was successful, which means that there was no conflict; true otherwise

Exceptions
SQLException:
if there was a database access error
getReaderback to summary
public CachedRowSetReader getReader() throws SQLException

Gets the reader for this writer.

Exceptions
SQLException:
if a database access error occurs
initSQLStatementsback to summary
private void initSQLStatements(CachedRowSet caller) throws SQLException

Composes a SELECT, UPDATE, INSERT, and DELETE statement that can be used by this writer to write data to the data source backing the given CachedRowSet object.

Parameters
caller:CachedRowSet

a CachedRowSet object for which this CachedRowSetWriter object is the writer

Exceptions
SQLException:
if a database access error occurs
insertNewRowback to summary
private boolean insertNewRow(CachedRowSet crs, PreparedStatement pstmt, CachedRowSetImpl crsRes) throws SQLException

Inserts a row that has been inserted into the given CachedRowSet object into the data source from which the rowset is derived, returning false if the insertion was successful.

Parameters
crs:CachedRowSet

the CachedRowSet object that has had a row inserted and to whose underlying data source the row will be inserted

pstmt:PreparedStatement

the PreparedStatement object that will be used to execute the insertion

Returns:boolean

false to indicate that the insertion was successful; true otherwise

Exceptions
SQLException:
if a database access error occurs
isPKNameValidback to summary
private boolean isPKNameValid(String pk, ResultSetMetaData rsmd) throws SQLException

Validate whether the Primary Key is known to the CachedRowSet. If it is not, it is an auto-generated key

Parameters
pk:String

- Primary Key to validate

rsmd:ResultSetMetaData

- ResultSetMetadata for the RowSet

Returns:boolean

true if found, false otherwise (auto generated key)

readObjectback to summary
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException
rollbackback to summary
public void rollback() throws SQLException

Implements javax.sql.rowset.spi.TransactionalWriter.rollback.

Doc from javax.sql.rowset.spi.TransactionalWriter.rollback.

Undoes all changes made in the current transaction. This method should be used only when auto-commit mode has been disabled.

Exceptions
SQLException:
if a database access error occurs or the Connection object within this CachedRowSet object is in auto-commit mode
rollbackback to summary
public void rollback(Savepoint s) throws SQLException

Implements javax.sql.rowset.spi.TransactionalWriter.rollback.

Doc from javax.sql.rowset.spi.TransactionalWriter.rollback.

Undoes all changes made in the current transaction made prior to the given Savepoint object. This method should be used only when auto-commit mode has been disabled.

Exceptions
SQLException:
if a database access error occurs or the Connection object within this CachedRowSet object is in auto-commit mode
setReaderback to summary
public void setReader(CachedRowSetReader reader) throws SQLException

Sets the reader for this writer to the given reader.

Exceptions
SQLException:
if a database access error occurs
updateOriginalRowback to summary
private boolean updateOriginalRow(CachedRowSet crs) throws SQLException

Updates the given CachedRowSet object's underlying data source so that updates to the rowset are reflected in the original data source, and returns false if the update was successful. A return value of true indicates that there is a conflict, meaning that a value updated in the rowset has already been changed by someone else in the underlying data source. A conflict can also exist if, for example, more than one row in the data source would be affected by the update or if no rows would be affected. In any case, if there is a conflict, this method does not update the underlying data source.

This method is called internally by the method writeData if a row in the CachedRowSet object for which this CachedRowSetWriter object is the writer has been updated.

Returns:boolean

false if the update to the underlying data source is successful; true otherwise

Exceptions
SQLException:
if a database access error occurs
updateResolvedConflictToDBback to summary
pack-priv void updateResolvedConflictToDB(CachedRowSet crs, Connection con) throws SQLException
writeDataback to summary
public boolean writeData(RowSetInternal caller) throws SQLException

Implements javax.sql.RowSetWriter.writeData.

Propagates changes in the given RowSet object back to its underlying data source and returns true if successful. The writer will check to see if the data in the pre-modified rowset (the original values) differ from the data in the underlying data source. If data in the data source has been modified by someone else, there is a conflict, and in that case, the writer will not write to the data source. In other words, the writer uses an optimistic concurrency algorithm: It checks for conflicts before making changes rather than restricting access for concurrent users.

This method is called by the rowset internally when the application invokes the method acceptChanges. The writeData method in turn calls private methods that it defines internally. The following is a general summary of what the method writeData does, much of which is accomplished through calls to its own internal methods.

  1. Creates a CachedRowSet object from the given RowSet object
  2. Makes a connection with the data source
    • Disables autocommit mode if it is not already disabled
    • Sets the transaction isolation level to that of the rowset
  3. Checks to see if the reader has read new data since the writer was last called and, if so, calls the method initSQLStatements to initialize new SQL statements
    • Builds new SELECT, UPDATE, INSERT, and DELETE statements
    • Uses the CachedRowSet object's metadata to determine the table name, column names, and the columns that make up the primary key
  4. When there is no conflict, propagates changes made to the CachedRowSet object back to its underlying data source
    • Iterates through each row of the CachedRowSet object to determine whether it has been updated, inserted, or deleted
    • If the corresponding row in the data source has not been changed since the rowset last read its values, the writer will use the appropriate command to update, insert, or delete the row
    • If any data in the data source does not match the original values for the CachedRowSet object, the writer will roll back any changes it has made to the row in the data source.
Returns:boolean

true if changes to the rowset were successfully written to the rowset's underlying data source; false otherwise

Exceptions
SQLException:

Doc from javax.sql.RowSetWriter.writeData.

if a database access error occurs