NHibernate,数据绑定到DataGridView,Lazy Loading和Session管理 - 需要建议 [英] NHibernate, Databinding to DataGridView, Lazy Loading, and Session managment - need advice

查看:160
本文介绍了NHibernate,数据绑定到DataGridView,Lazy Loading和Session管理 - 需要建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主要应用程序表单(WinForms)有一个DataGridView,它使用DataBinding和Fluent NHibernate来显示SQLite数据库中的数据。此表单在应用程序运行的整个时间都是打开的。



为了出于性能原因,我为所有数据库访问设置了约定DefaultLazy.Always()。



到目前为止,我发现完成这项工作的唯一方法是保持一个Session(让我们称之为MainSession),主要打开主窗体,所以NHibernate可以延迟加载用户随网格导航的新数据。



应用程序的另一部分可以在后台运行,并保存到数据库。目前,(经过相当大的努力)之后,我的方法是调用MainSession.Disconnect(),为每个Save创建一次性会话,并在完成Save之后再调用MainSession.Reconnect()。否则SQLite将抛出数据库文件被锁定的异常。



这似乎到目前为止运行良好,但过去的经验让我很紧张,很长一段时间(我遇到性能问题,当我试图使用一个单一的会话保存和负载 - 缓存填满,并陷入一切 - 见提交在我的NHibernate / SQLite项目中非常慢)。





如果这是一个糟糕的方法,有哪些选择?每当用户浏览网格时,我考虑开启和关闭我的主会话,但是对于我来说这是不是很明显 - 钩住网格中可能导致懒惰负载的每个事件?



我有一种n叨的感觉,试图以这种方式管理我自己的会话根本上是错误的做法,但是不清楚是什么正确的。



修改



我问了这个问题已经有一年多了,事实证明,在应用程序生命周期中开放的主要会话确实导致性能问题。



在SO上似乎有更多的NH用户这些天 - 有人想提出一个更好的方法吗?

解决方案

;-)

绊倒你的新问题让我想起以下内容:你明白了懒惰加载的原理,还是你错误的懒惰加载分页? NHibernate也提供了这个功能。



如果您只想在网格中显示一些定义的属性,这些属性当然在对象图中,我认为您应该使用获取的连接一次检索整个数据。如果数据的行数为高,您可以考虑分页 ,据我所知,它也可能使用DataGridView和Binding。

Lazy加载结果在多个数据库调用 - 在你的情况下,我认为至少每行一个。这似乎不是最好的解决方案。

如果你正在使用FetchType.Join的分页,你可以摆脱长时间运行的会话,所有的问题都应该解决。那么怎么样?


My main application form (WinForms) has a DataGridView, that uses DataBinding and Fluent NHibernate to display data from a SQLite database. This form is open for the entire time the application is running.

For performance reasons, I set the convention DefaultLazy.Always() for all DB access.

So far, the only way I've found to make this work is to keep a Session (let's call it MainSession) open all the time for the main form, so NHibernate can lazy load new data as the user navigates with the grid.

Another part of the application can run in the background, and Save to the DB. Currently, (after considerable struggle), my approach is to call MainSession.Disconnect(), create a disposable Session for each Save, and MainSession.Reconnect() after finishing the Save. Otherwise SQLite will throw "The database file is locked" exceptions.

This seems to be working well so far, but past experience has made me nervous about keeping a session open for a long time (I ran into performance problems when I tried to use a single session for both Saves and Loads - the cache filled up, and bogged down everything - see Commit is VERY slow in my NHibernate / SQLite project).

So, my question - is this a good approach, or am I looking at problems down the road?

If it's a bad approach, what are the alternatives? I've considered opening and closing my main session whenever the user navigates with the grid, but it's not obvious to me how I would do that - hook every event from the grid that could possibly cause a lazy load?

I have the nagging feeling that trying to manage my own sessions this way is fundamentally the wrong approach, but it's not obvious what the right one is.

Edit

It's been more than a year since I asked this question...and it turns out that keeping a main session open for the lifetime of the app has indeed led to performance problems.

There seem to be a lot more NH users on SO these days - anyone want to suggest a better approach?

解决方案

yeah it's me again. ;-)
stumbling upon your new question reminds me of the following: Did you understand the principle of lazy loading or are you mistaking lazy loading for pagination? NHibernate also provides functionality for that.

If you just want to display some defined properties within your grid that are of course within the object graph i think you should retrieve the whole data at once using 'fetched joins'. If the rowcount of the data is to high you can think about pagination, as far as i know its also possible using DataGridView and Binding.
Lazy Loading results in multiple database calls - in your case i'ld think at least one per row. This seems not to be the best performing solution.
If instead you are using paging with FetchType.Join you can get rid of the long running session and all your problems should be solved. So how about that?

这篇关于NHibernate,数据绑定到DataGridView,Lazy Loading和Session管理 - 需要建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆