-->
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.  [ 6 posts ] 
Author Message
 Post subject: Get Object from DataBase instead of Cache...
PostPosted: Wed Jul 04, 2007 4:20 am 
Beginner
Beginner

Joined: Mon Mar 26, 2007 5:47 am
Posts: 22
Hi,

How can I tell NHibernate to explicitly get and object from the DataBase, and not from the cache?

For instance:
1. User A uses the object MyCompany with ID = 5.
2. User A stops using this object (MyCompany = Nothing)
3. User B opens MyCompany with ID = 5, makes changes to it, and saves it to the database.
4. User A uses again MyCompany 5, but doesn't get the changes that User B has made in the meantime!! Although: They need the updated object!


We're using the Get-method to retrieve the object (objSelected = SessionManager.Session.Get(Of T)(intID) )

Does anybody knows how to do this? Any help will be appreciated.

Thanks a lot in advance,


Pieter





Hibernate version: 1.2.0.4000

Mapping documents:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Ghost.clsEntreprise, Ghost" table="tblEntreprises" dynamic-update="true" lazy="false">
<id name="EntrepriseID" column="EntrepriseID" type="Int32">
<generator class="identity" />
</id>
<property name="Nom" column="Nom" type="String" length="50" />
<property name="Compte" column="CompteBancaire" type="String" length="30" />
<property name="Iban" column="Iban" type="String" length="30" />
</class>
</hibernate-mapping>


Name and version of the database you are using: SQL 2005 SP2


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 04, 2007 5:31 am 
Regular
Regular

Joined: Wed Apr 25, 2007 4:18 am
Posts: 51
Location: Belarus, Gomel
Hi pieterc!

1) You have to create new (empty or clear) session to discard cache. Of course it will not help if you use second level cache.
2) You may use session.Refresh(probablyOutdatedObject) - to get "current state" from underlying DB. But it may fail if object was removed from DB... So you may want to use query (it will first check DB for existence of requested objects) to get object...

_________________
WBR, Igor


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 04, 2007 7:22 am 
Beginner
Beginner

Joined: Mon Mar 26, 2007 5:47 am
Posts: 22
Hi,

Thanks you for your quick answer.

About the solutions:
1) Creating a new session: Won't this have a downside on the performance?

2) About the Refresh: Does this mean that, in case the object wasn't yet in the cache of NHibernate, NH will actually check two times to the DataBase (the first to to get it, the second time fot rhe Refresh).

Thanks a lot,

Pieter


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 04, 2007 12:33 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
For an IQuery or ICriteria, you can call SetForceCacheRefresh(true) to bypass the second level cache.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 04, 2007 6:04 pm 
Regular
Regular

Joined: Wed Apr 25, 2007 4:18 am
Posts: 51
Location: Belarus, Gomel
Hi pieterc!

AFAIK session creation is not so costly.
Moreover, extra large session (session having thousands of objects in its cache) will be really slow.
But main point is NHibernate session idiosyncrasy - it's exception intolerance - so you HAVE to close session in case of any error...

I'm not so experienced in NHibernate internal workflow, but I bet it doesn't hit DB twice while querying.
It will execute just one ADO.NET query, but it will process resultset items (result rows) differently - if object IS in session cache (seek by class and id) - NHibernate will return cached instance, if not - it will create one from resultset. At least it make sense :)

_________________
WBR, Igor


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 04, 2007 6:19 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
IgorK wrote:
AFAIK session creation is not so costly.

Agreed. Specifically, the doc says the following:
An ISession is an inexpensive, non-threadsafe object that should be used once, for a single business process, and then discarded

Pieter, you may want to read Chapter 10 of the docs. It deals with concurrency and whatnot.

_________________
Karl Chu


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