问题描述
以下代码段在SQL Server 2008(SP1)上正常工作,但在Oracle 11g中,对会话的调用.BeginTransaction()引发异常,消息为``连接已是本地或分布式事务的一部分''(堆栈跟踪)如下所示).使用"NHibernate.Driver.OracleDataClientDriver".
The following code snippet works fine with SQL Server 2008 (SP1) but with Oracle 11g the call to session.BeginTransaction() throws an exception with the message ‘Connection is already part of a local or a distributed transaction’ (stack trace shown below). Using the '"NHibernate.Driver.OracleDataClientDriver".
还有其他人遇到吗?
using (var scope = new TransactionScope())
{
using (var session = sessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
// do what you need to do with the session
transaction.Commit();
}
scope.Complete();
}
Exception at: at NHibernate.Transaction.AdoTransaction.Begin(IsolationLevel isolationLevel)
at NHibernate.Transaction.AdoTransaction.Begin()
at NHibernate.AdoNet.ConnectionManager.BeginTransaction()
at NHibernate.Impl.SessionImpl.BeginTransaction()
at MetraTech.BusinessEntity.DataAccess.Persistence.StandardRepository.SaveInstances(List`1& dataObjects) in S:\MetraTech\BusinessEntity\DataAccess\Persistence\StandardRepository.cs:line 3103
Inner error message was: Connection is already part of a local or a distributed transaction
Inner exception at: at Oracle.DataAccess.Client.OracleConnection.BeginTransaction(IsolationLevel isolationLevel)
at Oracle.DataAccess.Client.OracleConnection.BeginDbTransaction(IsolationLevel isolationLevel)
at System.Data.Common.DbConnection.System.Data.IDbConnection.BeginTransaction()
at NHibernate.Transaction.AdoTransaction.Begin(IsolationLevel isolationLevel)
推荐答案
此处概述了仅使用事务范围的问题: NHibernate FlushMode查找前自动不刷新
The problem with using only the transaction scope is outlined here:NHibernate FlushMode Auto Not Flushing Before Find
看来nhibernate(带有Oracle方言的v3.1和11g db w/opd.net v2.112.1.2)需要它自己的事务来避免刷新问题,但是我无法使事务作用域正常工作休眠交易.
It appears nhibernate (v3.1 with oracle dialect and 11g db w/opd.net v2.112.1.2) requires it's own transactions to avoid the flushing issue but I haven't been able to get the transaction scope to work with the nhibernate transactions.
我似乎无法使它起作用:(这可能是nhibernate或odp.net中的缺陷,不确定...
I can't seem to get it to work :(this might be a defect in nhibernate or odp.net, not sure...
在这里发现了相同的问题: NHibernate 3.0:TransactionScope和自动刷新
found same problem here:NHibernate 3.0: TransactionScope and Auto-Flushing
已修正:找到了解决方案!通过输入"enlist = dynamic;"到我的oracle连接字符串中,问题已解决.我已经可以同时使用nhibernate事务(以解决刷新问题)和事务范围:
FIXED: found a solution! by putting "enlist=dynamic;" into my oracle connection string, the problem was resolved. I have been able to use both the nhibernate transaction (to fix the flush issue) and the transaction scope like so:
ISessionFactory sessionFactory = CreateSessionFactory();
using (TransactionScope ts = new TransactionScope())
{
using (ISession session = sessionFactory.OpenSession())
using (ITransaction tx = session.BeginTransaction())
{
//do stuff here
tx.Commit();
}
ts.Complete();
}
我检查了我的日志文件,发现了这一点:2011-06-27 14:03:59,852 [10]调试NHibernate.Impl.AbstractSessionImpl-参与DTC事务:可序列化
I checked my log files and found this:2011-06-27 14:03:59,852 [10] DEBUG NHibernate.Impl.AbstractSessionImpl - enlisted into DTC transaction: Serializable
在连接上执行任何SQL之前.我将进行单元测试以确认正确执行.我不太确定是什么可序列化告诉我的
before any SQL was executed on the connection. I will unit test to confirm proper execution. I'm not too sure what serializable is telling me though
这篇关于Oracle 11g的NHibernate TransactionScope问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!