是否可以接受长期交易? [英] Are long-living transactions acceptable?

查看:21
本文介绍了是否可以接受长期交易?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑通过以下方式在 2 层 WPF(或 Windows 窗体)应用程序中使用事务:

I am thinking about using transactions in 2-tier WPF (or windows forms) applications in following way:

当我们打开新的表单编辑数据时,我们可以开始新的事务,在这个事务中透明地编辑和持久化更改.然后我们可以单击确定"按钮并提交事务,或单击取消"按钮并回滚它.如果我们想用这个数据打开另一个对话窗口,我们可以使用嵌套事务.

We can begin new transaction when we open new form for editing data, edit and persist changes transparently in this transaction. Then we can click "OK" button and commit transaction, or "Cancel" button and rollback it. If we want to open another dialog window with this data, we can use nested transactions.

问题是:这种使用事务的方式是否可以接受?我知道有很多不同的方法可以实现这种逻辑,但我想列出这种逻辑的优缺点.

The question is: Is this way of using transactions acceptable or not? I know that there are a lot of different ways to implement such logic, but I'd like to list advantages and disadvantages of this one.

推荐答案

这里有一些你可能会遇到的问题

Here is a few problem that you might encounter if you go this way

  • 连接重置/关闭/超时(如果用户去洗手间)
  • 数据库配置(数据库大多是为许多短事务而不是长事务预先配置的.例如,可能需要调整用于跟踪 tx 期间已完成操作的撤消日志)
  • 问题,甚至可能是死锁(事务时间越长,同一个锁以冲突顺序被获取两次的可能性就越大)
  • connection reset/close/timeout (if user goes to the bathroom)
  • database configuration (database are mostly pre-configured for many short transactions, not long ones. E.g. the undo log that keeps track of what has been done during the tx may need to be tuned)
  • lock issues, even maybe deadlocks (the longer the transactions are, bigger the chances are the same lock is acquired twice possibly in conflicting order)

这是一种不鼓励的做法.改用乐观锁定.必要时读取数据,在内存中保留一份副本.当对话框关闭时,尝试将更改与数据库同步.如果中间的数据库中的数据已被修改,则该操作将中止.失败的概率取决于用户数量等.但这在实践中通常是可以接受的.

This is a discouraged practice. Use optimistic locking instead. Read data when necessary, keep a copy in memory. When dialog is closed, attempt to synchronize the changes with the database. If data have been modified in the database in between, the action is aborted. The probability that it fails will depend on the numer of users, etc. But this is frequently acceptable in practice.

这篇关于是否可以接受长期交易?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆