Verena Praher
Verena Praher

Reputation: 1272

NHibernate - flush before/after select?

Currently I am working on a project, that was programmed by someone else and it is known that the NHibernate part might not be well implemented. I was asked to improve that during my internship, but I am also very new to NHibernate. I came across some code parts where there was a Flush after a select, in my opinion that is completely useless, am I right (question 1)? I read almost the complete NHibernate Documentation but I am not sure I understand everything.

Should I flush before a select (question 2)? My thoughts are that the data would be up to date when I select after a flush.

(Currently the program doesn't use any transactions at all - I see a lot of room for improvement there)

Upvotes: 0

Views: 505

Answers (1)

LeftyX
LeftyX

Reputation: 35587

NH Official documents say:

9.7.1. Flushing the Session

If you happen to be using the ITransaction API, you don't need to worry about this step. It will be performed implicitly when the transaction is committed. Otherwise you should call ISession.Flush() to ensure that all changes are synchronized with the database.

You should always use transactions, even for a read.

If you're not using transaction you should use Flush after a write.
It doesn't really make sens to Flush after a read.
Have a look a these two answers where everything is explained very well.

Upvotes: 1

Related Questions