List of usage examples for javax.sql.rowset.spi SyncProviderException getSyncResolver
public SyncResolver getSyncResolver()
SyncResolver
object that has been set for this SyncProviderException
object, or if none has been set, an instance of the default SyncResolver
implementation included in the reference implementation. From source file:com.oracle.tutorial.jdbc.CachedRowSetSample.java
public void testPaging() throws SQLException, MalformedURLException { CachedRowSet crs = null;/*from w w w . j a va 2 s . c o m*/ this.con.setAutoCommit(false); try { crs = new CachedRowSetImpl(); crs.setUsername(settings.userName); crs.setPassword(settings.password); if (this.dbms.equals("mysql")) { crs.setUrl(settings.urlString + "?relaxAutoCommit=true"); } else { crs.setUrl(settings.urlString); } crs.setCommand("select * from MERCH_INVENTORY"); // Setting the page size to 4, such that we // get the data in chunks of 4 rows @ a time. crs.setPageSize(100); // Now get the first set of data crs.execute(); crs.addRowSetListener(new ExampleRowSetListener()); // Keep on getting data in chunks until done. int i = 1; do { System.out.println("Page number: " + i); while (crs.next()) { System.out.println("Found item " + crs.getInt("ITEM_ID") + ": " + crs.getString("ITEM_NAME")); if (crs.getInt("ITEM_ID") == 1235) { int currentQuantity = crs.getInt("QUAN") + 1; System.out.println("Updating quantity to " + currentQuantity); crs.updateInt("QUAN", currentQuantity + 1); crs.updateRow(); // Syncing the row back to the DB crs.acceptChanges(con); } } // End of inner while i++; } while (crs.nextPage()); // End of outer while // Inserting a new row // Doing a previous page to come back to the last page // as we ll be after the last page. int newItemId = 123456; if (this.doesItemIdExist(newItemId)) { System.out.println("Item ID " + newItemId + " already exists"); } else { crs.previousPage(); crs.moveToInsertRow(); crs.updateInt("ITEM_ID", newItemId); crs.updateString("ITEM_NAME", "TableCloth"); crs.updateInt("SUP_ID", 927); crs.updateInt("QUAN", 14); Calendar timeStamp; timeStamp = new GregorianCalendar(); timeStamp.set(2006, 4, 1); crs.updateTimestamp("DATE_VAL", new Timestamp(timeStamp.getTimeInMillis())); crs.insertRow(); crs.moveToCurrentRow(); // Syncing the new row back to the database. System.out.println("About to add a new row..."); crs.acceptChanges(con); System.out.println("Added a row..."); this.viewTable(con); } } catch (SyncProviderException spe) { SyncResolver resolver = spe.getSyncResolver(); Object crsValue; // value in the RowSet object Object resolverValue; // value in the SyncResolver object Object resolvedValue; // value to be persisted while (resolver.nextConflict()) { if (resolver.getStatus() == SyncResolver.INSERT_ROW_CONFLICT) { int row = resolver.getRow(); crs.absolute(row); int colCount = crs.getMetaData().getColumnCount(); for (int j = 1; j <= colCount; j++) { if (resolver.getConflictValue(j) != null) { crsValue = crs.getObject(j); resolverValue = resolver.getConflictValue(j); // Compare crsValue and resolverValue to determine // which should be the resolved value (the value to persist) // // This example choses the value in the RowSet object, // crsValue, to persist., resolvedValue = crsValue; resolver.setResolvedValue(j, resolvedValue); } } } } } catch (SQLException sqle) { JDBCTutorialUtilities.printSQLException(sqle); } finally { if (crs != null) crs.close(); this.con.setAutoCommit(true); } }
From source file:com.egt.core.db.xdp.RecursoCachedRowSet.java
/** * {@inheritDoc}// w w w . j a v a 2s .c o m */ @Override public void acceptChanges() throws SyncProviderException { try { this.getFilasConflictivas().clear(); super.acceptChanges(); } catch (SyncProviderException spe) { SyncResolver resolver = spe.getSyncResolver(); if (resolver != null && resolver instanceof SyncResolverX) { boolean showDeleted = this.tryToGetShowDeleted(); boolean sinDuplicados = TLC.getBitacora().isSinDuplicados(); int conflictos = 0; int row; int status = SyncResolver.NO_ROW_CONFLICT; String transaction = DBUtils.getTransactionLabel(status); SQLException sqlException; int errorCode; String message; String localizedMessage; String sqlState; String heading; String clave = CBM2.COMMIT_CHANGES_UNKNOWN_EXCEPTION; int tipoError; /* OJO con la constante, 1 -> fila con un error desconocido */ String mensaje; String prefijo = Global.PREFIJO_ETIQUETA_ID_RECURSO; try { this.tryToSetShowDeleted(true); TLC.getBitacora().setSinDuplicados(true); while (resolver.nextConflict()) { conflictos++; row = resolver.getRow(); status = resolver.getStatus(); transaction = DBUtils.getTransactionLabel(status); sqlException = ((SyncResolverX) resolver).getSQLException(); errorCode = sqlException.getErrorCode(); message = sqlException.getMessage(); localizedMessage = StringUtils.substringBefore(sqlException.getLocalizedMessage(), " Where: "); sqlState = sqlException.getSQLState(); heading = "Row:" + row + ", Status:" + status + ", Code:" + errorCode + ", State:" + sqlState; Bitacora.trace(heading + message); if (TLC.getInterpreteSql().isCommandIgnoredException(sqlException)) { clave = CBM2.COMMIT_CHANGES_COMMAND_IGNORED_EXCEPTION; tipoError = 0; /* OJO con la constante, 0 -> fila "ignorada" (no se sabe si tiene o no errores) */ mensaje = tipoError + Bitacora.getTextoMensaje(clave, transaction, prefijo + row); TLC.getBitacora().error(CBM2.COMMIT_CHANGES_COMMAND_IGNORED); if (status == SyncResolver.DELETE_ROW_CONFLICT) { this.tryToSetShowDeleted(false); } } else { String columna = TLC.getInterpreteSql() .getNotNullConstraintViolationColumn(sqlException); if (StringUtils.isNotBlank(columna)) { clave = CBM2.DATABASE_NOT_NULL_CONSTRAINT_VIOLATION; tipoError = 2; /* OJO con la constante, 2 -> fila con un error conocido */ mensaje = tipoError + TLC.getBitacora().error(clave, transaction, prefijo + row, columna); } else { clave = DBUtils.getConstraintMessageKey(message, status); if (clave == null) { clave = CBM2.COMMIT_CHANGES_UNKNOWN_EXCEPTION; tipoError = 1; /* OJO con la constante, 1 -> fila con un error desconocido */ mensaje = tipoError + TLC.getBitacora().error(clave, transaction, prefijo + row, localizedMessage); } else { tipoError = 2; /* OJO con la constante, 2 -> fila con un error conocido */ mensaje = tipoError + TLC.getBitacora().error(clave, transaction, prefijo + row); } } } this.getFilasConflictivas().put(String.valueOf(row - 1), mensaje); // if (status == SyncResolver.DELETE_ROW_CONFLICT) // if (this.absolute(row)) // if (this.rowDeleted()) // RETORNA FALSO! // this.undoDelete(); } if (conflictos == 0) { /* esto parece un BUG, y pasa cuando se elimina la ultima fila y showDeleted==false */ TLC.getBitacora().error(clave, transaction, "", spe.getLocalizedMessage()); } } catch (SQLException ex) { TLC.getBitacora().fatal(ex); } finally { this.tryToSetShowDeleted(showDeleted); TLC.getBitacora().setSinDuplicados(sinDuplicados); throw new SyncProviderException(this.getSyncProviderExceptionString(spe)); } } throw spe; } }
From source file:ips1ap101.lib.core.db.xdp.RecursoCachedRowSet.java
/** * {@inheritDoc}/*from ww w. jav a2s . com*/ */ @Override public void acceptChanges() throws SyncProviderException { try { this.getFilasConflictivas().clear(); super.acceptChanges(); } catch (SyncProviderException spe) { SyncResolver resolver = spe.getSyncResolver(); if (resolver != null && resolver instanceof SyncResolverX) { boolean showDeleted = this.tryToGetShowDeleted(); boolean sinDuplicados = TLC.getBitacora().isSinDuplicados(); int conflictos = 0; int row; int status = SyncResolver.NO_ROW_CONFLICT; String transaction = DBUtils.getTransactionLabel(status); SQLException sqlException; int errorCode; String message; String localizedMessage; String sqlState; String clave = CBM.COMMIT_CHANGES_UNKNOWN_EXCEPTION; int tipoError; /* OJO con la constante, 1 -> fila con un error desconocido */ String mensaje; String prefijo = Global.PREFIJO_ETIQUETA_ID_RECURSO; try { this.tryToSetShowDeleted(true); TLC.getBitacora().setSinDuplicados(true); while (resolver.nextConflict()) { conflictos++; row = resolver.getRow(); status = resolver.getStatus(); transaction = DBUtils.getTransactionLabel(status); sqlException = ((SyncResolverX) resolver).getSQLException(); errorCode = sqlException.getErrorCode(); message = sqlException.getMessage(); localizedMessage = DBUtils.getProperErrorMessage(sqlException.getLocalizedMessage()); sqlState = sqlException.getSQLState(); Bitacora.trace("SQLException Message = " + message); Bitacora.trace("SQLException Localized Message = " + localizedMessage); Bitacora.trace("SQLException Row = " + row + ", Status = " + status + ", Code = " + errorCode + ", State = " + sqlState); if (TLC.getInterpreteSql().isCommandIgnoredException(sqlException)) { clave = CBM.COMMIT_CHANGES_COMMAND_IGNORED_EXCEPTION; tipoError = 0; /* OJO con la constante, 0 -> fila "ignorada" (no se sabe si tiene o no errores) */ mensaje = tipoError + Bitacora.getTextoMensaje(clave, transaction, prefijo + row); TLC.getBitacora().error(CBM.COMMIT_CHANGES_COMMAND_IGNORED); if (status == SyncResolver.DELETE_ROW_CONFLICT) { this.tryToSetShowDeleted(false); } } else { String columna = TLC.getInterpreteSql() .getNotNullConstraintViolationColumn(sqlException); if (StringUtils.isNotBlank(columna)) { clave = CBM.DATABASE_NOT_NULL_CONSTRAINT_VIOLATION; tipoError = 2; /* OJO con la constante, 2 -> fila con un error conocido */ mensaje = tipoError + TLC.getBitacora().error(clave, transaction, prefijo + row, columna); } else { clave = DBUtils.getConstraintMessageKey(message, status); if (clave == null) { clave = CBM.COMMIT_CHANGES_UNKNOWN_EXCEPTION; tipoError = 1; /* OJO con la constante, 1 -> fila con un error desconocido */ mensaje = tipoError + TLC.getBitacora().error(clave, transaction, prefijo + row, localizedMessage); } else { tipoError = 2; /* OJO con la constante, 2 -> fila con un error conocido */ mensaje = tipoError + TLC.getBitacora().error(clave, transaction, prefijo + row); } } } this.getFilasConflictivas().put(String.valueOf(row - 1), mensaje); // if (status == SyncResolver.DELETE_ROW_CONFLICT) // if (this.absolute(row)) // if (this.rowDeleted()) // RETORNA FALSO! // this.undoDelete(); } if (conflictos == 0) { /* esto parece un BUG, y pasa cuando se elimina la ultima fila y showDeleted==false */ TLC.getBitacora().error(clave, transaction, "", spe.getLocalizedMessage()); } } catch (SQLException ex) { TLC.getBitacora().fatal(ex); } finally { this.tryToSetShowDeleted(showDeleted); TLC.getBitacora().setSinDuplicados(sinDuplicados); throw new SyncProviderException(this.getSyncProviderExceptionString(spe)); } } throw spe; } }