herondale
herondale

Reputation: 789

Using CodeIgniter transactions?

Is there any difference between using

$this->db->trans_start();

and using

$this->db->query('START TRANSACTION');?

Actually, I wish to apply this question to other programming languages, such as C# with BeginTransaction().

When do you explicitly write out that you want to begin a transaction in a query and when do you just invoke a built-in method from the framework or whatever language you are using?

Upvotes: 1

Views: 418

Answers (1)

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

Question: Is there any difference between using
Answer: No. Both do the same sort of job.


Explanation

If You need to get know about CI Transaction Read this answer on another question.

But By only using $this->db->query() you can't archive transaction part comletly.

I Recommend this personally Read this Documentaion


Question: When do you explicitly write out that you want to begin a transaction in a query and when do you just invoke a built-in method from the framework or whatever language you are using?
Answer: (based on PHP/CI)It's based on what kind a system you do.

Explanation

For example, if you have simple product add and its have many child tables. (assume 1 master and 4 child tables). so once one of the child table throws an error, data remain at the upper-level table. So to prevent that we should use Transaction on here.

Assume Child Table 2 occurred an error and this what happens (for Inserted Data).

| Table Name    | Normal Insert     if          |
                    /update     | Transaction   |
|    ------     |     ------    |    ------     |
| Master Table  | Remain        | Roalbacked    |   
| Child Table 1 | Remain        | Roalbacked    |
| Child Table 2 | Error         | Roalbacked    |
| Child Table 3 | Empty         | Nothing to do |
| Child Table 4 | Empty         | Nothing to do |

Useful Links

  1. Transactions - codeigniter.com
  2. My Answer on another question
  3. php-mysql transaction - mysqltutorial.org

Upvotes: 1

Related Questions