Welcome Guest, Not a member yet? Register   Sign In
mysqli and nested transactions
#1

Hi! It seems that nested transactions work correctly using $this->db->trans_start (and the appropriate fix from here https://ellislab.com/forums/viewthread/101648/) but the behaviour it's different when using $this->db->trans_begin

PHP Code:
public function trx_test() {
 
       $this->db->trans_begin();
 
       
        $this
->db->insert('test_tbl', array('id' => 0));
 
       
        $this
->db->trans_begin();
 
       
        $this
->db->insert('test_tbl', array('id' => 1));

 
       $this->db->trans_rollback();
 
       
        $this
->db->trans_commit();


test_table contains only 1 row with id = 0


PHP Code:
public function trx_test_2() {
        
$this->db->trans_start();
        
        
$this->db->insert('test_tbl', array('id' => 0));
        
        
$this->db->trans_start();
        
        
$this->db->insert('test_tbl', array('id' => 1));

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


test_table contains no rows


Is this difference by design or what? I'm expecting same behaviour (the correct one should be the one from trx_test_2).
Reply
#2

trans_start and trans_complete if for automatic commit or rollback based on the success or failure of your queries.

trans_begin, trans_rollback and trans_complete if for managing the transaction manually (you decide if and when you call rollback or commit).

You can't mix both and expect it to work. The doc is clear on that :
Quote:Note: Make sure to use $this->db->trans_begin() when running manual transactions, NOT $this->db->trans_start().

See this page for more details on transactions : http://www.codeigniter.com/user_guide/da...tions.html
Reply
#3

(This post was last modified: 11-28-2014, 05:59 AM by geekita.)

So there's no way to use nested trans_begin/trans_rollback/trans_commit without unexpected behavior?

BTW my trx_test_2 example works like a charm.
Reply
#4

In your second example you use trans_start with trans_commit/rollback instead of trans_start.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB