How to simulate a transaction failure? [closed]

2019-08-13 00:15发布

问题:

I am utilising transactions for some MySQL queries where I am doing multiple inserts across a few tables.

After the transaction has been committed, I am checking the status, throwing an error/exception if it failed.

However, I am struggling to test the case where it fails.

I tried garbling up a column name, but that threw a PHP error instead.

Under what circumstances does a transaction fail, so I can attempt to recreate my own failures for testing purposes?

回答1:

try to throw an exception inside your sql script using SIGNAL key word. for exmple:

SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Custom error';