-->
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: How to unuse cache, each time data read from cache
PostPosted: Wed Sep 28, 2005 4:24 am 
Newbie

Joined: Tue Aug 09, 2005 2:55 am
Posts: 5
Question description:

first I get the Sysuser and display it in my jsp page, it was correct . then I modify it and submit , the data sava into database successfully. then I get the same Sysuse obj second time. but this time the Sysuser obj is the old data before update. Obviously it invoke the cache obj . Then I begin to clear the cache use the statement such as :session.flush();
session.clear(); but I was no use!
How can I get the right Sysuser obj. And I find this question here and there. whether there are som way to not use the cache?
I'm waiting for your help! thanks!



Hibernate version:

hibernate-3.1beta3

Mapping documents:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.cglib.use_reflection_optimizer">true</property>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sigma</property>
<property name="hibernate.connection.username">sigma</property>
<property name="hibernate.connection.password">sigma</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="com/sigma/portal/db/Child.hbm.xml" />
<mapping resource="com/sigma/portal/db/Document.hbm.xml" />
<mapping resource="com/sigma/portal/db/Company.hbm.xml" />
<mapping resource="com/sigma/portal/db/Sysuser.hbm.xml" />
<mapping resource="com/sigma/portal/db/Filetype.hbm.xml" />
<mapping resource="com/sigma/portal/db/Parent.hbm.xml" />
<mapping resource="com/sigma/portal/db/Userole.hbm.xml" />
<mapping resource="com/sigma/portal/db/Depart.hbm.xml" />
<mapping resource="com/sigma/portal/db/Event.hbm.xml" />
<mapping resource="com/sigma/portal/db/Role.hbm.xml" />
<mapping resource="com/sigma/portal/db/Authorization.hbm.xml" />
<mapping resource="com/sigma/portal/db/Sysfunc.hbm.xml" />
<mapping resource="com/sigma/portal/db/Project.hbm.xml" />
</session-factory>
</hibernate-configuration>


Code between sessionFactory.openSession() and session.close():

the metho get obj:


public Sysuser getSysuser(String userid)
{
Sysuser user = null;
try {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
user = (Sysuser)session.get(Sysuser.class,userid);
session.flush();
session.clear();
tx.commit();
HibernateUtil.closeSession();
} catch (Exception e) {
e.printStackTrace();
}
return user;
}


The metho modi Sysuser:

public void modiSysuser(Sysuser user){
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
session.update(user);
session.flush();
session.clear();
tx.commit();
HibernateUtil.closeSession();
}

Full stack trace of any exception that occurs:

Name and version of the database you are using:

mysql-4.18

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 4:47 am 
Newbie

Joined: Wed Sep 28, 2005 4:07 am
Posts: 11
Calling update(user) does not change user. It only tells Hibernate, that "user" shall be again in persistent state. If you want to get the actual user-object you must call session.get(Sysuser.class,userid).

If this does not help, show me the code where the 2 Methods are called and when the user is changed.

_________________
Lars Fiedler


Top
 Profile  
 
 Post subject: I do just like you say,but it was no use!
PostPosted: Wed Sep 28, 2005 5:06 am 
Newbie

Joined: Tue Aug 09, 2005 2:55 am
Posts: 5
Here is the process I deal:

(1) modiUser.jsp

in this jsp I first invoke the sysuser and display it in the jsp.

the method is :
public Sysuser getSysuser(String userid)
{......
user = (Sysuser)session.get(Sysuser.class,userid);
.......
}

then I submit this modiUser.jsp to a servlet, in servlet I use the update method. the method is:

public void modiSysuser(Sysuser user){.....
session.update(user);
session.flush();
session.clear();.......
}

the the data is save it into the database. and I checked it has been updated.

(2) I invoke the modiUser.jsp again . but this time the jsp display the old obj . Not the obj I have updated.

Here is the question!!!!!!!!!!!!!!11


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 7:42 am 
Newbie

Joined: Wed Sep 28, 2005 4:07 am
Posts: 11
It is still not enough information. Please check the following:

- Set in Hibernate-Config.xml: <property name="show_sql">true</property>
- Set a breakpoint in both methods.
- Check if the calls to session.get() causes an SQL-Select
- Check if both Sessions are the same (==)
- Check if the user in session.update(user) is really the changed user.

_________________
Lars Fiedler


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 9:48 pm 
Newbie

Joined: Tue Aug 09, 2005 2:55 am
Posts: 5
I do just as you said:

Session is the same session.

This is MyHibernateUtil.java:

public class HibernateUtil {
public static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this thread has none yet
if (s == null) {
s = sessionFactory.openSession();
// Store it in the ThreadLocal variable
session.set(s);
}
return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}

}

This time I dispaly the hibernate sql:
I found flowing phenoment:
(1) first time in my modiUser.jsp I invoke the getSysuer is right. It display the right sql, Then I modi the data and save it. the Console also display the right update sql.
(2)then I execute modiUser.jsp. I found the hiberbate sql is not display in the cosole . but the modiUser.jsp display the old data that before modify.

obviously it invoke the cache . This time I changed my hibernate.cfg.xml and doesn't use use_query_cache but the problem is still unchange.

Here is my hibernate.cfg.xml content:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.cglib.use_reflection_optimizer">true</property>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sigma</property>
<property name="hibernate.connection.username">sigma</property>
<property name="hibernate.connection.password">sigma</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.transaction.flush_before_completion">true</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping resource="com/sigma/portal/db/Child.hbm.xml" />
<mapping resource="com/sigma/portal/db/Document.hbm.xml" />
<mapping resource="com/sigma/portal/db/Company.hbm.xml" />
<mapping resource="com/sigma/portal/db/Sysuser.hbm.xml" />
<mapping resource="com/sigma/portal/db/Filetype.hbm.xml" />
<mapping resource="com/sigma/portal/db/Parent.hbm.xml" />
<mapping resource="com/sigma/portal/db/Userole.hbm.xml" />
<mapping resource="com/sigma/portal/db/Depart.hbm.xml" />
<mapping resource="com/sigma/portal/db/Event.hbm.xml" />
<mapping resource="com/sigma/portal/db/Role.hbm.xml" />
<mapping resource="com/sigma/portal/db/Authorization.hbm.xml" />
<mapping resource="com/sigma/portal/db/Sysfunc.hbm.xml" />
<mapping resource="com/sigma/portal/db/Project.hbm.xml" />
</session-factory>
</hibernate-configuration>


Top
 Profile  
 
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.