NHibernate数据库调用计数? [英] NHibernate database call count?

查看:81
本文介绍了NHibernate数据库调用计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在运行时获取NHibernate对数据库的调用次数?

Is there a way to get at runtime the number of calls NHibernate is making to the database?

我知道我可以使用NHibernate Profiler(NHProf, http://nhprof.com/)手动查看这在调试时很重要-但是我想做的是在运行时获取NHibernate对数据库的实际调用次数,因此,如果它是50或300之类的荒谬数字(要确定的确切数字),我可以抛出异常).

I know I can use the NHibernate profiler (NHProf, http://nhprof.com/) to manually view this count while debugging - but what I'd like to do is at runtime to get the actual number of calls to the database NHibernate is making so I can throw an exception if it's a ridiculous number like 50 or 300 (exact number to be determined).

这将向开发人员表明,他/她需要使用急切"和未来"并调整NHibernate代码,以便不对数据库进行数百次调用.

This would be an indication to the developer that he/she needs to use 'Eager' and 'Future' and tweak the NHibernate code so hundreds of calls are not being made to the database.

在下面的示例中,我看到对数据库进行了284次调用-

Below I have an example where I'm seeing 284 calls being made to the database -

我们可以修复此代码-因此,我不寻求有关如何使此NHibernate代码更好的解决方案.相反,我想实现一个系统,该系统将通知开发人员他们需要调整Nhibernate代码.

We can fix this code - so I'm not looking for a solution on how to make this NHibernate code better. I would like instead to implement a system that would notify developers they need to tweak the Nhibernate code.

示例-假设我们有以下模型/数据库-

Example - suppose we have the following model/db -

客户 客户地址 命令 订单状态 orderDetail

customer customeraddress order orderstate orderDetail

下面的代码对每个相关表的每个订单详细信息进行一次select调用,等等.在数据库中只有72个订单详细信息的情况下,我们最终对数据库进行了284次调用.

The code below makes one select call for each order detail to each of the related tables, etc... With only 72 order details in the db, we end up with 284 calls made to the database.

        var query = QueryOver.Of<OrderDetail>()
            .Where(c => c.UpdateTime >= updateTime);

        using (var context = _coreSessionFactory.OpenSession())
        {
            var count = query.GetExecutableQueryOver(context)
                .ToRowCountQuery()
                .FutureValue<Int32>();

            var items = query
                .OrderBy(a => a.UpdateTime).Desc
                .Skip(index ?? 0)
                .Take(itemsPerPage ?? 20)
                .GetExecutableQueryOver(context)
                .Fetch(c => c.Order.OrderState).Eager
                .Fetch(c => c.Order.Customer.CustomerAddress).Eager
                .Future();

            return new
            {
                Typename = "PagedCollectionOfContract",
                Index = index ?? 0,
                ItemsPerPage = itemsPerPage ?? 20,
                TotalCount = count.Value,
                Items = items.ToList().Select(c => new
                {
                    Typename = "OrderDetail",
                    c.Id,
                    OrderId = c.Order.Id,
                    OrderStateAffiliates = c.Order.OrderStates.First(n => n.Name == StateNames.California).AffiliateCount,
                    CustomerZipCode = c.Order.Customer.CustomerAddresses.First(n => n.StateName == StateNames.California).ZipCode,
                    CustomerName = c.Order.Customer.Name,
                    c.DateTimeApproved,
                    c.Status
                })
                .ToArray()
            };
        }

理解此订单/客户模型并对其进行改进并不重要-只是一个示例,因此我们了解了为什么我需要获取NHibernate对数据库的调用次数.

It's not important for this order/customer model to be understood and to improve it - it's just an example so we get the idea on why I need to get the number of calls NHibernate makes to the db.

推荐答案

可以将SessionFactory配置为收集统计信息.例如.成功交易的数量或打开的会话数量.

The SessionFactory can be configured to collect statistics. E.g. the number of successful transactions or the number of sessions that were opened.

JavaLobby上的文章提供了一些详细信息.

This Article at JavaLobby gives some details.

这篇关于NHibernate数据库调用计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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