CI source Analysis (iii)-DB affairs

Source: Internet
Author: User
Tags error handling



(0) How to use
You can use the system-provided method: $this->db->trans_start (); Start transaction $this->db->query (); Executive Sqls$this->db->trans_complete (); End transaction (no need to worry about rolling back) if! $this->db->trans_status do something; Error handling
You can also handle commit and rollback yourself: $this->db->trans_begin (); Start transaction $this->db->query (); Execute sqlsif! $this->db->trans_status$this->db->trans_rollback (); Rollback, and handle error handling else$this->db->trans_commit (); Submit
(1) Error handling
    • Debug mode can be turned on in dev, test environment, and the DB error message will be displayed directly
    • In a production environment, the Tran_staus () method is required to determine if an error log needs to be logged, and the front end is returned with a false message (the individual feels that a log error should be in the trans_complete, or the DB class provides the error () method to get the wrong message )

(2) Multi-tier transactions
    • When multi-tier transactions, CI records the transaction level through _trans_depth, processing only the outermost transaction, that is, _trans_depth>1 Trans_start&&trans_complete does not work.
    • However, _trans_depth counts only in Trans_start and Trans_complete, which means that if a transaction is manually manipulated, the count does not work, and the first trans_commit is in effect (manual execution does not make it difficult to control the transaction level, Commit and rollback are more difficult to handle, so it is best to use the system recommended way to handle transactions)


CI source Analysis (iii)-DB affairs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.