(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