dongyuduan1890 2012-06-06 12:24
浏览 74
已采纳

将整个应用程序封装在数据库事务中

What are the theoretical & practical draw backs of having an entire php application running inside a database transaction, to ensure that any unexpected error or exception caused in the script reverts every change done in the database, thus avoiding a state where due to an unfinished script some tables have been updated and some others not? This obviously doesn't seem to be nor would ever be recommended as good practice, but I would like to understand in detail why.

  • 写回答

1条回答 默认 最新

  • dougong2005 2012-06-06 13:55
    关注

    Database transactions ensure that multiple operations obey the ACID properties:

    • Atomic - this is the property to which you allude in your question, whereby either every operation in the transaction succeeds or else they all fail;

    • Consistent - this property ensures that the database state is valid at all times;

    • Isolated - this property ensures that concurrent transactions (i.e. from different connections) occur as though they were executed serially; and

    • Durable - once commited, changes are permanent.

    In order to ensure the third property of Isolation, RDBMS systems like MySQL perform locking to ensure that, for example, one transaction does not write to a record whilst another is reading from it (when a record is locked, other transactions must wait for the lock to be released before they are able to proceed).

    If your transactions are unnecessarily long, they will lead to excessive locking and excessive waiting. It could even lead to unnecessary deadlocks, whereby transactions are waiting on locks held by eachother and cannot proceed until at least one is rolled back.

    For reasons of performance (and durability), you should therefore strive to keep transactions as small as is absolutely necessary to maintain consistency.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题