如何在nHiberate中创建一个只读会话? [英] how to create a readonly session in nHiberate?
问题描述
是否可以在nHibernate中创建只读连接?
Is it possible to creat a readonly connection in nHibernate ?
只读:nHibernate不会隐式或显式刷新对基础数据库的任何更改.
Read-only : where nHibernate will not flush out any changes to the underlying database implicitly or explicitly.
关闭nhibernate连接时,它会自动清除对持久对象的更改.
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).
推荐答案
我认为您已经找到了解决方案,将刷新模式设置为从不.是的,它是可变的,但是即使不是,代码也可以简单地创建另一个具有不同刷新模式的会话.
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.
我认为适当的解决方案是使用与只有SELECT权限(或任何其他权限)的数据库的连接,对ReadOnlySessionFactory.Create()
的东西来维护单独的ISessionFactory工厂.
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()
.
这篇关于如何在nHiberate中创建一个只读会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!