5

What's the point of disabling transactions? http://ellislab.com/codeigniter/user-guide/database/transactions.html

$this->db->trans_off()

I can't see the usage. Is it for testing purposes?

"When transactions are disabled, your queries will be auto-commited, just as they are when running queries without transactions."

I have a table called user where there exists a column nameOfUser. nameOfUser2 - column does NOT exist.

TEST1 This code would try to do 2 insertions with a normal transaction:

$this->db->trans_start();
$this->db->insert('user', array('nameOfUser' => 'testCVVCOOL'));
$this->db->insert('user', array('nameOfUser2' => 'test2'));
$this->db->trans_complete();    

but it was rolled back (nothing is inserted) because the column nameOfUser2-column does not exists in the second insert.

TEST2 This code would try to do 2 insertions with transaction disabled

$this->db->trans_off();
$this->db->trans_start();
$this->db->insert('user', array('nameOfUser' => 'testCVVCOOL'));
$this->db->insert('user', array('nameOfUser2' => 'test2'));
$this->db->trans_complete();    

Above would insert testCVVCOOL string into user-table even if there are an error in the second insert ($this->db->insert('user', array('nameOfUser2' => 'test2'));)

When do you have the need for disabling transactions in this way?

bestprogrammerintheworld
  • 5,417
  • 7
  • 43
  • 72
  • I am not sure but I believe it's more like a rest to the transaction state, as after you do trans_start() you might need to reset your transaction mode to off, if you have any query left to be executed after the transaction ended. – mamdouh alramadan Mar 16 '14 at 17:58

2 Answers2

1

When do you have the need for disabling transactions in this way?

My understanding is transactions don't allow more interaction in the same thread if there is an issue (as in your first example). So, if you wanted to do something else in the db before the commit and before the rollback, you wouldn't be able to.

Pseudo-example:

//will not work
$this->db->trans_start();
$this->db->insert('user', array('nameOfUser' => 'testCVVCOOL'));
$this->db->insert('user', array('nameOfUser2' => 'test2'));
//more likely this would be a condition that makes sense 
//perhaps using a query result from earlier in function
if(1===1){
    $this->db->query('INSERT INTO errors (db) values (1)');//nogo
}
$this->db->trans_complete(); 

-

//will work
$this->db->trans_off();
$this->db->trans_start();
$this->db->insert('user', array('nameOfUser' => 'testCVVCOOL'));
$this->db->insert('user', array('nameOfUser2' => 'test2'));
//more likely this would be a condition that makes sense 
//perhaps using a query result from earlier in function
if(1===1){
    $this->db->query('INSERT INTO errors (db) values (1)');//OK
}
$this->db->trans_complete(); 
stormdrain
  • 7,915
  • 4
  • 37
  • 76
  • You mean that you could mean (basically) for logging errors? – bestprogrammerintheworld Mar 18 '14 at 11:43
  • That's just one possible use-case. I think the point of allowing it is flexibility -- if one needed the functionality for whatever reason (errors, forcing writes, etc.) the functionality is there. – stormdrain Mar 18 '14 at 12:01
  • But that seems like an odd way to handle errors? (for example). In the docs (http://ellislab.com/codeigniter/user-guide/database/transactions.html) it says you can use $this->db->trans_status() for getting status of the transaction and then do some logging. This is my point. I can figure out any GOOD reason to use transactions off, because there are better alternatives? – bestprogrammerintheworld Mar 18 '14 at 12:14
  • Good point. You know, honestly, I'm not certain. Looking at the library, it seems that it is just a hook into the respective DB's transaction controls (autocommit, etc). It is really a question about the DB, not CodeIgniter. This http://stackoverflow.com/q/39583/183254 seems to suggest transactions are hard on resources and thus aren't good for high-traffic/high IO applications which makes sense. So I guess the answer is "A good reason to turn off transactions is for a high throughput application or part of application". – stormdrain Mar 18 '14 at 14:06
  • Ok about transactions that demands more resources. I get that :-) But that do no really answer my original question :-) And yes, it may demand more resources, but even on high traffic sites you need to be secure (make sure correct data is inserted). I would say, that's even more important when there are a lot of traffic. It's better to get better hardware or to have a good caching technique... – bestprogrammerintheworld Mar 18 '14 at 15:03
  • 1
    There are other ways to ensure correctness such as with an ORM. I'm not sure what else you are looking for in an answer; you asked why would someone want to do that and an answer is performance reasons. And what if you can't afford better hardware or caching? You can turn off transactions ;) – stormdrain Mar 18 '14 at 15:09
  • Ok, thanks a lot! :-) I don't know if I got smarter, but I might have gone wiser ;-) – bestprogrammerintheworld Mar 18 '14 at 19:18
0

It totally depends upon our scenario where we are using the transactions -

When Transaction is needed:

Need a set of operations at whole i.e. If some one orders a product online, and there are some steps involved in order confirmation then if any step is get failed then revert the whole order transaction.

When Transaction Off is needed:

If I am trying some stuff where any type of info is worth for me. Then we can use transaction off feature. Whatever error is occurred, save the data, which meets the criteria.

Suleman Ahmad
  • 2,025
  • 4
  • 28
  • 43