我正在尝试将NHibernate与.NET的TransactionScope对象结合使用。到目前为止,我已经成功地使用了Oracle 11g和SQLServer2008R2,没有出现任何问题。然而,SQLCompact似乎落在了它的脸上。
using (var scope = new TransactionScope(TranactionScopeOption.Required))
{
using (var session = _sessionFactory.OpenSession())
{
// The line below throws. I also tried passing in System.Data.IsolationLevel.ReadCommitted to no avail
using (var txn = session.BeginTransaction())
{
// Perform insert
txn.Commit();
}
}
scope.Complete();
}
这将导致以下异常。我理解这意味着什么,但我不明白它为什么要创建嵌套事务。
NHibernate.TransactionException: Begin failed with SQL exception ---> System.InvalidOperationException: SqlCeConnection does not support nested transactions.
at System.Data.SqlServerCe.SqlCeConnection.BeginTransaction(IsolationLevel isolationLevel)
at System.Data.SqlServerCe.SqlCeConnection.BeginDbTransaction(IsolationLevel isolationLevel)
at System.Data.Common.DbConnection.System.Data.IDbConnection.BeginTransaction(IsolationLevel isolationLevel)
at NHibernate.Transaction.AdoTransaction.Begin(IsolationLevel isolationLevel)
at NHibernate.Transaction.AdoTransaction.Begin(IsolationLevel isolationLevel)
发布于 2011-11-15 00:18:05
您的代码可能产生2个事务:
1)new TransactionScope(TranactionScopeOption.Required)
2)session.BeginTransaction()
如果环境事务已经存在,TransactionScope.Required“使用环境事务。否则,它将在输入范围之前创建一个新事务。这是默认值”。您的环境事务可以保证在session.BeginTransaction()
被击中时具有一个环境事务,因此,使它成为一个嵌套事务。
如果事务作用域尚未完成,则其范围内的所有内容都将回滚。
发布于 2014-01-29 09:47:33
我认为答案就在于这个问题:The connection object can not be enlisted in transaction scope
“据猜测,TransactionScope需要升级为分布式事务或嵌套事务,CE不支持这两种事务。”
https://stackoverflow.com/questions/8127735
复制相似问题