Search code examples
c#nhibernaterowcount

Nhibernate unexpected row count rising after deletes


Problem:

I'm getting error:

Batch update returned unexpected row count from update; actual row count: 0; expected: 1

when some entity is deleted between fetch and delete. After any next delete row count and expected are rising for e.g.:

Batch update returned unexpected row count from update; actual row count: 11; expected: 12

Code:

var queueEntry = _session.Query<MyTable>().FirstOrDefault(x => x.Id == id);
if (queueEntry != null)
{
    _session.Delete(queueEntry);

    try
    {
        _session.Flush();
    }
    catch (Exception ex)
    {
        _session.Clear();
    }

    return 1;
}

Attempts:

I've tried few things with no success, I've added this lines in catch clause:

_session.Disconnect();
_session.Reconnect();

I've tried to wrap whole code I've putted here with transaction and instead of _session.Flush() I putted transaction.Commit().

Mappings:

This is mapping by code

public class MyTable : IEntity
{
    public virtual int Id { get; protected set; }
    public virtual string Type { get; set; }
    public virtual string Source { get; set; }
    public virtual string OperationType { get; set; }
    public virtual bool IsVisible { get; set; }
    public virtual DateTime? Timestamp { get; set; }
    public virtual string ContentId { get; set; }
    public virtual int Priority { get; set; }
}

Question:

Is there any way to reset this expected value, so I can continue removing values without exceptions?

Of course entities are removed from table, but I keep getting exceptions. Using empty catch clause for ignoring this exceptions is not a solution for me.

EDIT:

Addtional info: this table is queue for long running process, it has no child tables, just few things usefull for service. Posted mappings. It happens after some time, not after first delete.


Solution

  • I've managed some solution, maybe not best but working:

    catch (Exception ex)
    {
        _session = _session.SessionFactory.OpenSession();
    }
    

    if opening new session it's losing information about row count and works fine next time.