如何模拟交易失败? [英] How to simulate a transaction failure?

查看:63
本文介绍了如何模拟交易失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将事务用于某些MySQL查询,其中我在几个表上进行多次插入。

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.

我尝试使列名变乱,但这却引发了PHP错误。

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?

推荐答案

尝试使用SIGNAL关键字在SQL脚本内引发异常。例如:

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

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

这篇关于如何模拟交易失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆