-->
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.  [ 5 posts ] 
Author Message
 Post subject: nhibernate updating after select... why?
PostPosted: Mon Jun 15, 2009 9:21 am 
Newbie

Joined: Mon Jun 15, 2009 9:12 am
Posts: 3
I'm using NHProf and I'm confused why NHibernate is updating an entity after it selects it and the transaction/session is committed/closed.

Here is the output from NHProf:
1. begin transaction with isolation level: unknown
2. SELECT job0_.jobid as jobid2_0_, job0_.name as name2_0_, job0_.tags as tags2_0_, job0_.dateCreated as dateCrea4_2_0_, job0_.lastModified as lastModi5_2_0_, job0_.nextRunDate as nextRunD6_2_0_, job0_.validationStatus as validati7_2_0_, job0_.validationErrors as validati8_2_0_, job0_.schedCron as schedCron2_0_, job0_.schedCronStart as schedCr10_2_0_, job0_.schedCronEnd as schedCr11_2_0_, job0_.schedCronExDates as schedCr12_2_0_, job0_.schedSimpleStart as schedSi13_2_0_ FROM Jobs job0_ WHERE job0_.jobid=@p0
3. commit transaction
4. UPDATE Jobs SET validationStatus = @p0 WHERE jobid = @p1 AND validationStatus=@p2

Here is the sql generated by the last Update statement:
Code:
UPDATE Jobs
SET    validationStatus = 1 /* @p0 */
WHERE  jobid = 'b6cdfe2e-59a8-4152-8d06-12dd8870faab' /* @p1 */
       AND validationStatus = 1 /* @p2 */


Seems completely unneccessary.


Top
 Profile  
 
 Post subject: Re: nhibernate updating after select... why?
PostPosted: Mon Jun 15, 2009 10:16 am 
Hi itamar82

Could you paste your mapping file and code to help us to find the problem?

[]s


Top
  
 
 Post subject: Re: nhibernate updating after select... why?
PostPosted: Mon Jun 15, 2009 10:41 am 
Newbie

Joined: Mon Jun 15, 2009 9:12 am
Posts: 3
Mapping:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
 
  <class name="MyApp.Domain.Job, MyApp" table="Jobs" lazy="true" optimistic-lock="dirty" dynamic-update="true">   
    <id name="Id" column="jobid" type="System.Guid">
      <generator class="guid"/>
    </id>

    <property name="Name" column="name" type="System.String" length="200" />
    <property name="Tags" column="tags" type="System.String" length="2000" />
    <property name="DateCreated" column="dateCreated" type="System.Int64" />
    <property name="LastModified" column="lastModified" type="System.Int64" />
    <property name="NextRunDate" column="nextRunDate" type="System.Int64" />
    <property name="ValidationStatus" column="validationStatus" type="System.Byte" />
    <property name="ValidationErrors" column="validationErrors" type="System.String" length="200" />
   
    <bag name="Tasks" lazy="true" cascade="all-delete-orphan" inverse="true">
      <key column="jobid" />     
      <one-to-many class="Producer.Core.Domain.Task, Producer.Core"/>
    </bag>
   
    <bag name="Results" lazy="true" cascade="all" order-by="StartTime" inverse="true">
      <key column="jobid" />
      <one-to-many class="Producer.Core.Domain.RunResult, Producer.Core"/>
    </bag>
     
  </class>
</hibernate-mapping>


Dao:

Code:
public class JobDao : Producer.Core.Interfaces.IDao
    {
        #region Fields

        protected ISession _session;
        protected ITransaction _transaction;
       
        protected static NLog.Logger _logger = NLog.LogManager.GetLogger("Producer.Data.NHibernate");

        #endregion

        #region Constructor(s)

        public JobDao()
        {
            _session = SessionManager.Instance.GetSession();

            _transaction = _session.BeginTransaction(System.Data.IsolationLevel.Serializable);

            _session.FlushMode = FlushMode.Commit;
           
        }

        #endregion

        #region Methods

        public Job GetJob(Guid id)
        {
            try
            {
                return _session.Get<Job>(id);
            }
            catch (Exception e)
            {
                _logger.TraceException("JobDao::GetJob() Id = '" + id + "' Exception", e);
            }

            return null;
        }


        public void Dispose()
        {           
            try
            {
                _transaction.Commit();
            }
            catch (Exception e)
            {
                _logger.TraceException("JobDao::Dispose() Transaction Exception", e);
            }
            finally
            {
                _transaction.Dispose();

                _transaction = null;               

                _session.Dispose();

                _session = null;

            }
        }

        #endregion


    }



Calling Code:

Code:
public Job GetJobById(Guid id)
        {
            Job job = null;

            using (var dao = IoC.Instance.Resolve<IDao>())
            {
                job = dao.GetJob(id);

                if (job == null)
                {
                    throw new JobNotFoundException();
                }

               
                return job;
            }           
        }


Top
 Profile  
 
 Post subject: Re: nhibernate updating after select... why?
PostPosted: Mon Jun 15, 2009 10:54 am 
Newbie

Joined: Mon Jun 15, 2009 9:12 am
Posts: 3
I think it has to do with optimistic-locking & dynamic-update="true". Anyway, this is weird, and I also don't understand why it runs the UPDATE query after the transaction commits


Top
 Profile  
 
 Post subject: Re: nhibernate updating after select... why?
PostPosted: Mon Jun 15, 2009 12:04 pm 
I think it's really strange.
Everything looks fine.
Make a simple test. Try remove optimistic-lock and dynamic-update.
Or maybe, try put select-before-update in your mapping file, to make NHibernate execute an SQL to verify
if some UPDATE needs to be executed.

[]s


Top
  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.