Search in sources :

Example 1 with OracleCallableStatement

use of oracle.jdbc.internal.OracleCallableStatement in project symmetric-ds by JumpMind.

the class OracleBulkDatabaseWriter method flush.

protected void flush() {
    statistics.get(batch).startTimer(DataWriterStatisticConstants.DATABASEMILLIS);
    try {
        if (rowArrays.size() > 0) {
            JdbcSqlTransaction jdbcTransaction = (JdbcSqlTransaction) transaction;
            Connection c = jdbcTransaction.getConnection();
            Connection oracleConnection = jdbcExtractor.getNativeConnection(c);
            Column[] columns = targetTable.getColumns();
            StringBuilder questions = new StringBuilder();
            for (int i = 0; i <= columns.length; i++) {
                questions.append("?, ");
            }
            questions.replace(questions.length() - 2, questions.length(), "");
            String sql = String.format("{ call %s(%s) }", buildProcedureName("i", targetTable), questions);
            OracleCallableStatement stmt = (OracleCallableStatement) oracleConnection.prepareCall(sql);
            for (int i = 0; i < columns.length; i++) {
                Column column = columns[i];
                ArrayDescriptor type = ArrayDescriptor.createDescriptor(getTypeName(column.getMappedTypeCode()), oracleConnection);
                List<Object> columnData = rowArrays.get(i);
                ARRAY array = new ARRAY(type, oracleConnection, columnData.toArray(new Object[columnData.size()]));
                stmt.setObject(i + 1, array);
            }
            int errorIndex = columns.length + 1;
            stmt.registerOutParameter(errorIndex, OracleTypes.ARRAY, getTypeName(Types.INTEGER));
            stmt.execute();
            ARRAY errorsArray = stmt.getARRAY(errorIndex);
            int[] errors;
            if (errorsArray != null) {
                errors = errorsArray.getIntArray();
            } else {
                errors = new int[0];
            }
            try {
                stmt.close();
            } catch (SQLException e) {
                log.info("Unable to close the prepared statment after user.", e);
            }
            if (errors.length > 0) {
                // set the statement count so the failed row number get reported correctly
                statistics.get(batch).set(DataWriterStatisticConstants.STATEMENTCOUNT, errors[0]);
                throw new BulkSqlException(errors, lastEventType.toString(), sql);
            }
        }
    } catch (SQLException ex) {
        StringBuilder failureMessage = new StringBuilder();
        failureMessage.append("Failed to flush the following data set from batch ");
        failureMessage.append(batch.getNodeBatchId());
        failureMessage.append(".  The last flushed line number of the batch was ");
        failureMessage.append(statistics.get(batch).get(DataWriterStatisticConstants.LINENUMBER));
        failureMessage.append("\n");
        for (List<Object> row : rowArrays) {
            failureMessage.append(StringUtils.abbreviate(Arrays.toString(row.toArray(new Object[row.size()])), CsvData.MAX_DATA_SIZE_TO_PRINT_TO_LOG));
            failureMessage.append("\n");
        }
        log.info(failureMessage.toString());
        throw platform.getSqlTemplate().translate(ex);
    } finally {
        lastEventType = null;
        rowArrays.clear();
        statistics.get(batch).stopTimer(DataWriterStatisticConstants.DATABASEMILLIS);
    }
}
Also used : ARRAY(oracle.sql.ARRAY) SQLException(java.sql.SQLException) Connection(java.sql.Connection) OracleCallableStatement(oracle.jdbc.internal.OracleCallableStatement) BulkSqlException(org.jumpmind.db.sql.BulkSqlException) Column(org.jumpmind.db.model.Column) ArrayDescriptor(oracle.sql.ArrayDescriptor) JdbcSqlTransaction(org.jumpmind.db.sql.JdbcSqlTransaction) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 OracleCallableStatement (oracle.jdbc.internal.OracleCallableStatement)1 ARRAY (oracle.sql.ARRAY)1 ArrayDescriptor (oracle.sql.ArrayDescriptor)1 Column (org.jumpmind.db.model.Column)1 BulkSqlException (org.jumpmind.db.sql.BulkSqlException)1 JdbcSqlTransaction (org.jumpmind.db.sql.JdbcSqlTransaction)1