Duplicate Record In Transaction

Hi,

In my project there is some huge transaction,most of the time it is working fine but in some time it is replicating the data in my table.

please check my table content…

if you see, same Transaction , same time, same description, and same amount

ID Date Description Credit Debit Balance

30 07-Mar-2013 15:51:35 Description 20.00 0.00 34.10

30 07-Mar-2013 15:51:35 Description 20.00 0.00 34.10

30 07-Mar-2013 15:51:35 Description 20.00 0.00 34.10

30 07-Mar-2013 15:51:35 Description 20.00 0.00 34.10

pls find below my controller and model…

my controller


public function actionTransfer(){

	$mymodel= new Mymodel;

	if(isset($_POST['Mymodel'])){

		$mymodel->attributes = $_POST['Mymodel'];

		$amount = $_POST['Mymodel']['DepositeAmount'];

		$remarks =$_POST['Mymodel']['Description'];

        $typeoftransaction = 'Regular';                           

        $status = $mymodel->addTransfer($ownerID,$receiverid,$amount,1,2,$remarks,$typeoftransaction);

        if($status){

          // send mail..

       else{

         echo "trnsaction failed";

       }

	}

}

My model


 

public function addTransfer($ownerID,$receiverid,$amount,$senddesc,$recvdesc,$remarks,$type){ 

// for sender reccord....


$sender= new Mymodel;

                $transaction=$sender->dbConnection->beginTransaction();

                $status = false;

                try{

                    $totalbalance = 0;

                    $txnno = 0;

                    $sendelatestRec = $sender->getwalletAmount($ownerID);

                    $totalbalance = $sendelatestRec->TotalBalance;

                    $txnno =  $sendelatestRec->TxnID + 1;

                    $sender->OwnerID = $ownerID;

                    $sender->SenderID = $ownerID;

                    $sender->ReceiverID = $receiverid;

                   $sender->DepositeAmount = 0;

                    $sender->WithdrawAmount = round($amount,2);

                    $sender->TotalBalance = round($totalbalance - $amount,2);

                    $sender->Type = $type;

                    $sender->Date = time();

                    $sender->TxnID = $txnno;

                    $sender->RemarksID = $senddesc ;

                    $sender->Description = $remarks ;

                    if($sender->save()){

                        

                     // for receiver reccord....


                        $receiver = new Mymodel;

                        $txnno = 0;

                        $totalbalance = 0;

                        $sendelatestRec = $receiver ->getwalletAmount($receiverid);

                        $totalbalance = $sendelatestRec->TotalBalance;

                        $txnno =  $sendelatestRec->TxnID + 1;

                        $receiver ->OwnerID = $receiverid;

                        $receiver ->SenderID = $ownerID;

                        $receiver ->ReceiverID = $receiverid;

                        $receiver ->DepositeAmount = round($amount,2);

                        $receiver ->WithdrawAmount = 0;

                        $receiver ->TotalBalance = round($totalbalance + $amount,2);

                        $receiver ->Type = $type;

                        $receiver ->Date = time();

						 $receiver ->TxnID = $txnno;

                        $receiver ->RemarksID = $recvdesc;

                        $receiver ->Description = $remarks ;

                        $receiver ->save();                  

                        $status = true;

                    }else{

                         $status = false;

                    }

                       $transaction->commit();  

                       

                }

                catch(Exception $e){

                  $transaction->rollBack();

                   $status =  false;

                }

                return $status;

}

Please tell me how it will be happen with same code, but 99% working , 1% will get wrong result.

Please advise

Nobody is here to give me a solution ?

Hi Maziar,

Are you sure that the duplicated rows have been created in a single transaction? I mean, not in a series of transactions executed almost at the same time?

In other words, are you sure that the code works 100% (not 99%) without duplicating the rows when you comment out the transaction?

Hi,

The duplication occurred in single transaction.Initially am not using the commit and roll back in my code.But i saw that there is heavy transaction in my website so some time am facing problem with inserting the data in the table.means sender data it will insert but have sme other reason receiver table not inserting data.Here I used commit and rollback.Whole time it is working some time am getting the duplicate record.

This is because i am using the commit and rollback?? please advise me…

Thanks

Hmm, I don’t know what’s going on.

I’m not sure if it is a point or not, but your current code still permits the situation in which only $sender is saved and $receiver is not saved.

In the above, you are committing without checking the result of $receiver->save().

I think it should be like this:




    $transaction=$sender->dbConnection->beginTransaction();

    $status = false;

    try{

        ...

        if($sender->save()){

            ...

            if ($receiver ->save()){

                $status = true;

            }

        }

        if ($status){

            $transaction->commit();

        } else {

            $transaction->rollBack();

        }

    }

    catch(Exception $e){

        $transaction->rollBack();

        $status =  false;

    }

    return $status;



But, again, I’m not sure if this is a point in the issue or not.

I think you have to examine why $receive->save() can fail.

Hi,

Yes you right,but current scenario am getting the record in both my sender and receiver table but only the things its replicating the value.

My point is that it is executing from any cache value or something , why i am asking this is if the main function executing 2 times or more at least the value should different(time and withdrawal/deposit value),but here all the value is same.

please help me to find a solution for this problem

Thanks

I’m sorry, but I have no idea about the replication of the record.

Maybe you have to show us the detailed information about the environment (OS, DBMS, their settings … etc.)

it can happen with some jqueries,multtiple post!! check with firebug-console

Hi,

we are using ubantu os and normal mysql db

Well, I think that the information that you have provided so far is too limited to be examined … although I’m also a little bit pessimistic about the possibility for you to do so, because it can be too large an amount of code and logs for a forum topic.

Please explain me what should i have to do… or please tell me what exactly you need the information.If you are able to send those amount of code through my personal mail i can give my email id .please let me know…

Hi MaziTizeh, welcome back.

I’m very sorry, but I don’t think I can help you a lot in this problem.

I think this is a kind of error that can be solved only by someone who knows everything about the project, i.e., you or your project partners.