下面列出了java.sql.SQLException#setNextException ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void test04() {
SQLException ex = new SyncFactoryException("Exception 1");
ex.initCause(t1);
SyncFactoryException ex1 = new SyncFactoryException("Exception 2");
SyncFactoryException ex2 = new SyncFactoryException("Exception 3");
ex2.initCause(t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
while (ex != null) {
assertTrue(msgs[num++].equals(ex.getMessage()));
Throwable c = ex.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
ex = ex.getNextException();
}
}
@Test
public void test06() {
SQLException ex = new SyncProviderException("Exception 1");
ex.initCause(t1);
SyncProviderException ex1 = new SyncProviderException("Exception 2");
SyncProviderException ex2 = new SyncProviderException("Exception 3");
ex2.initCause(t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
while (ex != null) {
assertTrue(msgs[num++].equals(ex.getMessage()));
Throwable c = ex.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
ex = ex.getNextException();
}
}
@Test
public void test04() {
SQLException ex = new SerialException("Exception 1");
ex.initCause(t1);
SerialException ex1 = new SerialException("Exception 2");
SerialException ex2 = new SerialException("Exception 3");
ex2.initCause(t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
while (ex != null) {
assertTrue(msgs[num++].equals(ex.getMessage()));
Throwable c = ex.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
ex = ex.getNextException();
}
}
@Test
public void test06() {
SQLException ex = new SyncProviderException("Exception 1");
ex.initCause(t1);
SyncProviderException ex1 = new SyncProviderException("Exception 2");
SyncProviderException ex2 = new SyncProviderException("Exception 3");
ex2.initCause(t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
while (ex != null) {
assertTrue(msgs[num++].equals(ex.getMessage()));
Throwable c = ex.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
ex = ex.getNextException();
}
}
@Test
public void test06() {
SQLException ex = new SyncProviderException("Exception 1");
ex.initCause(t1);
SyncProviderException ex1 = new SyncProviderException("Exception 2");
SyncProviderException ex2 = new SyncProviderException("Exception 3");
ex2.initCause(t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
while (ex != null) {
assertTrue(msgs[num++].equals(ex.getMessage()));
Throwable c = ex.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
ex = ex.getNextException();
}
}
/**
* Validate that the ordering of the returned Exceptions is correct
* using traditional while loop
*/
@Test
public void test12() {
SQLException ex = new SQLException("Exception 1", t1);
SQLException ex1 = new SQLException("Exception 2");
SQLException ex2 = new SQLException("Exception 3", t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
while (ex != null) {
assertTrue(msgs[num++].equals(ex.getMessage()));
Throwable c = ex.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
ex = ex.getNextException();
}
}
/**
* Validate that the ordering of the returned Exceptions is correct
* using traditional while loop
*/
@Test
public void test12() {
SQLException ex = new SQLException("Exception 1", t1);
SQLException ex1 = new SQLException("Exception 2");
SQLException ex2 = new SQLException("Exception 3", t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
while (ex != null) {
assertTrue(msgs[num++].equals(ex.getMessage()));
Throwable c = ex.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
ex = ex.getNextException();
}
}
@Test
public void test04() {
SQLException ex = new SyncFactoryException("Exception 1");
ex.initCause(t1);
SyncFactoryException ex1 = new SyncFactoryException("Exception 2");
SyncFactoryException ex2 = new SyncFactoryException("Exception 3");
ex2.initCause(t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
while (ex != null) {
assertTrue(msgs[num++].equals(ex.getMessage()));
Throwable c = ex.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
ex = ex.getNextException();
}
}
@Test
public void test04() {
SQLException ex = new SyncFactoryException("Exception 1");
ex.initCause(t1);
SyncFactoryException ex1 = new SyncFactoryException("Exception 2");
SyncFactoryException ex2 = new SyncFactoryException("Exception 3");
ex2.initCause(t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
while (ex != null) {
assertTrue(msgs[num++].equals(ex.getMessage()));
Throwable c = ex.getCause();
while (c != null) {
assertTrue(msgs[num++].equals(c.getMessage()));
c = c.getCause();
}
ex = ex.getNextException();
}
}
@Test
public void testCreateHopDatabaseBatchExceptionConstructsExceptionList() {
BatchUpdateException root = new BatchUpdateException();
SQLException next = new SQLException();
SQLException next2 = new SQLException();
root.setNextException( next );
next.setNextException( next2 );
List<Exception> exceptionList = Database.createHopDatabaseBatchException( "", root ).getExceptionsList();
assertEquals( 2, exceptionList.size() );
assertEquals( next, exceptionList.get( 0 ) );
assertEquals( next2, exceptionList.get( 1 ) );
}
/**
* issue a rollback when SQLException se occurs. If SQLException ouccurs when
* rollback, the new SQLException will be added into the chain of se.
*/
private static void rollBackAndThrowSQLException(Connection conn,
SQLException se) throws SQLException {
try {
conn.rollback();
} catch (SQLException e) {
se.setNextException(e);
}
throw se;
}
/**
* issue a rollback when SQLException se occurs. If SQLException ouccurs when
* rollback, the new SQLException will be added into the chain of se.
*/
private static void rollBackAndThrowSQLException(Connection conn,
SQLException se) throws SQLException {
try {
conn.rollback();
} catch (SQLException e) {
se.setNextException(e);
}
throw se;
}
/**
* Rolls up SQL exceptions by taking each proceeding exception
* and making it a child of the previous using the <code>setNextException</code>
* method of SQLException.
*/
public static SQLException rollupSqlExceptions(Collection<SQLException> exceptions) {
SQLException parent = null;
for (SQLException exception : exceptions) {
if (parent != null) {
exception.setNextException(parent);
}
parent = exception;
}
return parent;
}
/**
* Validate that the ordering of the returned Exceptions is correct
* using for-each loop
*/
@Test
public void test11() {
SQLException ex = new SQLException("Exception 1", t1);
SQLException ex1 = new SQLException("Exception 2");
SQLException ex2 = new SQLException("Exception 3", t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
for (Throwable e : ex) {
assertTrue(msgs[num++].equals(e.getMessage()));
}
}
/**
* Validate that the ordering of the returned Exceptions is correct
* using for-each loop
*/
@Test
public void test11() {
SQLException ex = new SQLException("Exception 1", t1);
SQLException ex1 = new SQLException("Exception 2");
SQLException ex2 = new SQLException("Exception 3", t2);
ex.setNextException(ex1);
ex.setNextException(ex2);
int num = 0;
for (Throwable e : ex) {
assertTrue(msgs[num++].equals(e.getMessage()));
}
}
/**
* Execute INSERT statement plan.
* @param cursor Cursor to take inserted data from.
* @param pageSize Batch size for streaming, anything <= 0 for single page operations.
* @return Number of items affected.
* @throws IgniteCheckedException if failed, particularly in case of duplicate keys.
*/
@SuppressWarnings({"unchecked"})
private static long dmlDoInsert(UpdatePlan plan, Iterable<List<?>> cursor, int pageSize) throws IgniteCheckedException {
GridCacheContext cctx = plan.cacheContext();
// If we have just one item to put, just do so
if (plan.rowCount() == 1) {
IgniteBiTuple t = plan.processRow(cursor.iterator().next());
if (cctx.cache().putIfAbsent(t.getKey(), t.getValue()))
return 1;
else
throw new TransactionDuplicateKeyException("Duplicate key during INSERT [key=" + t.getKey() + ']');
}
else {
// Keys that failed to INSERT due to duplication.
DmlBatchSender sender = new DmlBatchSender(cctx, pageSize, 1);
for (List<?> row : cursor) {
final IgniteBiTuple keyValPair = plan.processRow(row);
sender.add(keyValPair.getKey(), new DmlStatementsProcessor.InsertEntryProcessor(keyValPair.getValue()), 0);
}
sender.flush();
SQLException resEx = sender.error();
if (!F.isEmpty(sender.failedKeys())) {
String msg = "Failed to INSERT some keys because they are already in cache " +
"[keys=" + sender.failedKeys() + ']';
SQLException dupEx = new SQLException(msg, SqlStateCode.CONSTRAINT_VIOLATION);
if (resEx == null)
resEx = dupEx;
else
resEx.setNextException(dupEx);
}
if (resEx != null)
throw new IgniteSQLException(resEx);
return sender.updateCount();
}
}
void clearResultSets(boolean isPreparedBatch) throws SQLException {
// GemStone changes END
SQLException sqle = null;
try {
// Are there results?
// outside of the lower try/finally since results will
// setup and restore themselves.
// GemStone changes BEGIN
final java.sql.ResultSet results = this.results;
if (results != null) {
results.close();
setResults(null);
/* (original code)
if (results != null) {
results.close();
results = null;
*/
// GemStone changes END
}
} catch (SQLException s1) {
sqle = s1;
}
try {
// GemStone changes BEGIN
if (!isPreparedBatch)
// GemStone changes END
if (autoGeneratedKeysResultSet != null) {
autoGeneratedKeysResultSet.close();
autoGeneratedKeysResultSet = null;
}
} catch (SQLException sauto) {
if (sqle == null)
sqle = sauto;
else
sqle.setNextException(sauto);
}
// close all the dynamic result sets.
if (dynamicResults != null) {
for (int i = 0; i < dynamicResults.length; i++) {
java.sql.ResultSet lrs = dynamicResults[i];
if (lrs == null)
continue;
try {
lrs.close();
} catch (SQLException sdynamic) {
if (sqle == null)
sqle = sdynamic;
else
sqle.setNextException(sdynamic);
}
}
dynamicResults = null;
}
/*
We don't reset statement to null because PreparedStatement
relies on it being there for subsequent (post-close) execution
requests. There is no close method on database statement objects.
*/
updateCount = -1; // reset field
if (sqle != null)
throw sqle;
}
/**
* JDBC 2.0
* <p/>
* Submit a batch of commands to the database for execution.
* This method is optional.
* <p/>
* Moving jdbc2.0 batch related code in this class because
* callableStatement in jdbc 20 needs this code too and it doesn't derive
* from prepared statement in jdbc 20 in our implementation.
* BatchUpdateException is the only new class from jdbc 20 which is being
* referenced here and in order to avoid any jdk11x problems, using
* reflection code to make an instance of that class.
*
* @return an array of update counts containing one element for each
* command in the batch. The array is ordered according
* to the order in which commands were inserted into the batch
* @throws SQLException if a database-access error occurs, or the
* driver does not support batch statements
*/
@Override
public int[] executeBatch() throws SQLException {
checkExecStatus();
synchronized (getConnectionSynchronization()) {
setupContextStack();
int i = 0;
// As per the jdbc 2.0 specs, close the statement object's current resultset
// if one is open.
// Are there results?
// outside of the lower try/finally since results will
// setup and restore themselves.
clearResultSets();
Vector stmts = batchStatements;
batchStatements = null;
int size;
if (stmts == null)
size = 0;
else
size = stmts.size();
int[] returnUpdateCountForBatch = new int[size];
SQLException sqle;
try {
for (; i < size; i++) {
// If we saw an interrupt, stop execution of batch now.
// throwIf will likely only throw after at least one stm
// has been executed, since first time around we probably
// didn't do anything to notice interrupts yet.
InterruptStatus.throwIf(lcc);
if (executeBatchElement(stmts.get(i)))
throw newSQLException(SQLState.RESULTSET_RETURN_NOT_ALLOWED);
returnUpdateCountForBatch[i] = getUpdateCount();
}
InterruptStatus.restoreIntrFlagIfSeen(lcc);
return returnUpdateCountForBatch;
} catch (StandardException se) {
sqle = handleException(se);
} catch (SQLException sqle2) {
sqle = sqle2;
} finally {
restoreContextStack();
}
int successfulUpdateCount[] = new int[i];
System.arraycopy(returnUpdateCountForBatch, 0, successfulUpdateCount, 0, i);
SQLException batch =
new java.sql.BatchUpdateException(sqle.getMessage(), sqle.getSQLState(),
sqle.getErrorCode(), successfulUpdateCount, sqle);
batch.setNextException(sqle);
throw batch;
}
}
/**
* Perform DELETE operation on top of results of SELECT.
*
* @param cctx Cache context.
* @param cursor SELECT results.
* @param pageSize Batch size for streaming, anything <= 0 for single page operations.
* @return Results of DELETE (number of items affected AND keys that failed to be updated).
*/
private static UpdateResult doDelete(GridCacheContext cctx, Iterable<List<?>> cursor, int pageSize)
throws IgniteCheckedException {
DmlBatchSender sender = new DmlBatchSender(cctx, pageSize, 1);
for (List<?> row : cursor) {
if (row.size() != 2)
continue;
Object key = row.get(0);
ClusterNode node = sender.primaryNodeByKey(key);
IgniteInClosure<MutableEntry<Object, Object>> rmvC =
DmlStatementsProcessor.getRemoveClosure(node, key);
sender.add(
key,
new DmlStatementsProcessor.ModifyingEntryProcessor(row.get(1), rmvC),
0
);
}
sender.flush();
SQLException resEx = sender.error();
if (resEx != null) {
if (!F.isEmpty(sender.failedKeys())) {
// Don't go for a re-run if processing of some keys yielded exceptions and report keys that
// had been modified concurrently right away.
String msg = "Failed to DELETE some keys because they had been modified concurrently " +
"[keys=" + sender.failedKeys() + ']';
SQLException conEx = createJdbcSqlException(msg, IgniteQueryErrorCode.CONCURRENT_UPDATE);
conEx.setNextException(resEx);
resEx = conEx;
}
throw new IgniteSQLException(resEx);
}
return new UpdateResult(sender.updateCount(), sender.failedKeys().toArray(),
cursor instanceof QueryCursorImpl ? ((QueryCursorImpl) cursor).partitionResult() : null);
}
/**
* Convert this SqlException into a java.sql.SQLException
*/
// GemStone changes BEGIN
public SQLException getSQLException(final Agent agent)
/* (original code)
public SQLException getSQLException()
*/
// GemStone changes END
{
if ( wrappedException_ != null )
{
return wrappedException_;
}
// When we have support for JDBC 4 SQLException subclasses, this is
// where we decide which exception to create
// GemStone changes BEGIN
// fetching exception should not change inUnitOfWork (#44311)
Connection conn = null;
boolean savedInUnitOfWork = false;
if (agent != null) {
if ((conn = agent.connection_) != null) {
savedInUnitOfWork = conn.inUnitOfWork_;
}
}
final SQLException sqle = ClientExceptionUtil.newSQLException(
getMessage(), getSQLState(), getErrorCode(), this);
/* (original code)
SQLException sqle = exceptionFactory.getSQLException(getMessage(), getSQLState(),
getErrorCode());
sqle.initCause(this);
*/
// GemStone changes END
// Set up the nextException chain
if ( nextException_ != null )
{
// The exception chain gets constructed automatically through
// the beautiful power of recursion
sqle.setNextException(nextException_.getSQLException(
null /* GemStoneAddition */));
}
// GemStone changes BEGIN
// restore inUnitOfWork
if (conn != null) {
conn.setInUnitOfWork(savedInUnitOfWork);
}
// GemStone changes END
return sqle;
}