Commit 语句的使用

Usage of Commit Statement

如何正确使用Commit语句?我是否必须对每个插入语句和每个函数都使用 commit 语句?例如,如果我更改 table 以添加列,我是否必须使用提交语句?

更改 table 中的数据(插入、更新和删除)时需要提交。只要您不提交,其他用户将看不到更改的数据,您仍然可以选择通过执行回滚来撤消更改。

无需在每条语句后提交。

更改 table 本身时不需要提交 (alter table)。事实上,alter table 语句发出隐式提交。

Some more on implicit/explicit commit statements in Oracle

这样试试:

 try {
        // First of all, let's begin a transaction
        $db->beginTransaction();

        // A set of queries; if one fails, an exception should be thrown
        $db->query('first query');
        $db->query('second query');
        $db->query('third query');

        // If we arrive here, it means that no exception was thrown
        // i.e. no query has failed, and we can commit the transaction
        $db->commit();
    } catch (Exception $e) {
        // An exception has been thrown
        // We must rollback the transaction
        $db->rollback();
    }

根据积累的经验和 "good practices" - 你不应该在你的 functions/procedures 中实现 "commit" 语句(大多数时候,取决于任务,你试图实现什么). "commit" 决定权在调用您的 function/procedures 的用户。 当异常为 occurred/caught 时,您可以实现的是 "rollback"。 通常,当从 application/web 部分调用存储过程或函数时,它们会自动提交(据我所知,该选项可以在事务方法中更改,至少在 java 中是这样)。

如果你运行一个DDL statementCREATEALTERDROPGRANT等)那么:

Oracle Database implicitly commits the current transaction before and after every DDL statement.

如果您运行 DML 语句(INSERTUPDATEDELETESELECTCALLMERGE) 那么:

These statements do not implicitly commit the current transaction.

并且您将需要手动提交未提交的事务(但您不必 COMMIT 在每个语句之后)。

如果要执行部分回滚,则可以使用 SAVEPOINTs。

您不应将 COMMIT 语句放在函数或过程中,这样您就可以在单个事务中使用多个 functions/procedures,然后 COMMITROLLBACK 整个事务.