Database - transaction save point, batch updates

Transaction (transaction)
    When all statements are executed successfully, the transaction can be submitted to (commit). If a statement encounters an error, the transaction will be rolled back.
    The reason multiple commands into transactions is to ensure the integrity of the database (database integrity).
    By default, the database connection in auto-commit mode, SQL command to be executed once it is submitted to the database. Once the order is submitted, it could not be rollback.
   
    When using transactions, the need to turn off auto-commit mode conn.setAutoCommit (false);
    create a statement object Statement stat = conn.createStatement ();
    then arbitrarily called several times to executeUpdate method stat.executeUpdate (command1);
                                         stat.executeUpdate (command2) ;
    after executing all the commands, commit method is called to submit conn.commit ();
    if wrong, call rollback () appears rollback conn.rollback ();
   
savepoint
    using the save point, you can roll back only to when the rollback operation beginning point to save, rather than transactions.
    Statement stat = conn.createStatement (); // start a transaction, rollback () fallback transaction beginning
    stat.executeUpdate (command1);
    Savepoint svpt = conn.setSavepoint (); // set the save point, rollback (svpt) fall back to the point
    stat.executeUpdate (command2);
    ...
    conn.commit ();
    conn.releaseSavepoint (svpt); // when when not needed to save point, it must release the
   
batch update
    a sequence of commands at the same time be collected and submitted as a batch operation.
    In the same batch container may be INSERT, UPDATE, and DELETE operations, the command may be defined in the database, such as CREATE TABLE and DROP TABLE. However, adding the SELECT command in the batch processing will throw an exception.
   
    First batch processing required to create the Statement object
    Statement stat = conn.createStatement ();
    then call addBatch method:
    String the Command = "the CREATE TABLE ...";
    stat.addBatch (the Command);
    the while (...) {
        the Command = "INSERT ... VALUES INTO (...) ";
        stat.addBatch (the Command);
    }
    submit the entire batch updates
    int [] counts = stat.executeBatch () ; // Returns an array of a number of records of
   
    the operation to correct an error in batch processing mode, the batch must be performed as a single transaction. If the batch update fails, it must be rolled back to the state before the start of the batch operation.
    First, turn off the automatic submission mode and collecting a batch operation, the operation is executed and committed, and finally restore the initial auto commit mode:
    Boolean = conn.getAutoCommit the autoCommit ();
    conn.setAutoCommit (to false);
    the Statement conn.getStatement STAT = ();
    ...
    // the Keep Calling stat.addBatch (...);
    ...
    stat.executeBatch ();
    conn.commit ();
    conn.setAutoCommit (autoCommit);

Guess you like

Origin www.cnblogs.com/it-mh/p/11205533.html