-
Notifications
You must be signed in to change notification settings - Fork 1.9k
docs: update DB transactions #6886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
How about a test with |
96ee2f7
to
efb7f7a
Compare
I don't know why Strict Mode is needed. |
Added. It works as expected. But it seems the strict mode makes sense only when DBDebug is false. Is the strict mode a safeguard that prevents all subsequent transactions from being executed if one transaction fails when there were no exceptions in PHP? |
Adding the transaction status helps to understand the tests better.
yes, then you can use I wonder if calling |
There is no resetTransStatus() method. In case you wanted to try a series of transactions again while in strict mode.. It can be accomplished like this however: public function resetTransAfterFailure()
{
$this->disableDBDebug();
$builder = $this->db->table('job');
$this->db->transStrict(true);
// The first transaction group
$this->db->transStart();
$jobData = [
'name' => 'Grocery Sales',
'description' => 'Discount!',
];
$builder->insert($jobData);
$this->assertTrue($this->db->transStatus());
// Duplicate entry '1' for key 'PRIMARY'
$jobData = [
'id' => 1,
'name' => 'Comedian',
'description' => 'Theres something in your teeth',
];
$builder->insert($jobData);
$this->assertFalse($this->db->transStatus());
$this->db->transComplete();
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
$this->db->transStrict(false); // resets transaction status
$this->db->transComplete(); // resets transaction status
$this->db->transStrict(true); // return back to strick mode
// The second transaction group
$this->db->transStart();
$jobData = [
'name' => 'Comedian',
'description' => 'Theres something in your teeth',
];
$builder->insert($jobData);
$this->assertTrue($this->db->transStatus());
$this->db->transComplete();
$this->seeInDatabase('job', ['name' => 'Comedian']);
$this->enableDBDebug();
} It might be helpful if you wanted to call a series a transactions another time. $this->aMethodThatCallsASeriesOfTransactionsInStrictMode();
if ($this->db->transStatus() === false) {
$this->db->resetTransStatus();
$this->aMethodThatCallsASeriesOfTransactionsInStrictMode(); // try it again
} OR $this->aMethodThatCallsASeriesOfTransactionsInStrictMode();
$this->db->resetTransStatus();
$this->anotherMethodThatCallsASeriesOfTransactionsInStrictMode(); // try it again The way it is when running in strict mode (default) once a failure occurs you can never run anymore. |
We should show how to turn debug off in the managing errors section. |
Can you add this method and documentation? /**
* Reset transaction status - to restart transactions after strict mode failure
*/
public function resetTransStatus(): BaseConnection
{
$this->transStatus = true;
return $this;
} |
We probably need a public method on BaseConnection to set debug mode. |
e5b9d50
to
63eaff4
Compare
Do you need Since PHP 8.0, PDO throws exceptions by default: |
5cceb94
to
52aba71
Compare
I added a comment in the sample code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few typos, otherwise looks good!
I really wish we had a method. Perhaps a better way to handle this would be to have a parameter on I looked back in my own code where I used transactions and found that I had implemented a workflow using We need an easy way to set the failure behavior other than the database config file. |
@codeigniter4/database-team Before v4.3.0, Since v4.3.0,
Yes, now in
I feel that such a way is necessary unless we remove manual transactions functionality. |
I was strongly in favor of the DBDebug changes and @kenjis' explanation seems like the next logical steps. I've never actually used our And yes: let's plan to do away with DBDebug altogether. Firing a |
I created an issue #6909 This PR is only for documentation update and adding test code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we also update the user guide if the general behavior in 4.3 has changed? If we're about to throw an exception every time, the examples in the user guide seem useless now.
I would like to see Transactions to be mentioned in the changelog. This is a critical change for someone who uses them.
2ccb1c1
to
9ad345b
Compare
Rebased. |
Added about transactions in the changelog. |
I added a sample code and updated some descriptions. I also found that SQLite3, Postgres and OCI8 drivers throw ErrorException when a query error occurs CodeIgniter4/system/Debug/Exceptions.php Lines 174 to 176 in ac1422c
So depending on the value of the error reporting setting, the ErrorException may not be thrown. |
$this->db->transComplete(); | ||
} catch (DatabaseException $e) { | ||
// Automatically rolled back already. | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still feel uncomfortable with this code.
See #6917 |
Well, I wasn't saying anything since everyone seemed to be on board regarding throwing exceptions, but... personally, I don't think that throwing exceptions for anything related to transactions has sense. This is why we have So, for me #6917 is definitely a good way to solve this. |
e329f52
to
c5c5e39
Compare
Yes, I also think it is necessary missing feature for v4.3. |
Co-authored-by: sclubricants <vaughnb@scoil.com>
Co-authored-by: MGatner <mgatner@icloud.com>
All the current DB drivers throw an exception when a query error occurs. See codeigniter4#6912 MySQLi driver sets `mysqli_report(MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX)` that throws mysqli_sql_exception. SQLite3, Postgres and OCI8 drivers throw ErrorException because of Error Handler. SQLSRV driver has different implementaion. It throws DatabaseException when the query return value is false.
c5c5e39
to
b2e90d7
Compare
@sclubricants I sent #8767 |
Needs #6919Description
fix out of dated description. See "Strict mode" not as described. bcit-ci/CodeIgniter#4032Checklist: