Search in sources :

Example 51 with BatchUpdateException

use of java.sql.BatchUpdateException in project ignite by apache.

the class DmlUtils method doInsertBatched.

/**
 * Execute INSERT statement plan.
 *
 * @param plan Plan to execute.
 * @param cursor Cursor to take inserted data from. I.e. list of batch arguments for each query.
 * @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.
 */
private static List<UpdateResult> doInsertBatched(UpdatePlan plan, List<List<List<?>>> cursor, int pageSize) throws IgniteCheckedException {
    GridCacheContext cctx = plan.cacheContext();
    DmlBatchSender snd = new DmlBatchSender(cctx, pageSize, cursor.size());
    int rowNum = 0;
    SQLException resEx = null;
    for (List<List<?>> qryRow : cursor) {
        for (List<?> row : qryRow) {
            try {
                final IgniteBiTuple keyValPair = plan.processRow(row);
                snd.add(keyValPair.getKey(), new DmlStatementsProcessor.InsertEntryProcessor(keyValPair.getValue()), rowNum);
            } catch (Exception e) {
                String sqlState;
                int code;
                if (e instanceof IgniteSQLException) {
                    sqlState = ((IgniteSQLException) e).sqlState();
                    code = ((IgniteSQLException) e).statusCode();
                } else {
                    sqlState = SqlStateCode.INTERNAL_ERROR;
                    code = IgniteQueryErrorCode.UNKNOWN;
                }
                resEx = chainException(resEx, new SQLException(e.getMessage(), sqlState, code, e));
                snd.setFailed(rowNum);
            }
        }
        rowNum++;
    }
    try {
        snd.flush();
    } catch (Exception e) {
        resEx = chainException(resEx, new SQLException(e.getMessage(), SqlStateCode.INTERNAL_ERROR, IgniteQueryErrorCode.UNKNOWN, e));
    }
    resEx = chainException(resEx, snd.error());
    if (!F.isEmpty(snd.failedKeys())) {
        SQLException e = new SQLException("Failed to INSERT some keys because they are already in cache [keys=" + snd.failedKeys() + ']', SqlStateCode.CONSTRAINT_VIOLATION, DUPLICATE_KEY);
        resEx = chainException(resEx, e);
    }
    if (resEx != null) {
        BatchUpdateException e = new BatchUpdateException(resEx.getMessage(), resEx.getSQLState(), resEx.getErrorCode(), snd.perRowCounterAsArray(), resEx);
        throw new IgniteCheckedException(e);
    }
    int[] cntPerRow = snd.perRowCounterAsArray();
    List<UpdateResult> res = new ArrayList<>(cntPerRow.length);
    for (int i = 0; i < cntPerRow.length; i++) {
        int cnt = cntPerRow[i];
        res.add(new UpdateResult(cnt, X.EMPTY_OBJECT_ARRAY));
    }
    return res;
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ArrayList(java.util.ArrayList) BatchUpdateException(java.sql.BatchUpdateException) IgniteQueryErrorCode.createJdbcSqlException(org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode.createJdbcSqlException) SQLException(java.sql.SQLException) TransactionDuplicateKeyException(org.apache.ignite.transactions.TransactionDuplicateKeyException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) ArrayList(java.util.ArrayList) List(java.util.List) DmlStatementsProcessor(org.apache.ignite.internal.processors.query.h2.DmlStatementsProcessor) UpdateResult(org.apache.ignite.internal.processors.query.h2.UpdateResult) BatchUpdateException(java.sql.BatchUpdateException)

Example 52 with BatchUpdateException

use of java.sql.BatchUpdateException in project ignite by apache.

the class IgniteH2Indexing method executeUpdateDistributed.

/**
 * @param qryId Query id.
 * @param qryDesc Query descriptor.
 * @param qryParams Query parameters.
 * @param dml DML statement.
 * @param cancel Query cancel.
 * @return Update result wrapped into {@link GridQueryFieldsResult}
 * @throws IgniteCheckedException if failed.
 */
@SuppressWarnings("unchecked")
private List<QueryCursorImpl<List<?>>> executeUpdateDistributed(long qryId, QueryDescriptor qryDesc, QueryParameters qryParams, QueryParserResultDml dml, GridQueryCancel cancel) throws IgniteCheckedException {
    if (qryDesc.batched()) {
        Collection<UpdateResult> ress;
        List<Object[]> argss = qryParams.batchedArguments();
        UpdatePlan plan = dml.plan();
        GridCacheContext<?, ?> cctx = plan.cacheContext();
        // For MVCC case, let's enlist batch elements one by one.
        if (plan.hasRows() && plan.mode() == UpdateMode.INSERT && !cctx.mvccEnabled()) {
            CacheOperationContext opCtx = DmlUtils.setKeepBinaryContext(cctx);
            try {
                List<List<List<?>>> cur = plan.createRows(argss);
                // TODO: IGNITE-11176 - Need to support cancellation
                ress = DmlUtils.processSelectResultBatched(plan, cur, qryParams.updateBatchSize());
            } finally {
                DmlUtils.restoreKeepBinaryContext(cctx, opCtx);
            }
        } else {
            // Fallback to previous mode.
            ress = new ArrayList<>(argss.size());
            SQLException batchException = null;
            int[] cntPerRow = new int[argss.size()];
            int cntr = 0;
            for (Object[] args : argss) {
                UpdateResult res;
                try {
                    res = executeUpdate(qryId, qryDesc, qryParams.toSingleBatchedArguments(args), dml, false, null, cancel);
                    cntPerRow[cntr++] = (int) res.counter();
                    ress.add(res);
                } catch (Exception e) {
                    SQLException sqlEx = QueryUtils.toSqlException(e);
                    batchException = DmlUtils.chainException(batchException, sqlEx);
                    cntPerRow[cntr++] = Statement.EXECUTE_FAILED;
                }
            }
            if (batchException != null) {
                BatchUpdateException e = new BatchUpdateException(batchException.getMessage(), batchException.getSQLState(), batchException.getErrorCode(), cntPerRow, batchException);
                throw new IgniteCheckedException(e);
            }
        }
        ArrayList<QueryCursorImpl<List<?>>> resCurs = new ArrayList<>(ress.size());
        for (UpdateResult res : ress) {
            res.throwIfError();
            QueryCursorImpl<List<?>> resCur = (QueryCursorImpl<List<?>>) new QueryCursorImpl(singletonList(singletonList(res.counter())), cancel, false, false);
            resCur.fieldsMeta(UPDATE_RESULT_META);
            resCurs.add(resCur);
        }
        return resCurs;
    } else {
        UpdateResult res = executeUpdate(qryId, qryDesc, qryParams, dml, false, null, cancel);
        res.throwIfError();
        QueryCursorImpl<List<?>> resCur = (QueryCursorImpl<List<?>>) new QueryCursorImpl(singletonList(singletonList(res.counter())), cancel, false, false);
        resCur.fieldsMeta(UPDATE_RESULT_META);
        resCur.partitionResult(res.partitionResult());
        return singletonList(resCur);
    }
}
Also used : CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) BatchUpdateException(java.sql.BatchUpdateException) ClusterTopologyServerNotFoundException(org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteClusterReadOnlyException(org.apache.ignite.internal.processors.cache.distributed.dht.IgniteClusterReadOnlyException) CacheServerNotFoundException(org.apache.ignite.cache.CacheServerNotFoundException) SQLException(java.sql.SQLException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) UpdatePlan(org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan) BatchUpdateException(java.sql.BatchUpdateException)

Example 53 with BatchUpdateException

use of java.sql.BatchUpdateException in project dal by ctripcorp.

the class DalCommandTest method testThreeLayerExceptionTransaction.

// Three layer transaction throws exception
@Test
public void testThreeLayerExceptionTransaction() throws Exception {
    DalClient client = DalClientFactory.getClient(dbName);
    try {
        client.execute(new OneLayerExceptionDalCommand(), new DalHints());
        Assert.fail();
    } catch (Throwable e) {
        System.out.println(e.getMessage());
        Assert.assertTrue(e instanceof BatchUpdateException);
        Assert.assertTrue(e.getMessage().equals("Data truncation: Data too long for column 'Name' at row 1"));
        Assert.assertTrue(isCurrentTransactionNull());
    }
}
Also used : DalClient(com.ctrip.platform.dal.dao.DalClient) DalHints(com.ctrip.platform.dal.dao.DalHints) OneLayerExceptionDalCommand(com.ctrip.platform.dal.dao.client.DalCommand.nesting.normal.OneLayerExceptionDalCommand) BatchUpdateException(java.sql.BatchUpdateException) Test(org.junit.Test)

Example 54 with BatchUpdateException

use of java.sql.BatchUpdateException in project dal by ctripcorp.

the class DalCommandTest method testBatchOperationThrowExceptionInDalCommand.

// Two layer transaction with exception which should throws actual exception
@Test
public void testBatchOperationThrowExceptionInDalCommand() throws Exception {
    DalClient client = DalClientFactory.getClient(dbName);
    try {
        client.execute(new ThrowExceptionDalCommand(), new DalHints());
        Assert.fail();
    } catch (Throwable e) {
        System.out.println(e.getMessage());
        Assert.assertTrue(e instanceof BatchUpdateException);
        Assert.assertTrue(e.getMessage().equals("Data truncation: Data too long for column 'Name' at row 1"));
        Assert.assertTrue(isCurrentTransactionNull());
    }
}
Also used : DalClient(com.ctrip.platform.dal.dao.DalClient) DalHints(com.ctrip.platform.dal.dao.DalHints) ThrowExceptionDalCommand(com.ctrip.platform.dal.dao.client.DalCommand.ThrowExceptionDalCommand) BatchUpdateException(java.sql.BatchUpdateException) Test(org.junit.Test)

Example 55 with BatchUpdateException

use of java.sql.BatchUpdateException in project dal by ctripcorp.

the class DalCommandTest method testFourLayerExceptionTransaction.

// Four layer transaction throws exception
@Test
public void testFourLayerExceptionTransaction() throws Exception {
    DalClient client = DalClientFactory.getClient(dbName);
    try {
        client.execute(new TwoLayerExceptionDalCommand(), new DalHints());
        Assert.fail();
    } catch (Throwable e) {
        System.out.println(e.getMessage());
        Assert.assertTrue(e instanceof BatchUpdateException);
        Assert.assertTrue(e.getMessage().equals("Data truncation: Data too long for column 'Name' at row 1"));
        Assert.assertTrue(isCurrentTransactionNull());
    }
}
Also used : DalClient(com.ctrip.platform.dal.dao.DalClient) DalHints(com.ctrip.platform.dal.dao.DalHints) TwoLayerExceptionDalCommand(com.ctrip.platform.dal.dao.client.DalCommand.nesting.normal.TwoLayerExceptionDalCommand) BatchUpdateException(java.sql.BatchUpdateException) Test(org.junit.Test)

Aggregations

BatchUpdateException (java.sql.BatchUpdateException)103 SQLException (java.sql.SQLException)39 PreparedStatement (java.sql.PreparedStatement)33 Statement (java.sql.Statement)22 ArrayList (java.util.ArrayList)19 Test (org.junit.Test)19 Connection (java.sql.Connection)17 Test (org.testng.annotations.Test)17 BaseTest (util.BaseTest)17 SerializedBatchUpdateException (util.SerializedBatchUpdateException)17 ResultSet (java.sql.ResultSet)13 List (java.util.List)12 CallableStatement (java.sql.CallableStatement)8 HashSet (java.util.HashSet)8 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)7 HashMap (java.util.HashMap)6 Map (java.util.Map)5 CustomChangeException (liquibase.exception.CustomChangeException)5 DatabaseException (liquibase.exception.DatabaseException)5 SetupException (liquibase.exception.SetupException)5