交易最佳实践 [英] Transactions best practices
问题描述
多少钱你依赖于数据库事务?
How much do you rely on database transactions?
你preFER或大或小事务范围?
Do you prefer small or large transaction scopes ?
你preFER客户端事务处理(例如在TransactionScope的.NET)的服务器上
方交易或反之亦然?
Do you prefer client side transaction handling (e.g. TransactionScope in .NET) over server side transactions or vice-versa?
有关嵌套事务是什么?
What about nested transactions?
你有一些提示和放大器;与交易相关的技巧。
Do you have some tips&tricks related to transactions ?
任何陷阱你遇到了交易工作?
Any gotchas you encountered working with transaction ?
中的所有排序的答案是受欢迎的。
All sort of answers are welcome.
推荐答案
我总是在using语句换的交易。
I always wrap a transaction in a using statement.
using(IDbTransaction transaction )
{
// logic goes here.
transaction.Commit();
}
一旦交易移出范围,它被设置。如果事务仍处于活动状态,它被回滚。这种行为故障保险您意外地锁定了数据库。即使未处理的异常被抛出,交易仍然会回滚。
Once the transaction moves out of scope, it is disposed. If the transaction is still active, it is rolled back. This behaviour fail-safes you from accidentally locking out the database. Even if an unhandled exception is thrown, the transaction will still rollback.
在我的code其实我省略明确回滚并依靠using语句做的工作对我来说。我只明确地执行提交。
In my code I actually omit explicit rollbacks and rely on the using statement to do the work for me. I only explicitly perform commits.
我发现这种模式已经大大减少记录锁定问题。
I've found this pattern has drastically reduced record locking issues.
这篇关于交易最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!