Search in sources :

Example 16 with BatchUpdateException

use of java.sql.BatchUpdateException in project jdk8u_jdk by JetBrains.

the class BatchUpdateExceptionTests method test11.

/**
     * Validate that if null is specified for the update count, it is returned
     * as null
     */
@Test
public void test11() {
    BatchUpdateException ex = new BatchUpdateException((int[]) null);
    assertTrue(ex.getMessage() == null && ex.getSQLState() == null && ex.getErrorCode() == 0 && ex.getUpdateCounts() == null && ex.getLargeUpdateCounts() == null);
}
Also used : BatchUpdateException(java.sql.BatchUpdateException) SerializedBatchUpdateException(util.SerializedBatchUpdateException) Test(org.testng.annotations.Test) BaseTest(util.BaseTest)

Example 17 with BatchUpdateException

use of java.sql.BatchUpdateException in project spf4j by zolyfarkas.

the class ThrowablesTest method testChain2.

@Test
@SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
public void testChain2() {
    Throwable t = new RuntimeException("bla1", new BatchUpdateException("Sql bla", "ORA-500", 500, new int[] { 1, 2 }, new RuntimeException("la la")));
    Throwable newRootCause = new TimeoutException("Booo");
    Throwable result = Throwables.chain(t, newRootCause);
    LOG.debug("Thowable string = {}", Throwables.toString(result));
    Assert.assertArrayEquals(new int[] { 1, 2 }, ((BatchUpdateException) result.getCause()).getUpdateCounts());
    Assert.assertEquals(newRootCause, com.google.common.base.Throwables.getRootCause(result));
    Assert.assertEquals(4, com.google.common.base.Throwables.getCausalChain(result).size());
}
Also used : BatchUpdateException(java.sql.BatchUpdateException) SocketTimeoutException(java.net.SocketTimeoutException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 18 with BatchUpdateException

use of java.sql.BatchUpdateException in project pentaho-kettle by pentaho.

the class Database method insertFinished.

/**
 * Close the prepared statement of the insert statement.
 *
 * @param ps             The prepared statement to empty and close.
 * @param batch          true if you are using batch processing (typically true for this method)
 * @param psBatchCounter The number of rows on the batch queue
 * @throws KettleDatabaseException
 * @deprecated use emptyAndCommit() instead (pass in the number of rows left in the batch)
 */
@Deprecated
public void insertFinished(PreparedStatement ps, boolean batch) throws KettleDatabaseException {
    boolean isBatchUpdate = false;
    try {
        if (ps != null) {
            if (!isAutoCommit()) {
                // Execute the batch or just perform a commit.
                if (batch && getDatabaseMetaData().supportsBatchUpdates()) {
                    // The problem with the batch counters is that you can't just
                    // execute the current batch.
                    // Certain databases have a problem if you execute the batch and if
                    // there are no statements in it.
                    // You can't just catch the exception either because you would have
                    // to roll back on certain databases before you can then continue to
                    // do anything.
                    // That leaves the task of keeping track of the number of rows up to
                    // our responsibility.
                    isBatchUpdate = true;
                    ps.executeBatch();
                    commit();
                } else {
                    commit();
                }
            }
            // Let's not forget to close the prepared statement.
            // 
            ps.close();
        }
    } catch (BatchUpdateException ex) {
        throw createKettleDatabaseBatchException("Error updating batch", ex);
    } catch (SQLException ex) {
        if (isBatchUpdate) {
            throw createKettleDatabaseBatchException("Error updating batch", ex);
        } else {
            throw new KettleDatabaseException("Unable to commit connection after having inserted rows.", ex);
        }
    }
}
Also used : SQLException(java.sql.SQLException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) BatchUpdateException(java.sql.BatchUpdateException)

Example 19 with BatchUpdateException

use of java.sql.BatchUpdateException in project pentaho-kettle by pentaho.

the class Database method insertRow.

/**
 * Insert a row into the database using a prepared statement that has all values set.
 *
 * @param ps           The prepared statement
 * @param batch        True if you want to use batch inserts (size = commit size)
 * @param handleCommit True if you want to handle the commit here after the commit size (False e.g. in case the step
 *                     handles this, see TableOutput)
 * @return true if the rows are safe: if batch of rows was sent to the database OR if a commit was done.
 * @throws KettleDatabaseException
 */
public boolean insertRow(PreparedStatement ps, boolean batch, boolean handleCommit) throws KettleDatabaseException {
    String debug = "insertRow start";
    boolean rowsAreSafe = false;
    boolean isBatchUpdate = false;
    try {
        // Unique connections and Batch inserts don't mix when you want to roll
        // back on certain databases.
        // That's why we disable the batch insert in that case.
        // 
        boolean useBatchInsert = getUseBatchInsert(batch);
        // 
        if (!isAutoCommit()) {
            if (useBatchInsert) {
                debug = "insertRow add batch";
                // Add the batch, but don't forget to run the batch
                ps.addBatch();
            } else {
                debug = "insertRow exec update";
                ps.executeUpdate();
            }
        } else {
            ps.executeUpdate();
        }
        written++;
        if (handleCommit) {
            // TableOutput step)
            if (!isAutoCommit() && (written % commitsize) == 0) {
                if (useBatchInsert) {
                    isBatchUpdate = true;
                    debug = "insertRow executeBatch commit";
                    ps.executeBatch();
                    commit();
                    ps.clearBatch();
                } else {
                    debug = "insertRow normal commit";
                    commit();
                }
                written = 0;
                rowsAreSafe = true;
            }
        }
        return rowsAreSafe;
    } catch (BatchUpdateException ex) {
        throw createKettleDatabaseBatchException("Error updating batch", ex);
    } catch (SQLException ex) {
        if (isBatchUpdate) {
            throw createKettleDatabaseBatchException("Error updating batch", ex);
        } else {
            throw new KettleDatabaseException("Error inserting/updating row", ex);
        }
    } catch (Exception e) {
        throw new KettleDatabaseException("Unexpected error inserting/updating row in part [" + debug + "]", e);
    }
}
Also used : SQLException(java.sql.SQLException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) KettleValueException(org.pentaho.di.core.exception.KettleValueException) BatchUpdateException(java.sql.BatchUpdateException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) SQLException(java.sql.SQLException) KettleDatabaseBatchException(org.pentaho.di.core.exception.KettleDatabaseBatchException) BatchUpdateException(java.sql.BatchUpdateException)

Example 20 with BatchUpdateException

use of java.sql.BatchUpdateException in project pentaho-kettle by pentaho.

the class DatabaseTest method testCreateKettleDatabaseBatchExceptionConstructsExceptionList.

@Test
public void testCreateKettleDatabaseBatchExceptionConstructsExceptionList() {
    BatchUpdateException root = new BatchUpdateException();
    SQLException next = new SQLException();
    SQLException next2 = new SQLException();
    root.setNextException(next);
    next.setNextException(next2);
    List<Exception> exceptionList = Database.createKettleDatabaseBatchException("", root).getExceptionsList();
    assertEquals(2, exceptionList.size());
    assertEquals(next, exceptionList.get(0));
    assertEquals(next2, exceptionList.get(1));
}
Also used : SQLException(java.sql.SQLException) NamingException(javax.naming.NamingException) BatchUpdateException(java.sql.BatchUpdateException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) SQLException(java.sql.SQLException) KettleDatabaseBatchException(org.pentaho.di.core.exception.KettleDatabaseBatchException) 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