Multi begin Sql, bank data protection

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 hedge a flag that activates 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 testing it does not support multi BEGIN; , investigating a landing I also did not find other banks that would support this situation.

Question

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

    
asked by anonymous 15.04.2015 / 16:14

1 answer

1

Solution

I have created methods that will execute 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 transaction_rollback flag, getting

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

Summary

BEGIN;
    // INSERT 
ROLLBACK;
    
17.04.2015 / 14:09