com.sun.sql.rowset.internal
Class CachedRowSetXWriter

java.lang.Object
  extended by com.sun.sql.rowset.internal.CachedRowSetXWriter
All Implemented Interfaces:
java.io.Serializable, javax.sql.rowset.spi.TransactionalWriter, javax.sql.RowSetWriter

public class CachedRowSetXWriter
extends java.lang.Object
implements javax.sql.rowset.spi.TransactionalWriter, java.io.Serializable

An implementation of RowSetWriter to work with CachedRowSetX rowsets. Based on the Sun reference implementation of CachedRowSetWriter.

See Also:
SyncProvider, SyncFactory, SyncFactoryException, Serialized Form

Constructor Summary
CachedRowSetXWriter()
           
 
Method Summary
 void closeConnection()
           
 void commit()
           
 CachedRowSetXReader getReader()
          Gets the reader for this writer.
 void rollback()
           
 void rollback(java.sql.Savepoint s)
           
 void setReader(CachedRowSetXReader 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

CachedRowSetXWriter

public CachedRowSetXWriter()
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(CachedRowSetXReader 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 CachedRowSetXReader 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
Specified by:
commit in interface javax.sql.rowset.spi.TransactionalWriter
Throws:
java.sql.SQLException

rollback

public void rollback()
              throws java.sql.SQLException
Specified by:
rollback in interface javax.sql.rowset.spi.TransactionalWriter
Throws:
java.sql.SQLException

rollback

public void rollback(java.sql.Savepoint s)
              throws java.sql.SQLException
Specified by:
rollback in interface javax.sql.rowset.spi.TransactionalWriter
Throws:
java.sql.SQLException

closeConnection

public void closeConnection()
                     throws java.sql.SQLException
Throws:
java.sql.SQLException