com.sun.rowset.internal
Class CachedRowSetWriter

java.lang.Object
  extended bycom.sun.rowset.internal.CachedRowSetWriter
All Implemented Interfaces:
javax.sql.RowSetWriter, java.io.Serializable, TransactionalWriter

public class CachedRowSetWriter
extends java.lang.Object
implements TransactionalWriter, java.io.Serializable

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.

Version:
0.2
Author:
Jonathan Bruce
See Also:
SyncProvider, SyncFactory, SyncFactoryException, Serialized Form

Constructor Summary
CachedRowSetWriter()
           
 
Method Summary
 void commit()
          Makes permanent all changes that have been performed by the acceptChanges method since the last call to either the commit or rollback methods.
 CachedRowSetReader getReader()
          Gets the reader for this writer.
 void rollback()
          Undoes all changes made in the current transaction.
 void rollback(java.sql.Savepoint s)
          Undoes all changes made in the current transaction made prior to the given Savepoint object.
 void setReader(CachedRowSetReader reader)
          Sets the reader for this writer to the given reader.
 boolean writeData(javax.sql.RowSetInternal caller)
          Propagates changes in the given RowSet object back to its underlying data source and returns true if successful.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CachedRowSetWriter

public CachedRowSetWriter()
Method Detail

writeData

public boolean writeData(javax.sql.RowSetInternal caller)
                  throws java.sql.SQLException
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.

Specified by:
writeData in interface javax.sql.RowSetWriter
Returns:
true if changes to the rowset were successfully written to the rowset's underlying data source; false otherwise
Throws:
java.sql.SQLException

setReader

public void setReader(CachedRowSetReader reader)
               throws java.sql.SQLException
Sets the reader for this writer to the given reader.

Throws:
java.sql.SQLException - if a database access error occurs

getReader

public CachedRowSetReader getReader()
                             throws java.sql.SQLException
Gets the reader for this writer.

Throws:
java.sql.SQLException - if a database access error occurs

commit

public void commit()
            throws java.sql.SQLException
Description copied from interface: TransactionalWriter
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.

Specified by:
commit in interface TransactionalWriter
Throws:
java.sql.SQLException - if a database access error occurs or the Connection object within this CachedRowSet object is in auto-commit mode

rollback

public void rollback()
              throws java.sql.SQLException
Description copied from interface: TransactionalWriter
Undoes all changes made in the current transaction. This method should be used only when auto-commit mode has been disabled.

Specified by:
rollback in interface TransactionalWriter
Throws:
java.sql.SQLException - if a database access error occurs or the Connection object within this CachedRowSet object is in auto-commit mode

rollback

public void rollback(java.sql.Savepoint s)
              throws java.sql.SQLException
Description copied from interface: TransactionalWriter
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.

Specified by:
rollback in interface TransactionalWriter
Parameters:
s - a Savepoint object marking a savepoint in the current transaction. All changes made before s was set will be undone. All changes made after s was set will be made permanent.
Throws:
java.sql.SQLException - if a database access error occurs or the Connection object within this CachedRowSet object is in auto-commit mode