-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Why does a IQuery.List result in a dirty session?
PostPosted: Wed Apr 14, 2010 1:02 pm 
Newbie

Joined: Thu Sep 17, 2009 11:22 am
Posts: 10
Can someone tell me why the following code results in a dirty session?

Code:
   
    using (ITransaction t = CurrentSession.BeginTransaction())
    {
        IQuery query = CurrentSession.CreateQuery("from SensorHistory srh "
                                + "where srh.Sensor = :sensor "
                                + "and srh.ReadingTimeUTC between :startDate and :endDate");
        query.SetParameter("sensor", sensor);
        query.SetParameter("startDate", startDateUTC);
        query.SetParameter("endDate", endDateUTC);

        var list = query.List<SensorHistory>();

        t.Commit();
   }


If I check the "CurrentSession.IsDirty()" before query.List<SensorHistory>() it returns false but true after. The problem is these entities come from a large table so when the enclosing transaction is committed, it tries to write these entities back to the DB, which causes a table scan that takes a very LOONNGGG time.

Thanks,
Dave


Top
 Profile  
 
 Post subject: Re: Why does a IQuery.List result in a dirty session?
PostPosted: Wed Apr 14, 2010 7:04 pm 
Newbie

Joined: Thu Sep 17, 2009 11:22 am
Posts: 10
This seems to be a more general problem. Whenever we query entities from the DB, NHibernate updates all values for the entity when the transaction is committed, even though none of the attributes are changed...at least by my code.


Top
 Profile  
 
 Post subject: Re: Why does a IQuery.List result in a dirty session?
PostPosted: Thu Apr 15, 2010 2:04 am 
Beginner
Beginner

Joined: Fri Feb 27, 2009 6:07 am
Posts: 38
Location: Moscow,Russia
http://nhforge.org/doc/nh/en/index.html#manipulatingdata-flushing
Use FlushMode.


Top
 Profile  
 
 Post subject: Re: Why does a IQuery.List result in a dirty session?
PostPosted: Thu Apr 15, 2010 10:28 am 
Newbie

Joined: Thu Sep 17, 2009 11:22 am
Posts: 10
Thanks, but FlushMode won't help me. I'm not modifying any of the entities I'm fetching. I simply do a query with no update. When I commit, any entity I've fetched is written back weather I've updated it or not.


Top
 Profile  
 
 Post subject: Re: Why does a IQuery.List result in a dirty session?
PostPosted: Thu Apr 15, 2010 11:11 am 
Newbie

Joined: Thu Sep 17, 2009 11:22 am
Posts: 10
k


Top
 Profile  
 
 Post subject: Re: Why does a IQuery.List result in a dirty session?
PostPosted: Fri Apr 16, 2010 4:46 am 
Beginner
Beginner

Joined: Fri Feb 27, 2009 6:07 am
Posts: 38
Location: Moscow,Russia
Test like this:
Code:
using (ITransaction t = CurrentSession.BeginTransaction())
    {
        CurrentSession.FlushMode = FlushMode.Never;

        IQuery query = CurrentSession.CreateQuery("from SensorHistory srh "
                                + "where srh.Sensor = :sensor "
                                + "and srh.ReadingTimeUTC between :startDate and :endDate");
        query.SetParameter("sensor", sensor);
        query.SetParameter("startDate", startDateUTC);
        query.SetParameter("endDate", endDateUTC);

        var list = query.List<SensorHistory>();

        t.Commit();
   }


or without Transaction:
Code:
    IQuery query = CurrentSession.CreateQuery("from SensorHistory srh "
                                + "where srh.Sensor = :sensor "
                                + "and srh.ReadingTimeUTC between :startDate and :endDate");
        query.SetParameter("sensor", sensor);
        query.SetParameter("startDate", startDateUTC);
        query.SetParameter("endDate", endDateUTC);

        var list = query.List<SensorHistory>();


Top
 Profile  
 
 Post subject: Re: Why does a IQuery.List result in a dirty session?
PostPosted: Fri Apr 16, 2010 10:36 am 
Newbie

Joined: Thu Sep 17, 2009 11:22 am
Posts: 10
I think you've missed the point of my question. I want to know why this is happening. ASAIK, this should not happen if I only read an entity and do not modify it.

I realize I can manipulate the FlushMode but that's not the point.


Top
 Profile  
 
 Post subject: Re: Why does a IQuery.List result in a dirty session?
PostPosted: Sat Apr 17, 2010 3:03 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Have a look at this: http://nhforge.org/doc/nh/en/index.html#events. I think it's the FindDirty method you need. Implement an interceptor (there's n EmptyInterceptor for start) and set a breakpoint in that method. You should be able to figure out why hibernate considers your object dirty there. In my experience there is something in your setters, that makes changes to the object.

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.