Question

I'm using Castle ActiveRecord in a warehouse project. I have several tables that get frequently updated: infeed, stack, stack_location. If a lot is happening (infeed is added, stacks are formed) sometimes the id that is set on an object after calling CreateAndFlush is the ID from another table. The database is MySQL, the ID columns are int(11) not null auto_increment primary key. I use PrimaryKeyType.Native for the ID property.

I also suffered from the following issue: How to retrieve the last insert ID in Castle ActiveRecord? but only with my infeed table (and I normally have something like 4 stacks / infeed). I added a catch to that situation with a 5000ms sleep and a SaveAndFlush afterwards, which makes sure I get an ID at that point.

I need to call flush directly because I need the ID to write it to a PLC which will hand it to me at a later point. My application is multi-threaded, but if I'm correct there's just one thread writing to the database when all of this goes wrong. I don't have anything marked as volatile, there are no locks in place preventing database access from multiple threads, but I assume Castle ActiveRecord does locking where appropiate.

Était-ce utile?

La solution

As already mentioned in a comment on my question:

I hate to answer my own questions, but seems I've found that I'm doing it all wrong. Apparently, nHibernate is intentionally not thread-aware. Because of that I need to take care of the nHibernate sessions that are used inside different threads. With Castle ActiveRecord the solution seems to be to use 'using(new SessionScope())' blocks in threads to cater for this issue. If anyone has a better suggestion or a more clear explanation I'm obviously interested.

All issues I had have not returned anymore.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top