Programming, technology, and CRM – from a Belgian programmer exiled to Missouri
  • rss
  • Home
  • Soft Gallery
    • autosvnbackup.sh
    • VBScript Snippets
  • Contact Me
  • Welcome

NHibernate does not read uncommitted data!

Nicolas Galler | June 5, 2007

This came as a bit of a shock for me being used to work directly with the database but it makes so much sense when you think about the unit of work pattern implemented by NHibernate/Hibernate.
When you work directly with the database, an update query is immediately sent, and a subsequent select query (within the same transaction) will return the updated data, even though the transaction has not committed yet. But with Hibernate, the query gets “cached” as a unit of work. A subsequent query to retrieve that particular item (by id) will hit the level 1 cache and return the updated item, but a criteria query may not find it, if you just updated the criteria being searched on.
Basically:

ITransaction tx = sess.BeginTransaction();
Account a = new Account();
a.AccountName = "what a cutie";
sess.Save(a);
// now an HQL query "from Account a where a.AccountName='what a cutie',
// will not return the new account!

This is very apparent if you use the profiler and look at the order of the queries… the insert query gets sent after the select one!
In order to get this to work you need to add a call to sess.Flush():


ITransaction tx = sess.BeginTransaction();
Account a = new Account();
a.AccountName = "what a cutie";
sess.Save(a);
sess.Flush();
// now an HQL query "from Account a where a.AccountName='what a cutie',
// will return the new account

Had me pulling my hair for a moment, now I am smacking myself for not seeing the obvious. Too bad NHibernate does not flush automatically before submitting HQL queries. There might be an option for that.

Categories
Programming
Comments rss
Comments rss
Trackback
Trackback

« On Resharper MIME -> MAPI Conversion »

Leave a Reply

Click here to cancel reply.

Categories

  • Dojo (1)
  • Experiments (4)
  • Force.com (2)
  • Interesting (1)
  • Javascript (3)
  • MSCRM (1)
  • Programming (63)
  • Rant (3)
  • Saleslogix (41)
  • Tricks (8)
  • Uncategorized (32)

Post History

  • 2011
    • January (3)
    • February (2)
    • March (1)
  • 2010
    • January (3)
    • March (3)
    • April (2)
    • August (2)
    • October (4)
    • November (1)
    • December (2)
  • 2009
    • March (2)
    • April (1)
    • May (3)
    • June (3)
    • July (1)
    • September (3)
    • October (2)
    • December (5)
  • 2008
    • January (9)
    • February (4)
    • March (9)
    • April (1)
    • May (5)
    • June (8)
    • July (1)
    • August (2)
    • September (1)
    • November (1)
    • December (3)
  • 2007
    • January (3)
    • February (7)
    • March (1)
    • April (3)
    • May (6)
    • June (2)
    • July (1)
    • August (2)
    • September (5)
    • October (3)
    • November (5)
    • December (4)
  • 2006
    • January (2)
    • September (1)
    • November (3)
    • December (4)
  • 2005
    • April (1)

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox