我完全不明白我们应该如何用PHP编写SQL事务。
我们有发票付款部分,所以我们必须做
updateInvoice()
addJournals()
setUpReport()中进行支付详细信息
因此,我们将这三个操作都包含在一个事务中。
try {
$this->conn->beginTransaction();
updateInvoice();
addJournals();
setUpReport();
$this->conn->commit();
} catch (Exception $ex) {
$this->conn->rollback();
}在这个事务中大约有8-10个表,而且这些事务似乎正在锁定所有这些表。
此外,我们注意到这个过程花费了太多的时间,并且在这个过程中偶尔会发生死锁。在做一些研究时,我理解我们需要使上面的事务变得原子化和简单。大多数建议都指向将事务拆分为多个事务。因此,我计划对每个函数进行单独的事务处理,例如
try {
$this->conn->beginTransaction();
updateInvoice();
$this->conn->commit();
} catch (Exception $ex) {
$this->conn->rollback();
}
try {
$this->conn->beginTransaction();
addJournals();
$this->conn->commit();
} catch (Exception $ex) {
$this->conn->rollback();
}
try {
$this->conn->beginTransaction();
setUpReport();
$this->conn->commit();
} catch (Exception $ex) {
$this->conn->rollback();
}如果我像这样重构代码,如果在setUpReport()上发生错误,那么很难在上面的2个事务中恢复操作。因此,我真的很困惑我们需要如何构建事务。
发布于 2022-08-19 10:40:41
我也有同样的问题,我更改了max_execution_time = 60
如果无法更改值,请重新连接
例子:
echo "Other queries in your system / framework.....";
$this->sendQuery("SQL: SELECT * from session_tab...");
$this->sendQuery("SQL: SELECT * from privileges... ");
$this->sendQuery("SQL: .....");
$this->sendQuery("SQL: ......");
$this->sendQuery("SQL: SELECT * from table..."); // Waiting long time..
echo "After that you want execute next queries.. " ;
$this->db->disconnect();
$this->db->connect();
try {
$this->conn->beginTransaction();
setUpReport();
$this->conn->commit();
} catch (Exception $ex) {
$this->conn->rollback();
} https://stackoverflow.com/questions/73415318
复制相似问题