Thursday 15 February 2007

NHibernate - a couple of transaction related issues

A couple of issues with transactional updates

1. Timeout expired.

A timeout expired error was occurring when attempting to update the same record (and field) twice within a single transaction.
This only occurred when the database update was being performed.
Solution: When updating a record multiple times do not perform the Update until all field values have been updated.

2. Illegal attempt to associate a collection with two open sessions
This error was returned when attempting to update child records of a parent record as well as updating the parent record, where the child record was being read independent of the parent record.
e.g
Parent record links to Child1 and Child2
Parent record read (which implicity reads Child1 & Child2 and puts in list Parent.Children)
Child1 record is read independently.
Parent record Updated.
Child1 record Updated.
There is no need to explicity read and update the Child1 instance. The instance (from Parent) should be updated (in memory) instead.