NHibernate和事务 - “事务没有连接,或被断开” [英] NHibernate and Transactions - "Transaction not connected, or was disconnected"

查看:258
本文介绍了NHibernate和事务 - “事务没有连接,或被断开”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码的一个代码片段,其中,在 transaction.Rollback()语句中引发了意外的,至少在我的部分异常。



异常的类型是 NHibernate.TransactionException ,消息是事务未连接或断开连接。 堆栈跟踪看起来像这样: NHibernate.Transaction.AdoTransaction.CheckNotZombied()at NHibernate.Transaction.AdoTransaction.Rollback()

  IEmployeeService employeeService = new EmployeeService(session); 
var people = ReadFromFile('c:\temp.csv');

(var person in people)
{
ITransaction transaction = session.BeginTransaction();
尝试
{
employeeService.Update(person);

employeeService.CheckForRecursion();

transaction.Commit();

catch(Exception exp)
{
if(!transaction.WasRolledBack)
transaction.Rollback();






CheckForRecursion使用一些SQL来查找引入的任何递归通过最后更新,如果是这样我想撤消。当递归被引入时,异常从SQL中冒出来,正好如此,我抓住它并尝试回滚。这就是当我遇到错误。

我已经包裹回滚在一个try catch所以整个事情可以继续,但我看到每个后续迭代的相同的例外for循环。



想法?这个模式是否正确?

解决方案

为什么不直接处理交易?

  using(ITransaction transaction = session.BeginTransaction())
{
employeeService.Update(person);

employeeService.CheckForRecursion();

transaction.Commit();



$ b

发生异常时,应该销毁会话。这个人的变化是在没有撤消的情况下做出的。当它不能被存储时,它在内存中是无效的。


The following is a representative snippet of my code where an unexpected, at least on my part, exception is being thrown at the transaction.Rollback() statement.

The exception is of type NHibernate.TransactionException and the message is "Transaction not connected, or was disconnected." And the stack trace looks like this: NHibernate.Transaction.AdoTransaction.CheckNotZombied() at NHibernate.Transaction.AdoTransaction.Rollback().

IEmployeeService employeeService = new EmployeeService(session);
var people = ReadFromFile('c:\temp.csv');

for (var person in people)
{
    ITransaction transaction = session.BeginTransaction();
    try
    {
        employeeService.Update(person);

        employeeService.CheckForRecursion();

        transaction.Commit();
    }
    catch(Exception exp)
    {
        if (!transaction.WasRolledBack)
            transaction.Rollback();
    }
}

The CheckForRecursion uses some SQL to look for any recursion introduced by the last update, if so I want to undo. When recursion has been introduced then an exception bubbles up from SQL, rightly so, and I catch it and attempt to rollback. That's when I encounter the error.

I have wrapped the rollback in a try catch so the whole thing can carry on but I see the same exception on each subsequent iteration of the for loop.

Ideas? Is this pattern correct?

解决方案

Why don't you just dispose the transaction?

using (ITransaction transaction = session.BeginTransaction())
{
    employeeService.Update(person);

    employeeService.CheckForRecursion();

    transaction.Commit();
}

When an exception occurs, you should destroy the session. The changes had been made on the person, where it is not undone. When it can't be stored, it is not valid in memory.

这篇关于NHibernate和事务 - “事务没有连接,或被断开”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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