Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a readonly session in nHiberate?

Tags:

nhibernate

Is it possible to creat a readonly connection in nHibernate ?

Read-only : where nHibernate will not flush out any changes to the underlying database implicitly or explicitly.

When closing a nhibernate connection it does automatically flush out the changes to the persistent object.

Setting the flush mode to never is one way - but is reversable (i.e some code can reset the flush mode).

like image 463
dotnetcoder Avatar asked Apr 20 '09 13:04

dotnetcoder


1 Answers

I think you've already found the solution, setting flush mode to never. Yes, it is changeable but even if it wasn't, code could simply create another session that had a different flush mode.

I think the appropriate solution is to suggest read-only with session.FlushMode = FlushMode.Never and enforce it by using a connection to the database that only has SELECT permissions (or whatever is appropriate for your situation). Maintaining separate ISessionFactory factories might help by allowing something like ReadOnlySessionFactory.Create().

like image 99
Stuart Childs Avatar answered Oct 28 '22 14:10

Stuart Childs