-->
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.  [ 3 posts ] 
Author Message
 Post subject: Session doesn't fetch the latest after using nativequery
PostPosted: Tue Mar 30, 2010 6:48 pm 
Newbie

Joined: Tue Mar 30, 2010 6:29 pm
Posts: 3
Session doesn't fetch the latest data from table after using nativequery to update the table. Can see the data being
updated after using the native query. But after that I've used the namedquery to fetch the data from table. It doesn't
refresh from database. Instead it always returns the existing object in session.

I have a scenario where in the table there are 20 records and 2500 concurrent users/threads are hitting the db and trying to update those 20 records.
No new inserts. Due to this we were having locking problems with the table. Table just tracks the user responses in

I've planned to batch the updates and send them in a batch. I've used createNativeQuery to update the table.
Had to use the native query to update the db counts.

1) String sql = "UPDATE TEST SET NEG_COUNT = NEG_COUNT + ?1, POS_COUNT = POS_COUNT + ?2 WHERE ID = ?3";
Query query = session.createNativeQuery(sql);
query.setParameter(1, 1);
query.setParameter(2, 1);
query.setParameter(3, 1);
query.setFlushMode(FlushModeType.COMMIT);
int rows = query.executeUpdate(); // updates the db

2) Query query = session.createQuery("from TEST where id in (:id)");

Step does not reflect the latest data that was updated in step1.

Any ideas?
Thanks


Top
 Profile  
 
 Post subject: Re: Session doesn't fetch the latest after using nativequery
PostPosted: Wed Mar 31, 2010 2:36 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Instead it always returns the existing object in session.


This is expected and documented in http://docs.jboss.org/hibernate/stable/ ... tch-direct

Quote:
This means that manipulating data directly in the database (using the SQL Data Manipulation Language (DML) the statements: INSERT, UPDATE, DELETE) will not affect in-memory state.


The possible solutions are:

* Call session.evict() before the second query
* Call session.evict(entity) before the second query
* Use session.refresh(entity) which has been designed with this use case in mind:

Quote:
This method is, however, useful in certain special circumstances. For example
...
* after executing direct SQL (eg. a mass update) in the same session


Top
 Profile  
 
 Post subject: Re: Session doesn't fetch the latest after using nativequery
PostPosted: Wed Mar 31, 2010 3:57 pm 
Newbie

Joined: Tue Mar 30, 2010 6:29 pm
Posts: 3
Thanks for the reply.
I was using JPA's Entitymanager and not the hibernate session. But using Hibernate's JPA implementation.
Entitymanager does not have evict().

Thanks
Krish


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