English 中文(简体)
• 如何测试MySQL的交易?
原标题:How to test MySQL transactions?

我有一个问题,即在交易中测试这些询问。 我在相当一段时间里一直在利用MySQL的交易,每次我这样做,我都使用这样的东西:

$doCommit = true;
$error = "";
mysql_query("BEGIN");

/* repeat this part with the different queries in the transaction
   this often involves updating of and inserting in multiple tables */
$query = "SELECT, UPDATE, INSERT, etc";
$result = mysql_query($query);
if(!$result){
    $error .= mysql_error() . " in " . $query . "<BR>";
    $doCommit = false;
}
/* end of repeating part */

if($doCommit){
    mysql_query("COMMIT");
} else {
    echo $error;
    mysql_query("ROLLBACK");
}

现在经常发生的情况是,我想要测试我的交易,因此,我改动了mysql_query(“ROLBACK”);,但我可以想象,这不是检验这种困难的非常好办法。 通常无法将每张表格复制到一张温床和更新,并插入这些表格,然后删除这些表格(例如,表格可能非常庞大)。 当然,当该守则生效时,就会出现相关的错误处理(而不是仅仅印刷错误)。

这样做的最佳途径是什么?

最佳回答

首先,你们的执行工作有ug。 如果发现问题错误,目前的交易自动收回,然后结束。 由于你继续提出询问,他们不会在交易中(这些交易将投向非行)。 然后,当你执行<代码>Rollback时,就无声失败。 http://dev.mysql.com/doc/refman/5.0/en/commit.html MySQL docs/a>:

Rolling back can be a slow operation that may occur implicitly without the user 
having explicitly asked for it (for example, when an error occurs).

只有在你在申请中确定需要退约时,方可使用明确指挥代码<>ROLBACK。 例如,如果你从一个账户中提取资金,如果你发现用户没有足够资金来完成交换,你就会明确退款。

在测试交易时,我确实复制数据库。 我建立了一个新的数据库并安装了一套“血清数据”。 然后,我使用自动工具进行所有测试。 该工具将实际进行交易和进行部队飞行,并检查在整个测试过程中维持预期数据库状态。 由于如果你对交易的投入不为人所知,更难以从交易中规划到最终状态,因此,对活(甚至从活到活)数据进行测试不会容易。 你们可以这样做(而且应该这样做),但并不取决于这些结果,以确定贵系统是否发挥作用。 利用这些结果为自动检测器建造新的测试案例......

问题回答

难道你可以重新证明你的第一例,并使用一些非行的包裹。

In that wrapper class you can have a variable $normalCommit = true; and a method SetCommitMode() which sets that $normalCommit variable. And you have a method Commit() which commits if($normalCommit == true) Or even have a variable $failTransaction which calls mysql_query("ROLLBACK"); if you wish (so you could pass/fail many sequential tests).

Then when you run the test, you can set somewhere in the test code file: $myDBClass->SetCommitMode(false); or $myDBClass->RollBackNextOperation(true); before the operation which you wish to fail, and it will just fail. In such a way the code which you are testing will not contain those fail/commit checks, only the DB class will contain them.

通常情况下,法国国家测验法(特别是如果你进行单位测试)应采用“三氯苯并二苯”和“滚BackNext”作业方法,因此,你无意中不使用生产法中的这些要求。

或者,如果贵国的法典在出现这种结构错误后没有实施,那么你可以将一些高射数据传送到你的方法中(如果你正在测试一种方法),像消极的变数一样,用于在UNSignED油田中节省开支,那么,如果你的代码没有发生这种差错(但不应这样做)。

总的来说,我使用了像我这样的东西(例如,我用纸):):

$db->beginTransaction();
try {
  $db->exec( INSERT/DELETE/UPDATE );
  $db->commit();
}
catch (PDOException $e) {
  $db->rollBack();
  // rethrow the error or
}

或者,如果你有自己的例外手,则使用特别条款,对您的指定官员进行追踪。 例:

function my_exception_handler($exception) {
  if($exception instanceof PDOException) {
    // assuming you have a registry class
    Registry::get( database )->rollBack();
  }
}




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签