Search in sources :

Example 1 with DBDInsertReplaceMethod

use of org.jkiss.dbeaver.model.data.DBDInsertReplaceMethod in project dbeaver by serge-rider.

the class DatabaseTransferConsumer method insertBatch.

private void insertBatch(boolean force) throws DBCException {
    if (isPreview) {
        return;
    }
    boolean needCommit = force || ((rowsExported % settings.getCommitAfterRows()) == 0);
    Map<String, Object> options = new HashMap<>();
    boolean disableUsingBatches = settings.isDisableUsingBatches();
    boolean onDuplicateKeyCaseOn = settings.getOnDuplicateKeyInsertMethodId() != null && !settings.getOnDuplicateKeyInsertMethodId().equals(DBSDataManipulator.INSERT_NONE_METHOD);
    options.put(DBSDataManipulator.OPTION_DISABLE_BATCHES, disableUsingBatches);
    if (onDuplicateKeyCaseOn) {
        String insertMethodId = settings.getOnDuplicateKeyInsertMethodId();
        SQLInsertReplaceMethodDescriptor insertReplaceMethod = SQLInsertReplaceMethodRegistry.getInstance().getInsertMethod(insertMethodId);
        if (insertReplaceMethod != null) {
            try {
                DBDInsertReplaceMethod insertMethod = insertReplaceMethod.createInsertMethod();
                options.put(DBSDataManipulator.OPTION_INSERT_REPLACE_METHOD, insertMethod);
            } catch (DBException e) {
                log.debug("Can't get insert replace method", e);
            }
        }
    }
    if ((needCommit || disableUsingBatches) && executeBatch != null) {
        targetSession.getProgressMonitor().subTask("Insert rows (" + rowsExported + ")");
        boolean retryInsert;
        do {
            retryInsert = false;
            try {
                executeBatch.execute(targetSession, options);
            } catch (Throwable e) {
                log.error("Error inserting row", e);
                if (!disableUsingBatches) {
                    DBWorkbench.getPlatformUI().showError("Error inserting row", "Data transfer failed during batch insert\n" + "(you can disable batch insert in order to skip particular rows).", e);
                    throw new DBCException("Can't insert row", e);
                } else {
                    if (!ignoreErrors) {
                        switch(DBWorkbench.getPlatformUI().showErrorStopRetryIgnore(DTMessages.database_transfer_consumer_task_error_occurred_during_data_load, e, true)) {
                            case STOP:
                                // just stop execution
                                throw new DBCException("Can't insert row", e);
                            case RETRY:
                                // do it again
                                retryInsert = true;
                                break;
                            case IGNORE:
                                // Just do nothing and go to the next row
                                retryInsert = false;
                                break;
                            case IGNORE_ALL:
                                ignoreErrors = true;
                                retryInsert = false;
                                break;
                        }
                    }
                }
            }
        } while (retryInsert);
    }
    if (settings.isUseTransactions() && needCommit) {
        DBCTransactionManager txnManager = DBUtils.getTransactionManager(targetSession.getExecutionContext());
        if (txnManager != null && txnManager.isSupportsTransactions() && !txnManager.isAutoCommit()) {
            targetSession.getProgressMonitor().subTask("Commit changes");
            txnManager.commit(targetSession);
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) SQLInsertReplaceMethodDescriptor(org.jkiss.dbeaver.model.sql.registry.SQLInsertReplaceMethodDescriptor) DBDInsertReplaceMethod(org.jkiss.dbeaver.model.data.DBDInsertReplaceMethod)

Example 2 with DBDInsertReplaceMethod

use of org.jkiss.dbeaver.model.data.DBDInsertReplaceMethod in project dbeaver by dbeaver.

the class DatabaseTransferConsumer method insertBatch.

private void insertBatch(boolean force) throws DBCException {
    if (isPreview) {
        return;
    }
    boolean needCommit = force || ((rowsExported % settings.getCommitAfterRows()) == 0);
    Map<String, Object> options = new HashMap<>();
    boolean disableUsingBatches = settings.isDisableUsingBatches();
    boolean onDuplicateKeyCaseOn = settings.getOnDuplicateKeyInsertMethodId() != null && !settings.getOnDuplicateKeyInsertMethodId().equals(DBSDataManipulator.INSERT_NONE_METHOD);
    options.put(DBSDataManipulator.OPTION_DISABLE_BATCHES, disableUsingBatches);
    if (onDuplicateKeyCaseOn) {
        String insertMethodId = settings.getOnDuplicateKeyInsertMethodId();
        SQLInsertReplaceMethodDescriptor insertReplaceMethod = SQLInsertReplaceMethodRegistry.getInstance().getInsertMethod(insertMethodId);
        if (insertReplaceMethod != null) {
            try {
                DBDInsertReplaceMethod insertMethod = insertReplaceMethod.createInsertMethod();
                options.put(DBSDataManipulator.OPTION_INSERT_REPLACE_METHOD, insertMethod);
            } catch (DBException e) {
                log.debug("Can't get insert replace method", e);
            }
        }
    }
    if ((needCommit || disableUsingBatches) && executeBatch != null) {
        targetSession.getProgressMonitor().subTask("Insert rows (" + rowsExported + ")");
        boolean retryInsert;
        do {
            retryInsert = false;
            try {
                executeBatch.execute(targetSession, options);
            } catch (Throwable e) {
                log.error("Error inserting row", e);
                if (!disableUsingBatches) {
                    DBWorkbench.getPlatformUI().showError("Error inserting row", "Data transfer failed during batch insert\n" + "(you can disable batch insert in order to skip particular rows).", e);
                    throw new DBCException("Can't insert row", e);
                } else {
                    if (!ignoreErrors) {
                        switch(DBWorkbench.getPlatformUI().showErrorStopRetryIgnore(DTMessages.database_transfer_consumer_task_error_occurred_during_data_load, e, true)) {
                            case STOP:
                                // just stop execution
                                throw new DBCException("Can't insert row", e);
                            case RETRY:
                                // do it again
                                retryInsert = true;
                                break;
                            case IGNORE:
                                // Just do nothing and go to the next row
                                retryInsert = false;
                                break;
                            case IGNORE_ALL:
                                ignoreErrors = true;
                                retryInsert = false;
                                break;
                        }
                    }
                }
            }
        } while (retryInsert);
    }
    if (settings.isUseTransactions() && needCommit) {
        DBCTransactionManager txnManager = DBUtils.getTransactionManager(targetSession.getExecutionContext());
        if (txnManager != null && txnManager.isSupportsTransactions() && !txnManager.isAutoCommit()) {
            targetSession.getProgressMonitor().subTask("Commit changes");
            txnManager.commit(targetSession);
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) SQLInsertReplaceMethodDescriptor(org.jkiss.dbeaver.model.sql.registry.SQLInsertReplaceMethodDescriptor) DBDInsertReplaceMethod(org.jkiss.dbeaver.model.data.DBDInsertReplaceMethod)

Aggregations

DBException (org.jkiss.dbeaver.DBException)2 DBDInsertReplaceMethod (org.jkiss.dbeaver.model.data.DBDInsertReplaceMethod)2 SQLInsertReplaceMethodDescriptor (org.jkiss.dbeaver.model.sql.registry.SQLInsertReplaceMethodDescriptor)2