php - How to create a new transaction scope in propel -
we using propel orm on mysql db. changing several tables , call external services during transaction. in between log these actions , response in logging table. if error occurs, revert actions want keep log messages. @ moment log messages using same transaction scope , reverted transaction.
do new connection , transactionscope with
$con = propel::getconnection(database_name);
or have check if same connection returned
pseudo code
public function write_log() { $con = propel::getconnection(database_name); $log=new log(); $log->message('foo'); $log->save($con); } public function change_data() { write_log('start'); $con = propel::getconnection(database_name); $con->begintransaction(); try { //this message should stay in database write_log('change_db_data:'.$new_db_value); //this should reverted change_db_data($new_db_value); write_log('call webservice_1'); $response=call_webservice_1(); write_log($response); if($response==null) { $con->rollback(); } write_log('call webservice_2'); $response=call_webservice_2(); write_log($response); if($response==null) { $con->rollback(); } $con->commit(); } catch(exception $e){ $con->rollback(); write_log('error') } write_log('end'); }
good choice picking propel. have 2 choices, either encapsulate logging in own transactions, or use nested transaction, uniquely supported propel.
the first requires transactionality in write_log function:
public function write_log() { $con = propel::getconnection(database_name); $con->begintransaction(); $log=new log(); $log->message('foo'); $log->save($con); $con->commit(); }
the second start nested transaction , ensure inner transaction rolled back:
public function write_log() { $con = propel::getconnection(database_name); $log=new log(); $log->message('foo'); $log->save($con); } public function change_data() { $con = propel::getconnection(database_name); $con->begintransaction(); write_log('start'); $con->begintransaction(); try { //this message should stay in database write_log('change_db_data:'.$new_db_value); //this should reverted change_db_data($new_db_value); write_log('call webservice_1'); $response=call_webservice_1(); write_log($response); if($response==null) { throw new \exception('null response.'); } write_log('call webservice_2'); $response=call_webservice_2(); write_log($response); if($response==null) { throw new \exception('null response.'); } $con->commit(); } catch(exception $e){ $con->rollback(); write_log('error') } write_log('end'); $con->commit(); }
Comments
Post a Comment