CodeIgniter Forums
transaction doesn't work properly - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: transaction doesn't work properly (/showthread.php?tid=68982)



transaction doesn't work properly - vahid.vdn - 09-24-2017

I'm looking for a case how `transaction` do it's job. In first case I write a `sleep(10000)` between two queries, then turn of the wamp server:

PHP Code:
$this->db->trans_begin();
$q1 $this->db->query("INSERT INTO `test`(`test_col`) VALUES ('aaaaaa')");
sleep(100000);
$q2 $this->db->query("INSERT INTO `test`(`test_col`) VALUES ('bbbbbb')");
$this->db->trans_complete(); 


But first record is saved without second one. Then I tried with another case, that first query fails because of duplicated value:

PHP Code:
$this->db->trans_begin();
$q1 $this->db->query("INSERT INTO `test`(`test_col`) VALUES ('duplicated')");
$q2 $this->db->query("INSERT INTO `test`(`test_col`) VALUES ('new row')");
$this->db->trans_complete(); 



But again the new one exist in the database. Any idea? Is there anything I'm missing?


Also `trans_rollback`  doesn't work.

PHP Code:
if ($this->db->trans_status() === FALSE)
{
 
   // I get this line, but trans_rollback doesn't work
 
   $this->db->trans_rollback();




RE: transaction doesn't work properly - vahid.vdn - 09-24-2017

I must use InnoDB in order to work with transactions!


RE: transaction doesn't work properly - skunkbad - 09-24-2017

The problem is that since you are using trans_begin instead of trans_start, you are running the transaction manually. When running transactions manually, you don't want to use trans_complete, but you are. Remove trans_complete, and then you should be able to rollback or commit.


RE: transaction doesn't work properly - vahid.vdn - 09-25-2017

(09-24-2017, 08:24 AM)skunkbad Wrote: The problem is that since you are using trans_begin instead of trans_start, you are running the transaction manually. When running transactions manually, you don't want to use trans_complete, but you are. Remove trans_complete, and then you should be able to rollback or commit.

I don't want to use rollback or commit. In my case, the problem was with Storage Engine. It must be InnoDB.


RE: transaction doesn't work properly - Narf - 09-25-2017

MyISAM strikes again.


RE: transaction doesn't work properly - InsiteFX - 09-25-2017

LOL! @Narf


RE: transaction doesn't work properly - skunkbad - 09-25-2017

(09-25-2017, 12:48 AM)vahid.vdn Wrote:
(09-24-2017, 08:24 AM)skunkbad Wrote: The problem is that since you are using trans_begin instead of trans_start, you are running the transaction manually. When running transactions manually, you don't want to use trans_complete, but you are. Remove trans_complete, and then you should be able to rollback or commit.

I don't want to use rollback or commit. In my case, the problem was with Storage Engine. It must be InnoDB.

If you don't want to rollback or commit, then why are you using trans_begin instead of trans_start? You need to go back and read the docs related to transactions.