我正在尝试在分布式事务下更新 DB2 数据库(通过 NHibernate),但它一直失败。
这是我写的代码:
public bool ExecuteUsingDTC(List<Func<bool>> tasks)
{
var result = false;
using (var scope = new TransactionScope(TransactionScopeOption.Required))
{
using (var session = sessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
//carry out db modification tasks
tasks.ForEach(task => { result = result && task.Invoke(); })
transaction.Commit();
}
scope.Complete();
}
return result;
}
我不断收到异常:
NHibernate.TransactionException was unhandled by user code
Message=Begin failed with SQL exception
Source=NHibernate
StackTrace:
at NHibernate.Transaction.AdoTransaction.Begin(IsolationLevel isolationLevel)
at NHibernate.Transaction.AdoTransaction.Begin()
at NHibernate.AdoNet.ConnectionManager.BeginTransaction()
at NHibernate.Impl.SessionImpl.BeginTransaction()
at DTCProofOfConcept_Repository.ExecuteUsingDTC(List`1 tasks) in C:\Project\Infrastructure\GlobalRepositories\DTCProofOfConcept_Repository.vb:line 20
at
InnerException: IBM.Data.DB2.DB2Exception
ErrorCode=-2147467259
Message=ERROR [HY011] [IBM] CLI0126E Operation invalid at this time. SQLSTATE=HY011
Source=IBM.Data.DB2
StackTrace:
at IBM.Data.DB2.DB2Connection.HandleError(IntPtr hHandle, SQL_HANDLE hType, RETCODE retcode)
at IBM.Data.DB2.DB2Transaction.set_AutoCommit(Boolean value)
at IBM.Data.DB2.DB2Transaction.BeginTransaction()
at IBM.Data.DB2.DB2Connection.BeginTransactionObject(IsolationLevel isolevel)
at IBM.Data.DB2.DB2Connection.BeginTransaction(IsolationLevel isolevel)
at IBM.Data.DB2.DB2Connection.BeginTransaction()
at IBM.Data.DB2.DB2Connection.System.Data.IDbConnection.BeginTransaction()
at NHibernate.Transaction.AdoTransaction.Begin(IsolationLevel isolationLevel)
InnerException: