Path: blob/master/src/java.sql.rowset/share/classes/com/sun/rowset/providers/RIOptimisticProvider.java
40948 views
/*1* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.rowset.providers;2627import com.sun.rowset.JdbcRowSetResourceBundle;28import javax.sql.*;29import java.io.*;3031import javax.sql.rowset.spi.*;32import com.sun.rowset.internal.*;3334/**35* The reference implementation of a JDBC Rowset synchronization provider36* providing optimistic synchronization with a relational datastore37* using any JDBC technology-enabled driver.38*39* <h2>1.0 Backgroud</h2>40* This synchronization provider is registered with the41* <code>SyncFactory</code> by default as the42* <code>com.sun.rowset.providers.RIOptimisticProvider</code>.43* As an extension of the <code>SyncProvider</code> abstract44* class, it provides the reader and writer classes required by disconnected45* rowsets as <code>javax.sql.RowSetReader</code> and <code>javax.sql.RowSetWriter</code>46* interface implementations. As a reference implementation,47* <code>RIOptimisticProvider</code> provides a48* fully functional implementation offering a medium grade classification of49* syncrhonization, namely GRADE_CHECK_MODIFIED_AT_COMMIT. A50* disconnected <code>RowSet</code> implementation using the51* <code>RIOptimisticProvider</code> can expect the writer to52* check only rows that have been modified in the <code>RowSet</code> against53* the values in the data source. If there is a conflict, that is, if a value54* in the data source has been changed by another party, the55* <code>RIOptimisticProvider</code> will not write any of the changes to the data56* source and will throw a <code>SyncProviderException</code> object.57*58* <h2>2.0 Usage</h2>59* Standard disconnected <code>RowSet</code> implementations may opt to use this60* <code>SyncProvider</code> implementation in one of two ways:61* <OL>62* <LI>By specifically calling the <code>setSyncProvider</code> method63defined in the <code>CachedRowSet</code> interface64* <pre>65* CachedRowset crs = new FooCachedRowSetImpl();66* crs.setSyncProvider("com.sun.rowset.providers.RIOptimisticProvider");67* </pre>68* <LI>By specifying it in the constructor of the <code>RowSet</code>69* implementation70* <pre>71* CachedRowset crs = new FooCachedRowSetImpl(72* "com.sun.rowset.providers.RIOptimisticProvider");73* </pre>74* </OL>75* Note that because the <code>RIOptimisticProvider</code> implementation is76* the default provider, it will always be the provider when no provider ID is77* specified to the constructor.78* <P>79* See the standard <code>RowSet</code> reference implementations in the80* <code>com.sun.rowset</code> package for more details.81*82* @author Jonathan Bruce83* @see javax.sql.rowset.spi.SyncProvider84* @see javax.sql.rowset.spi.SyncProviderException85* @see javax.sql.rowset.spi.SyncFactory86* @see javax.sql.rowset.spi.SyncFactoryException87*88*/89public final class RIOptimisticProvider extends SyncProvider implements Serializable {9091private CachedRowSetReader reader;92private CachedRowSetWriter writer;9394/**95* The unique provider identifier.96*/97private String providerID = "com.sun.rowset.providers.RIOptimisticProvider";9899/**100* The vendor name of this SyncProvider implementation101*/102private String vendorName = "Oracle Corporation";103104/**105* The version number of this SyncProvider implementation106*/107private String versionNumber = "1.0";108109/**110* ResourceBundle111*/112private JdbcRowSetResourceBundle resBundle;113114/**115* Creates an <code>RIOptimisticProvider</code> object initialized with the116* fully qualified class name of this <code>SyncProvider</code> implementation117* and a default reader and writer.118* <P>119* This provider is available to all disconnected <code>RowSet</code> implementations120* as the default persistence provider.121*/122public RIOptimisticProvider() {123providerID = this.getClass().getName();124reader = new CachedRowSetReader();125writer = new CachedRowSetWriter();126try {127resBundle = JdbcRowSetResourceBundle.getJdbcRowSetResourceBundle();128} catch(IOException ioe) {129throw new RuntimeException(ioe);130}131}132133/**134* Returns the <code>'javax.sql.rowset.providers.RIOptimisticProvider'</code>135* provider identification string.136*137* @return String Provider ID of this persistence provider138*/139public String getProviderID() {140return providerID;141}142143/**144* Returns the <code>javax.sql.RowSetWriter</code> object for this145* <code>RIOptimisticProvider</code> object. This is the writer that will146* write changes made to the <code>Rowset</code> object back to the data source.147*148* @return the <code>javax.sql.RowSetWriter</code> object for this149* <code>RIOptimisticProvider</code> object150*/151public RowSetWriter getRowSetWriter() {152try {153writer.setReader(reader);154} catch (java.sql.SQLException e) {}155return writer;156}157158/**159* Returns the <code>javax.sql.RowSetReader</code> object for this160* <code>RIOptimisticProvider</code> object. This is the reader that will161* populate a <code>RowSet</code> object using this <code>RIOptimisticProvider</code>.162*163* @return the <code>javax.sql.RowSetReader</code> object for this164* <code>RIOptimisticProvider</code> object165*/166public RowSetReader getRowSetReader() {167return reader;168}169170/**171* Returns the <code>SyncProvider</code> grade of synchronization that172* <code>RowSet</code> objects can expect when using this173* implementation. As an optimisic synchonization provider, the writer174* will only check rows that have been modified in the <code>RowSet</code>175* object.176*/177public int getProviderGrade() {178return SyncProvider.GRADE_CHECK_MODIFIED_AT_COMMIT;179}180181/**182* Modifies the data source lock severity according to the standard183* <code>SyncProvider</code> classifications.184*185* @param datasource_lock An <code>int</code> indicating the level of locking to be186* set; must be one of the following constants:187* <PRE>188* SyncProvider.DATASOURCE_NO_LOCK,189* SyncProvider.DATASOURCE_ROW_LOCK,190* SyncProvider.DATASOURCE_TABLE_LOCK,191* SyncProvider.DATASOURCE_DB_LOCk192* </PRE>193* @throws SyncProviderException if the parameter specified is not194* <code>SyncProvider.DATASOURCE_NO_LOCK</code>195*/196public void setDataSourceLock(int datasource_lock) throws SyncProviderException {197if(datasource_lock != SyncProvider.DATASOURCE_NO_LOCK ) {198throw new SyncProviderException(resBundle.handleGetObject("riop.locking").toString());199}200}201202/**203* Returns the active data source lock severity in this204* reference implementation of the <code>SyncProvider</code>205* abstract class.206*207* @return <code>SyncProvider.DATASOURCE_NO_LOCK</code>.208* The reference implementation does not support data source locks.209*/210public int getDataSourceLock() throws SyncProviderException {211return SyncProvider.DATASOURCE_NO_LOCK;212}213214/**215* Returns the supported updatable view abilities of the216* reference implementation of the <code>SyncProvider</code>217* abstract class.218*219* @return <code>SyncProvider.NONUPDATABLE_VIEW_SYNC</code>. The220* the reference implementation does not support updating tables221* that are the source of a view.222*/223public int supportsUpdatableView() {224return SyncProvider.NONUPDATABLE_VIEW_SYNC;225}226227/**228* Returns the release version ID of the Reference Implementation Optimistic229* Synchronization Provider.230*231* @return the <code>String</code> detailing the version number of this SyncProvider232*/233public String getVersion() {234return this.versionNumber;235}236237/**238* Returns the vendor name of the Reference Implementation Optimistic239* Synchronization Provider240*241* @return the <code>String</code> detailing the vendor name of this242* SyncProvider243*/244public String getVendor() {245return this.vendorName;246}247248private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {249// Default state initialization happens here250ois.defaultReadObject();251// Initialization of transient Res Bundle happens here .252try {253resBundle = JdbcRowSetResourceBundle.getJdbcRowSetResourceBundle();254} catch(IOException ioe) {255throw new RuntimeException(ioe);256}257258}259static final long serialVersionUID =-3143367176751761936L;260261}262263264