Yii Framework Forum: Duplicate Record In Transaction - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Duplicate Record In Transaction Rate Topic: -----

#1 User is offline   MaziTizeh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 58
  • Joined: 12-June 12
  • Location:U.A.E

Posted 22 June 2013 - 06:54 AM

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
0

#2 User is offline   MaziTizeh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 58
  • Joined: 12-June 12
  • Location:U.A.E

Posted 23 June 2013 - 01:07 AM

Nobody is here to give me a solution ?
0

#3 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,005
  • Joined: 16-February 11
  • Location:Japan

Posted 23 June 2013 - 01:31 AM

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?
0

#4 User is offline   MaziTizeh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 58
  • Joined: 12-June 12
  • Location:U.A.E

Posted 23 June 2013 - 07:03 AM

View Postsoftark, on 23 June 2013 - 01:31 AM, said:

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
0

#5 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,005
  • Joined: 16-February 11
  • Location:Japan

Posted 23 June 2013 - 11:19 AM

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.

Quote

    $transaction=$sender->dbConnection->beginTransaction();
    $status = false;
    try{
        ...
        if($sender->save()){
            ...
            $receiver ->save();
            $status = true;
        }else{
            $status = false;
        }
        $transaction->commit();
    }
    catch(Exception $e){
        $transaction->rollBack();
        $status =  false;
    }
    return $status;


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.
0

#6 User is offline   MaziTizeh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 58
  • Joined: 12-June 12
  • Location:U.A.E

Posted 24 June 2013 - 01:54 AM

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
0

#7 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,005
  • Joined: 16-February 11
  • Location:Japan

Posted 24 June 2013 - 05:19 AM

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.)
0

#8 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 831
  • Joined: 20-April 11
  • Location:India

Posted 24 June 2013 - 05:26 AM

View PostMaziar, on 24 June 2013 - 01:54 AM, said:

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


it can happen with some jqueries,multtiple post!! check with firebug-console
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#9 User is offline   MaziTizeh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 58
  • Joined: 12-June 12
  • Location:U.A.E

Posted 25 June 2013 - 09:15 AM

View Postsoftark, on 24 June 2013 - 05:19 AM, said:

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.)


Hi,
we are using ubantu os and normal mysql db
0

#10 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,005
  • Joined: 16-February 11
  • Location:Japan

Posted 25 June 2013 - 11:23 AM

View PostMaziar, on 25 June 2013 - 09:15 AM, said:

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.
0

#11 User is offline   MaziTizeh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 58
  • Joined: 12-June 12
  • Location:U.A.E

Posted 03 December 2013 - 09:06 AM

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..

View Postsoftark, on 25 June 2013 - 11:23 AM, said:

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.

0

#12 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,005
  • Joined: 16-February 11
  • Location:Japan

Posted 06 December 2013 - 06:44 AM

Hi MaziTizeh, welcome back.

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

Quote

99% working , 1% will get wrong

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users