Nhibernate should I flush session explicitly after “SaveOrUpdate”?

I just coded a simple test. My dummy class would auto flush when I had a single identity column. I wrote the same test though with a composite ID and I get the behavior you describe above.

You are definitely on to something here. I don't use composite keys, but the primary key was assigned and I also use a Version field. If I removed the version field and also changed to primary key to for ex "Generated by Identity" it works as expected, this must be a bug.

– Marius Jan 16 '10 at 17:07.

You should use different ISession instances for the insert and select methods. It's best if these sessions are in a using block so that they are properly disposed of and inside a database transaction: using (var session = sessionFactory.OpenSession()) using (var tx = session. BeginTransaction()) { // perform your insert here tx.Commit(); }.

Yeah like that :) – Mark Dickinson Jan 14 '10 at 18:10 2 I don't like this constraint. It should be perfectly acceptable to create a transaction and perform both read and writes inside the same transaction. – Marius Jan 14 '10 at 18:35 It is acceptable.

The important thing is to create and commit the ITransaction for each unit of work. You should be doing this anyway and as the documentation says, it automatically causes a flush. – Aaronaught Jan 16 '10 at 17:18 Hmm, this is giving me a headache.

I just refactored my code so that transactions are started explictly and commited for each unit of work. Now, if there already exists a unit of work when I create a new one (which means that the unit of works will share the same session, but both will invoke session. BeginTransaction(), I get an error if the outer unit of work tries to RollBack when the inner unit of work has completed the transaction (although both have requested their own transaction).

– Marius Jan 17 '10 at 11:06.

Assuming that your repository wither creates or takes a session that already exists. When you make the WorkItem, and commit it, it just goes on a list of jobs for that session to do. If your repository does not create a transaction and your commit method does not explicity commit the transaction.

Then the update or save will wait until the session closes, so although you can see the object that you created, you will not be able to get the scope identity, for instance, until you close the session or flush it. Certainly searchResult won't know anything about the object you created without a flush or closing transaction. There are perils with transactions the same as sessions in general but if you are making new objects you should be using explicit transactions.

NHProfiler will readily warn you about this.

I use explicit transactions, the whole integration test runs in a single session/transaction. I still don't see a good reason as to why nhibernate does not autoflush when seing that i'm performing a query. – Marius Jan 14 '10 at 18:32 Yeah, I'm not saying its ideal, I found out about it yesterday, when I had a child object that I had just added (and called session.

Update on the parent object) and yet the object's id had not been set. It seemed strange but like I said, NHProfiler will warn you about this. I opted to have my repository interface expose methods to begin and commit transactions on a current session, this way I am in theory not tied to nhibernate.

Cheers, Mark – Mark Dickinson Jan 14 '10 at 21:47.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions