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.

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

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法