LLBLGen - TransactionScope 或 DataAccessAdapter.StartTransaction

发布于 2024-07-18 10:27:04 字数 511 浏览 4 评论 0原文

我发现使用 llblgen 管理事务有两个主要选项。

方法 1:

using(DataAccessAdapter adapter = new DataAccessAdapter())
{
    adapter.StartTransaction(IsolationLevel.ReadCommitted, "TR");
    try
    {
        // ...
        adapter.Commit();
    }
    catch
    {
        adapter.Rollback();
        throw;
    }
}

方法 2:

using(TransactionScope scope = new TransactionScope())
{
    // ...
    scope.Complete();
}

您首选的方法是什么?为什么? (我正在使用适配器/2.6 .net/3.5)

I see there are two main options for managing transactions with llblgen.

Method 1:

using(DataAccessAdapter adapter = new DataAccessAdapter())
{
    adapter.StartTransaction(IsolationLevel.ReadCommitted, "TR");
    try
    {
        // ...
        adapter.Commit();
    }
    catch
    {
        adapter.Rollback();
        throw;
    }
}

Method 2:

using(TransactionScope scope = new TransactionScope())
{
    // ...
    scope.Complete();
}

What is your prefered method and why? (I'm using adapapter/2.6 .net/3.5)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾铮苏瑾 2024-07-25 10:27:04

我倾向于使用 TransactionScope 来管理事务,因为这就是它的设计目的,而 DataAccessAdapter 虽然能够创建事务,但主要是为 DataAccess 设计的。

为了更清楚一点,您可以使用 TransactionScope 来管理跨多个 DataAccessAdapter 的多个事务,而单个 DataAccessAdapter 似乎具有特定的范围。

例如:

using(TransactionScope ts = new TransactionScope())
{
    using(DataAccessAdapter d1 = new DataAccessAdapter())
    {
        //do some data access stuff
    }
    using(DataAccessAdapter d2 = new DataAccessAdapter())
    {
        //do some other data access stuff  
    }
    ts.complete();
}

另一个注释是 TransactionScope 是线程安全的,而 DataAdapter 则不是。

I would lean towards using TransactionScope for managing transactions as this is what it was designed for whereas the DataAccessAdapter, while it has the ability to create transactions is designed primarily for DataAccess.

To try and be a little clearer, you could use TransactionScope to manage multiple transactions across multiple DataAccessAdapters whilst a single DataAccessAdapter appears to have a specific scope.

For example:

using(TransactionScope ts = new TransactionScope())
{
    using(DataAccessAdapter d1 = new DataAccessAdapter())
    {
        //do some data access stuff
    }
    using(DataAccessAdapter d2 = new DataAccessAdapter())
    {
        //do some other data access stuff  
    }
    ts.complete();
}

Another side note is that TransactionScope is thread safe, where as DataAdapters are not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文