Multi Sql Begin, database data protection

Asked

Viewed 86 times

1

In my application I use BEGIN;, COMMIT;, ROLLBACK;, Try, Catch:

Example:

try{
    $this->execute('BEGIN;');
        // CODIGO COM VALIDAÇÕES
    $this->execute('COMMIT;');
}catch(Exception $e){
    $this->execute('ROLLBACK;');
}

It turns out that for test purposes I choose a flag that activates the BEGIN;, ROLLBACK; Global

Example

function __construct(){
    if($this->transaction_rollback === true){
        $this->Execute('BEGIN;');
    }
}

function __destruct(){
    if($this->transaction_rollback === true){
        $this->Execute('ROLLBACK;');
    }
}

Summary

BEGIN;
    BEGIN;    //there is already a transaction in progress
        // INSERT 
    COMMIT;
ROLLBACK;     //there is no transaction in progress

Situation

I use postgreSql and by the tests it does not have support to multi BEGIN;, researching a landing also did not find other banks that would support this situation.

Question

Would have some other way to protect the bank for testing purposes?

  • Are you by any chance confusing "multiples Begin" with savepoint ? -> http://www.postgresql.org/docs/8.1/static/sql-savepoint.html

1 answer

1


Solution

I have created methods that will execute the BEGIN;, COMMIT;, ROLLBACK;

Example

function begin(){
    $this->Execute('BEGIN;');
}

function commit(){
    $this->Execute('COMMIT;');
}

function rollback(){
    $this->Execute('ROLLBACK;');
}

however in commit I made the change to include the flag transaction_rollback, getting

function commit(){
    if($this->transaction_rollback === true){ 
        $this->rollback(); 
        return;
    }
    $this->Execute('COMMIT;');
}

Summary

BEGIN;
    // INSERT 
ROLLBACK;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.