Transaction cannot roll back in coding

I am working on a transaction, and in the code I mentioned, I ran into some problem. I did not commit transactions, but inserted data into my database.

$this->db->trans_begin();
$this->db->insert('tblorder',$data);
$orderid=$this->db->insert_id();
foreach ($orderItemList as $orderItemList) {
    $orderitem = array('orderid' =>$orderid ,'productid' =>$orderItemList->productid ,'amount' =>$orderItemList->amount);
    $this->db->insert('tblorderitem',$orderitem);
}
$this->db->trans_complete();

if ($this->db->trans_status() == 1) {
    $this->db->trans_rollback();
    return "true";
} else {
    $this->db->trans_commit();
    return "false";
}

I canceled the transaction, and again all the data was inserted into my database. What is the problem? I can’t get it.

+4
source share
6 answers

Do not use $this->db->trans_complete();

according to the documentation

$this->db->trans_begin();

$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->query('AND YET ANOTHER QUERY...');

if ($this->db->trans_status() === FALSE)
{
        $this->db->trans_rollback();
}
else
{
        $this->db->trans_commit();
}
+5
source

UPDATE RESPONSE!

I must tell you that by default the codeigniter configuration automatically captures all transactions.

- , :

$this->db->trans_off();    

$this->db->begin();

, ,

$this->db->trans_complete();

, :

$result = $this->db->trans_complete();

if($result === true){
    $this->db->trans_commit();
}else{
    $this->db->trans_rollback();
}

, :

$this->db->trans_off();

$this->db->query("insert this...");
$this->db->query("insert that...");

$result = $this->db->trans_complete();

if($result === true){
    $this->db->trans_commit();
}else{
    $this->db->trans_rollback();
}
+2

CI 3.0.1, , , - $this->db->trans_start(); $this->db->trans_complete();

, :

$this->db->trans_start();
//any code goes here
$this->db->trans_complete();

, - , $this->db->trans_complete();

, trans_start, trans_begin. trans_complete trans_status trans_commit trans_rollback.

:

$this->db->trans_start();
//any code goes here
$this->db->trans_start();
//any code goes here
$this->db->trans_complete();
//any code goes here
$this->db->trans_complete();

Codeigniter Strict Mode, trans_complete , - .

+2

ROLLBACK? - - , trans_status? , trans_rollback trans_complete trans_commit.

, ( ):

trans_start
...
if ( some non-db problem )  trans_rollback
trans_complete
+2

. . .

$this->db->trans_begin();

$this->db->insert('tblorder',$data);
$orderid=$this->db->insert_id();

$orderitem = array();

foreach ($orderItemList as $orderItemList) 
{
    $orderitem[] = array('orderid' =>$orderid ,'productid' =>$orderItemList->productid ,'amount' =>$orderItemList->amount);
}

$this->db->insert_batch('tblorderitem', $orderitem); 

$this->db->trans_complete();

if ($this->db->trans_status() === FALSE) 
{
    $this->db->trans_rollback();
    return FALSE;
} 
else 
{
    $this->db->trans_commit();
    return TRUE;
}
+2

, , , - . ? CodeIgniter, "" . , , .

, . $orderItemList, db->insert .

$this->db->trans_start();
$this->db->insert('tblorder', $data);
$orderid = $this->db->insert_id();
foreach($orderItemList as $orderItem)
{
    $orderedItems[] = [
      'orderid' => $orderid,
      'productid' => $orderItem->productid,
      'amount' => $orderItem->amount
    ];
}
if(isset($orderedItems))
{
    $this->db->insert_batch('tblorderitem', $orderedItems);
}
$this->db->trans_complete();
$this->db->trans_status();
+2

Source: https://habr.com/ru/post/1684845/


All Articles