Pascal Data Objects

procedure TPDO.rollBack;

Rolls back the current transaction, as initiated by TPDO.beginTransaction. It is an error to call this method if no transaction is active.

If the database was set to autocommit mode, this function will restore autocommit mode after it has rolled back the transaction.

Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction. The implicit COMMIT will prevent you from rolling back any other changes within the transaction boundary.

The following example is not that realistic, but it shows how a rollback can be used with a try-except block. PDO will not generate exceptions on bad queries, so these would have have to be raised manually.

   {Intentionally add bad UPDATE syntax}

    try
        db.beginTransaction;
        db.execute_now ('UPDATK callsigns SET callsign=''bravo'' WHERE userid = 3');  
        if (db.errorInfo.error_code > 0) then raise exception.create ('Bad Update');
        db.commit;
    except
        on E: Exception do begin
            writeln ('rolling back because: ' + E.message);
            db.rollBack;
        end;
    end;