SQL Server Compact(CE)是否在另一个内部支持RequiresNew事务作用域? [英] Does SQL Server Compact (CE) support a RequiresNew transaction scope inside another one?

查看:56
本文介绍了SQL Server Compact(CE)是否在另一个内部支持RequiresNew事务作用域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个使用SQL Server CE 3.5 SP1的非常简单的示例,在该示例中,我尝试在现有事务中使用新事务.

Here's a very simple example using SQL Server CE 3.5 SP1 where I attempt to use a new transaction inside an existing one.

using (var db = new MyDataContext(<connection string>))
using (var ts = new TransactionScope())
{
    db.Thing.InsertOnSubmit(new Thing());
    db.SubmitChanges();
    using (var ts2 = new TransactionScope(TransactionScopeOption.RequiresNew))
    {
        db.Thing.InsertOnSubmit(new Thing());
        db.SubmitChanges();   // exception here
        ts2.Complete();
    }
    [... do more stuff ...]
    ts.Complete();
}

这将导致错误无法在事务作用域中征用连接对象".在第二次调用"db.SubmitChanges()"时

This causes the error "The connection object can not be enlisted in transaction scope." on the second call to "db.SubmitChanges()"

这篇文章指出,SQL CE在一个事务中不支持两个连接,但这里我只有一个(除非SubmitChanges()创建另一个).

This article states that SQL CE does not support two connections inside one transaction, but I only have one here (unless SubmitChanges() creates another one).

上面的想法是,如果子事务提交了,那么即使外部事务失败,它也会保持提交状态.据我所知,这是 RequiresNew的预期用途.

The idea above is that if the sub-transaction commits then it stays committed even if the outer transaction fails. To the best of my understanding, this is the intended use of RequiresNew.

此MSDN文章声称SQL CE本身就支持嵌套事务,因此我认为以上应该是可能的.是吗?如果是这样,我如何修改代码以使其正常工作?

This MSDN article claims that SQL CE supports nested transactions as such, hence my thinking that the above should be possible. Is it? If so, how do I modify the code to make this work?

编辑:这篇MSDN文章与另一篇MSDN文章矛盾,并说不支持嵌套事务.也许是问题所在?

Edit: While this MSDN article contradicts the other MSDN article and says nested transactions are not supported. Perhaps that is the problem?

推荐答案

不,您不能使用嵌套事务.在您的SQL CE数据库上运行此代码,您将看到错误消息.

Nope, you cannot use nested transactions. Run this code on your SQL CE database and you will see the error message.

BEGIN TRANSACTION;
SELECT 'GOT 1';
BEGIN TRANSACTION;
SELECT 'GOT 2';

主要错误0x80004005,次要错误27994 Microsoft SQL Server Compact不支持嵌套事务.

Major Error 0x80004005, Minor Error 27994 Microsoft SQL Server Compact does not support nested transactions.

这篇关于SQL Server Compact(CE)是否在另一个内部支持RequiresNew事务作用域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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