-->
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.  [ 9 posts ] 
Author Message
 Post subject: Using JCS in Hibernate
PostPosted: Thu Sep 11, 2003 9:03 am 
Beginner
Beginner

Joined: Tue Sep 09, 2003 5:28 pm
Posts: 33
Location: Cincinnati, OH
Hi all,

In our database, there is a table called codsc. We access the codsc table
many times in all parts of our application. The data rarely changes,
so it is a good candidate for caching.

The primary key of codsc is id, code.
The columns we need cached are desc, dstat.

Basically we want the JCS to do is:

- We need to be able to request the data by primary key.

- We want the first request to actually retrieve the data.

- After the first request, the cached data should be returned.

- We do not need to write the data back out to the database.

- It would be nice if the cached data would expire after a period of time,
and refresh the data from the database.

I found out there is JCS in Hibernate, but I never used it before, I am not sure
if it fits my project or not? Did anyone try JCS in Hibernate before? And could
you give me an simple example how to do it?

Thank you all so much!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 9:24 am 
Beginner
Beginner

Joined: Tue Sep 09, 2003 5:28 pm
Posts: 33
Location: Cincinnati, OH
Hi,

I tried to put <jcs-cache usage="read-only" /> to the .hbm.xml file, and how do I test the cache works? and How do I know the data is from the cache not the database?

Thanks a lot!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 10:23 am 
Newbie

Joined: Wed Aug 27, 2003 2:53 pm
Posts: 14
Location: New Jersey
Enable debugging for hibernate's cache package by adding the following to your log4j properties . . .

log4j.logger.net.sf.hibernate.cache=debug

Then you should see "Cache new" messages when objects are added to the cache and "Cache hit" messages when an object is found in the cache (instead of going to the DB).

Sarah


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 12:04 pm 
Beginner
Beginner

Joined: Tue Sep 09, 2003 5:28 pm
Posts: 33
Location: Cincinnati, OH
Thank you Shand1216 for replying!

I enabled the debug, but I did not see "Cache new" or "Cache hit", here are the messages that was showed:

--------------------------------------------------------------------

11:48:08,858 DEBUG ReadWriteCache:24 - Cache lookup: com.hibernate.AliasKey@6ea0649b
11:48:08,928 DEBUG ReadWriteCache:37 - Cache miss: com.hibernate.AliasKey@6ea0649b
11:48:09,048 DEBUG ReadWriteCache:56 - Caching: com.hibernate.AliasKey@6ea0649b
11:48:09,078 DEBUG ReadWriteCache:65 - Cached: com.hibernate.AliasKey@6ea0649b
11:48:09,088 DEBUG ReadWriteCache:24 - Cache lookup: com.hibernate.AliasKey@b15593fa
11:48:09,108 DEBUG ReadWriteCache:37 - Cache miss: com.hibernate.AliasKey@b15593fa
11:48:09,188 DEBUG ReadWriteCache:56 - Caching: com.hibernate.AliasKey@b15593fa
11:48:09,209 DEBUG ReadWriteCache:65 - Cached: com.hibernate.AliasKey@b15593fa

----------------------------------------------------------------------

And also in my cache.ccf, I use

jcs.default=DC
jcs.region.com.hibernate.Alias=DC

it will show me that:

-------------------------------------------------------------------------

11:48:03,240 WARN OptionConverter:135 - Could not find value for key jcs.auxiliary.DC
11:48:03,240 ERROR CompositeCacheConfigurator:359 - Could not instantiate auxFactory named "DC".

--------------------------------------------------------------------------

Do you know what I need to change?

Here is my .hbm.xml file

*****************************************************
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.hibernate.Alias" table="alias">
<jcs-cache usage="read-write"/>
<composite-id name="id">
<key-property name="firm" column="firm"/>
<key-property name="userid" column="userid"/>
<key-property name="alias" column="alias"/>
</composite-id>

<property name="sub_client" column="sub_client"/>
<property name="update_ident" column="update_ident"/>
</class>

</hibernate-mapping>
<!-- parsed in 10ms -->
**********************************************************

Here is the code I load the object:
**********************************************************

public void listAlias() throws HibernateException
{
Session session = sessionFactory.openSession();

Iterator iter = session.iterate("from com.hibernate.Alias as alias");

while (iter.hasNext())
{
Alias myAlias = (Alias) iter.next();
System.out.println("-------client is----" + myAlias.getClient());
System.out.println("-------subclient is----" + myAlias.getSub_client());
session.evict(myAlias);
}

session.close();
}

**********************************************************
Thanks for helping!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 12:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
use nonstrict-read-write.


its simply too difficult for me to explain all the rules of read-write strategy in a forum post


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 1:51 pm 
Beginner
Beginner

Joined: Tue Sep 09, 2003 5:28 pm
Posts: 33
Location: Cincinnati, OH
Thank you Gavin!

I tried nonstrict-read-write, it did show me "Caching new: com.hibernate.AliasKey@b15593fa" message, so it seems working.

But just for testing purpose, I first call listAlias() function to load all data from database, and then call update function to update two fileds of the table, then call listAlias() again, I expected to see unchanged data (since it is from cache), but it gave me the changed data, I am not sure if that is correct? Or I did something wrong?


update function:
----------------------------------------------------------------------------
public void update(Alias alias) throws HibernateException
{
Session session = sessionFactory.openSession();
Transaction tx = null;


try{
tx = session.beginTransaction();
session.update(alias);
tx.commit();
}
catch (HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally{
session.close();
}
}
------------------------------------------------------------------------------

listAlias function:

-------------------------------------------------------------------------------

public void listAlias() throws HibernateException
{
Session session = sessionFactory.openSession();

Iterator iter = session.iterate("from com.rippe.lms.hibernate.Alias as alias");

while (iter.hasNext())
{
Alias myAlias = (Alias) iter.next();
System.out.println("-------client is----" + myAlias.getClient());
System.out.println("-------subclient is----" + myAlias.getSub_client());
session.evict(myAlias);
}

session.close();
}
---------------------------------------------------------------------------------


Main()
---------------------------------------------------------------------------------

hiberTest.listAlias();

AliasKey aliasKey = new AliasKey(new BigDecimal(0), "test", "test");
Alias myAlias = hiberTest.findByPrimaryKey(aliasKey);

myAlias.setClient("6000");
myAlias.setSub_client("100");

hiberTest.update(myAlias);

hiberTest.listAlias();

----------------------------------------------------------------------------------

Thanks a lot!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 1:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Of course not!!

Hibernate's cache is aware of changes made by Hibernate itself.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 2:40 pm 
Beginner
Beginner

Joined: Tue Sep 09, 2003 5:28 pm
Posts: 33
Location: Cincinnati, OH
Okay, I see.

So if I made changes using JDBC connection directly, Hibernate cache won't be able to know it?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 11:19 am 
Beginner
Beginner

Joined: Tue Sep 09, 2003 5:28 pm
Posts: 33
Location: Cincinnati, OH
Hi,

Someone told me to enable hibernate.show_sql to true, if it shows sql statements, that means data is from database, if not which to say the data is from Cache. I tried this, but it always gave me sql statements, so that means all data is from database? I did use <jcs-cache usage="nonstrict-read-write"/> in my .hbm.xml file, I don't know what's wrong with it?

Thanks a lot!


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