Ran into a small issue this morning. Posting the solution here should help me find it next time in a jiffy.
I was performing some database validation – I needed to ensure that x object with y name does not already exist in the DB before submitting it, and more stuff, etc. I have to query the DB to see if this value exists or not. I’m using Castle Active Record and I’m using the Repository pattern to wrap my Active Record calls. I’m also utilizing the ActiveRecordMediator<T> class to help keep the AR (Active Record) DB calls out of the AR storage classes.
If everything was golden, I’d update the record using ActiveRecordMediator’s Update call. Otherwise some error handling took place. When the record was updated I had to hit the DB again. When this happened I got this exception:
NonUniqueObjectException: a different object with the same identifier value was already associated with the session
Oh boy. Fun.
Anyway, this seems to be a scoping issue as once my validation was occuring I was able to grab onto a record and then NHibernate cached that for me. Unfortunately when I went out of scope, NHibernate supposedly cleans up and gets rid of this cached object (from what I’ve read – if anyone had any more info on this please reply). However, later in the method call I perform a very similar call, and at this point the exception is thrown.
I fixed this by evicting the object directly after I used it. I just needed to check one value on the object and I never needed it again until (if and only if) I’d then update it.
The code to do this was as such:
ActiveRecordMediator<T>.Evict(instance);
Where the instance was the instance that i needed to cleanup.
After I performed an eviction from the session, the update worked perfectly.
Here is one link that helped me get started down this path to the solution.
Leave a Reply
You must be logged in to post a comment.