-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate Cache and Performance Questions
PostPosted: Thu Jan 06, 2005 3:09 pm 
Newbie

Joined: Wed Oct 20, 2004 3:53 pm
Posts: 1
Hi, All

We have a middleware runing on TOMCAT and Oracle9i database. We have 2 servlets - producer and consumer servlet. The Producer servlet receives a XML file from client and instantialize an object then call our java class to build prepared statement to save into database. The Consumer servlet receives the query requirement from client and runs a sql statement and generates XML message and return it back to the client. Because the consumer's query always query for the latest object that producer generated, so we have a cache which stores the objects the producer generated. When the consumer query an object, the consumer search the cache first. The consumer query the database only if cache missing.

Now we are going to use some open source product for our XML and java object mapping. We inverstigated JAXB, Castor and Hibernate and we decided using Hibernate. But we have a couple questions to ask:

1. The Hibernate implementation is slower than our handcoded JDBC statement. (see code below) We are using the HibernateUtil downloaded from CaveatEmptor.
2. We still want to implement the strategy of caching the lastest object the producer generated. As hibernate make the object persistent, can we load this object from the persistent layer in consumer servlet, then call session.flush() and call session.clear()? Or should we use our own cache like we did before? We read something about the Hibernate second level cache, is it for this kind of purpose and how to configure and use it?

Any hints or Hyperlinks are appreciated.

Following is the pseodo code:


public class ProducerWithHibernate extends HttpServlet {
/**
* Server cache for quick retrieval of Latest Objects. This hashtable is
* shared with the ConsumerWithHibernate Servlet via the Servlet Context.
*/
private Hashtable i_xmlDocumentCache;

private HibernateUtil i_hbUtil;
/**
* Servlet init method.
* @exception ServletException if an exception occurs.
*/
public void init() throws ServletException {
// Get the XML Document Cache from the Servlet Context for access by the
// Consumer Servlet
i_xmlDocumentCache = (Hashtable)context.getAttribute("ObjectCache");
i_hbUtil = new HibernateUtil();
}



/**
* Servlet doPost method.
*/
public void doPost(HttpServletRequest p_req, HttpServletResponse p_res)
throws ServletException, IOException {
long startTime, parseTime = 0, commitTime = 0, writeTime = 0, stopTime;
startTime = System.currentTimeMillis();
//parse xml message and instantialize Hibernate Objects
//

// At this point, we have Hibernate objects.
// So cache the ObjItemLoc Hibernate object in i_xml_DocumentCache.
i_xmlDocumentCache.put(new Long(hbObjId),hbObj);
parseTime = System.currentTimeMillis();

//Make Hibernate Objects Persistent
i_hbUtil.beginTransaction() ;
i_hbUtil.getSession().saveOrUpdate(hbObj);

stopTime = System.currentTimeMillis();
System.out.println(i_className +
"->" +
Thread.currentThread().getName() +
": parse time = " +
(parseTime-startTime) +
" elapsed time = " +
(stopTime-startTime));

}


/**
*
*/
public void destroy() {
i_hbUtil.commitTransaction();
i_hbUtil.closeSession();
}
}

public class ConsumerWithHibernate extends HttpServlet {

private Hashtable i_xmlDocumentCache;

public void init() throws ServletException {

// The cache for quick retrieval of XML Documents.
i_xmlDocumentCache = new Hashtable();

context.setAttribute("xmlDocumentCache", i_xmlDocumentCache);

}


public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
boolean getFromCache = false;

//Get Hibernated Object from Cache
HibernateObj hbObj = (HibernateObj)i_xmlDocumentCache.get(new Long(requestParameter));
//Can we get the Hibernated Object from the persistent layer???

if(!getFromCache){
//retrieve from database
}



Hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<!-- Don't forget to copy your JDBC driver to the lib/ directory! -->
<!-- Settings for a remote Oracle9/10g database.-->
<property name="dialect">net.sf.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:ORAD</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>

<!-- Use the C3P0 connection pool.-->
<property name="c3p0.min_size">3</property>
<property name="c3p0.max_size">5</property>
<property name="c3p0.timeout">1800</property>

<!-- Use EHCache but not the query cache. -->
<property name="cache.provider_class">net.sf.ehcache.hibernate.Provider</property>
<property name="cache.use_query_cache">false</property>
<property name="cache.use_minimal_puts">false</property>

<!-- hbObj mapping files. -->
<mapping resource="hibernateObj.hbm.xml"/>
</session-factory>
</hibernate-configuration>


Thanks a lot
Phelix


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 06, 2005 3:17 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
enable the cache at the class level (see the reference guide)

_________________
Emmanuel


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