最好的方式写Delphi数据库应用程序与事务&数据感知组件 [英] Preferable way to write Delphi database apps with transactions & data-aware components

查看:469
本文介绍了最好的方式写Delphi数据库应用程序与事务&数据感知组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用事务和数据感知组件编写Delphi数据库应用程序的首选方式是什么?



我必须编写访问InnoDB表的客户端应用程序,做一些主 - 详细的东西里面的事务。在对交易进行一些研究(从一般的观点来看)后,我谦恭地得出结论:非数据感知组件和手编码SQL将是事务的完美匹配;但是数据感知组件不会。它们似乎不是相互制造的。



我真的需要使用事务,但另一方面,我不能只是抛出数据感知因为他们大大简化了事情。



有人可以请我吗?我一直在谷歌搜索,但我还没有找到任何有用的答案。也许因为我的英语不够好,我的关键字有限。



BTW,我使用Delphi 7,目前评估UniDAC作为数据访问库。



谢谢。



EDIT b $ b

描述我的问题的一个方面的例子:



想象一个有2个DBGrids的表单。第一个网格是MasterGrid,上面是这些按钮:添加,编辑&删除。第二个网格是DetailGrid。如果用户点击添加,那么它就像这样:




  • Connection.StartTransaction

  • 然后,添加Master.Post,然后Master.Edit(所以主数据集具有自动增量主键,现在可以编辑)

  • 以模态显示编辑表单,用户填充主表

  • 如果用户单击确定,应用程序将执行Master.Post和Connection.Commit。如果用户点击取消,则应用程序将执行Connection.Rollback。



我知道事务应该尽可能短你可以看到上面的事务只有用户填写表单的速度。



如果我使用非数据感知组件,我将根据用户输入创建自定义插入SQL,然后在StartTransaction和Commit之间执行SQL。所以我可以实现很短的交易。



编辑2



的你的参与。我从vcldeveloper中选择答案,因为它是我当前需要的最接近的解决方案。

解决方案

其他提到使用DatasetProvider和ClientDataset的组合进行批量更新,但是在使用ADO或UniDAC组件的情况下,您不需要额外的DatasetProvider + ClientDataset层,因为ADO和UniDAC都支持批量更新。



对于 ADO ,您应该做什么是将数据集的 LockType 设置为 ltBatchOptimistic 。对于 UniDAC ,您应将 CacheUpdate 属性设置为 True



您的数据集缓存您对其内存记录集所做的所有更改,只有在调用 UpdateBatch 方法(ADO)或 ApplyUpdates 方法(UniDAC )。现在您应该做的是让您的用户在主数据集中插入/编辑一条记录,以及使用任何数据在细节数据集中记录他/她想要的任何记录。 - 你喜欢的组件。所有更改都将被缓存。当用户完成后,您可以启动一个新的事务,首先为主数据集调用UpdateBatch(或者在UniDAC的情况下为ApplyUpdate),然后调用详细数据集,如果一切顺利,则提交该事务。



这将使您的交易变得简单,而不需要额外的ClientDataset层。



/ p>

What is the preferable way to write Delphi database applications using transactions and also data-aware components?

I have to write a client app that access InnoDB tables, and do some master-detail kind of things inside transactions. After doing some research on transactions (from general point-of-view), then I humbly make a conclusion that non data-aware components and hand-coded SQL would be the "perfect match" of transactions; But the data-aware components wouldn't be. They don't seem to be made for each other.

I have the real need to use transactions, but on the other hand I could not just throw the data-aware components away because they greatly simplify things.

Could somebody please enlighten me? I have been Googling it, but I have not found any useful answer. Perhaps because my English is not good enough that my keywords are limited.

BTW, I'm using Delphi 7 and currently evaluating UniDAC as the data access library.

Thank you.

EDIT

Example to describe an aspect of my question:

Imagine a form with 2 DBGrids on it. The first grid is MasterGrid and above it are these buttons: Add, Edit & Delete. The second grid is DetailGrid. If the user click Add, then it go like this:

  • Connection.StartTransaction
  • Master.Append then Master.Post then Master.Edit (so the master dataset has the autoincrement primary key, and it is editable now)
  • Show the editing form modally, in which the user fills the master records, and also add some detail records using another form.
  • If the user click OK, the app would do Master.Post and Connection.Commit. If the user click Cancel, then the app would do Connection.Rollback.

I know that transactions should be as short as possible, but you can see above that the transaction is only as short as the speed of the user filling the form.

If I were using non data-aware components, I would make custom insert SQLs based from user input, then execute the SQL between StartTransaction and Commit. So I can achieve very short transaction.

EDIT 2

I thank all of you for your kind participation. I pick the answer from vcldeveloper because it is the closest solution to my current need.

解决方案

Others mentioned using a combination of DatasetProvider and ClientDataset to have a batch update, but in case of using ADO or UniDAC components, you do not need the extra layer of DatasetProvider + ClientDataset, because both ADO and UniDAC support batch updates.

For ADO, what you should do is to set LockType of your dataset to ltBatchOptimistic. For UniDAC, you should set CacheUpdate property to True.

This change makes your dataset to cache all the changes you make on its in-memory recordset, and send them alltogether to database only when you call UpdateBatch method (ADO) or ApplyUpdates method (UniDAC).

Now what you should do is to let your user insert/edit a record in the master dataset and whatever records he/she wants in the details dataset using whatever data-aware components you like. All the changes would be cached. When your user is done, you can start a new transaction, and first call UpdateBatch (or ApplyUpdate in case of UniDAC) for the master dataset, and then for the details dataset, and if everything goes fine, commit the transaction.

This will make your transactions short without needing the extra layer of ClientDataset.

Regards

这篇关于最好的方式写Delphi数据库应用程序与事务&数据感知组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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