-->
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: The list method does not return the updated value from DB
PostPosted: Thu Jan 20, 2005 4:16 pm 
Newbie

Joined: Thu Jan 20, 2005 3:22 pm
Posts: 2

The problem is: I´m working with a unique instance of a session, to execute a find (with the same hql query) for 2 times, because the values that my app get for the first find(), can be changed by another process after a time, and when one of the row that the find already returned in the first time, where changed by another process directly in the database, when my app execute find again (with the same hql query), find returns the same value that where returned in the first time, in others words find returns a value that was not the value stored in the database, but the value stored on the session cache.
If one row was inserted by another process directly in the database, and this was not returned for the first find, on the second find this object is added on the list returned by the find.
If one of the object on the list returned for the first find, was removed by another process directly in the database, the second find did not return this.
If I call the clear function on the Session object, between the 2 times, the second find returns the values stored on the database.
The problem only occurs when one row that was returned in the first find, was updated.

Hibernate version: 2.1.6

Mapping documents:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="br.com.voicetechnology.db.sys.User" table="tbl_sys_user">
<id name="key" column="int_user_key" unsaved-value="null"><generator class="identity"/></id>
<property name="firstName" column="vch_first_Name"/>
<property name="lastName" column="vch_last_Name"/>
<property name="username" column="vch_username"/>
<property name="password" column="vch_password"/>
<property name="fone" column="vch_fone"/>
<property name="email" column="vch_email"/>
<property name="locale" column="vch_locale"/>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Session s = sf.openSession();
User u = null;
List l = null;
List l1 = null;
Iterator i = null;
try
{

l = s.find("from User");
i = l.iterator();
while(i.hasNext())
{
u = (User) i.next();
System.out.println(u.getFirstName());
}
Thread.sleep(20000); //Time to other process modify rows in DB
l = s.find("from User");
i = l.iterator();
while(i.hasNext())
{
u = (User) i.next();
s.refresh(u);
System.out.println(u.getFirstName());
}
} catch (HibernateException e)
{
e.printStackTrace();
}


Full stack trace of any exception that occurs:
does not occurs exceptions.

Name and version of the database you are using:
MySQL 4.0.17

The generated SQL (show_sql=true):
select user0_.int_user_key as int_user1_, user0_.vch_first_Name as vch_firs2_, user0_.vch_last_Name as vch_last3_, user0_.vch_username as vch_user4_, user0_.vch_password as vch_pass5_, user0_.vch_fone as vch_fone, user0_.vch_email as vch_email, user0_.vch_locale as vch_locale from tbl_sys_user user0_

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2005 4:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
If I call the clear function on the Session object, between the 2 times, the second find returns the values stored on the database.

So what's the problem? You just stated the solution...

A session acts as a cache for the scope of a unit of work (aka, the session's lifetime). As such, it is susceptible to statel-data issues as is well documented. The solution is to use the methods clear() and evict() to manage the internal session cache.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 21, 2005 8:20 am 
Newbie

Joined: Thu Jan 20, 2005 3:22 pm
Posts: 2
The problem is: when I use clear() or evict(), I´m chaging the state of my object to detached, and i do not want to do this, I want only bypass de session cache.

I Already change my code instead of use find(), I´m using Query, just to set the lockmode, because i saw on the documentation, when use LockMode = Read, the session cache must be by passed, but this not works.

q = s.createQuery("from User");
q.setLockMode("from User",LockMode.READ);
l = q.list();


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.