NHibernate database call count?

拈花ヽ惹草 提交于 2019-12-11 16:34:07

问题


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

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).

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.

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

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 -

customer customeraddress order orderstate orderDetail

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()
            };
        }

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.


回答1:


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

This Article at JavaLobby gives some details.




回答2:


You can use log4net to gather that info.

Logging NHibernate with log4net



来源:https://stackoverflow.com/questions/7667092/nhibernate-database-call-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!