Wednesday, January 14, 2015

Spring, Hibernate, EhCache Recipe

A simple scenario explaining the usage and performance, when using EhCache along with Hibernate in a Spring environment. The performance results are taken using mySQL as the database.

[Download Sample Code] Please be informed that the size is about 10MB; as i have provided all the dependencies.

In this example, I need to retrieve close to 5,000 records in a single fetch and then cache this information. As usual, setup Spring contexts in your Spring configuration file. I have just one class, which is a Hibernate DAO, HibernateDoctorDAO.java. The dependency injection hierarchy is dataSource > sessionFactory > hibernateTemplate. hibernateTemplate is then injected into the HibernateDoctorDAO.java at runtime by the Spring Framework.

The implementations of each of these are:
dataSource > org.apache.commons.dbcp.BasicDataSource
sessionFactory > org.springframework.orm.hibernate3.LocalSessionFactoryBean
hibernateTemplate > org.springframework.orm.hibernate3.HibernateTemplate

The results clearly shows the difference in performance with EhCache enabled, even in this simple example:
PERFORMANCE COMPARISON (in seconds)
==================================
QUERY FETCH TIME (INITIAL): 0.599
QUERY FETCH TIME (HIBERNATE CACHE): 0.212
QUERY FETCH TIME (2ND LEVEL CACHE): 0.091
Version Reference > Spring-2.5, Hibernate-3.0, EhCache-1.4, mySQL-5.0

Instructions: Unzip the file and start by creating the database. Any mySQL database that is compatible with the MySQL Connector 5.0.8 is fine for this example. You will have to create the database data_explosion (USERNAME: root, PASSWORD: architect.2012) and the table DOCTOR_TABLE. You can use the provided doctor.sql as reference. Then run the java standalone com.sumsoft.spring.orm.hibernate.dataload.JDBCDataLoader to create and load a large number of arbitary records into the DOCTOR_TABLE. You can ignore or remove every invocation that is to PATIENT_TABLE, as this is only additional for this example. Then use spring_hibernate_cache.bat to run the application. Though not tested on UNIX, you may write a script similar to the batch file to run this application. 


Wednesday, January 7, 2015

JBoss Cache XSD Config in Eclipse

I was facing this issue in Eclipse Europa where the Spring configuration XML was not getting validated, when i included JBoss Cache related configuration in it. This is primarily because the Namespace and the Schema URI,
are not available at the respective locations, hence i guess these were not bundled in Eclipse Europa. I spent about 4-5 hours breaking my head on this issue before I found the solution in another blog.

The way to get your XML to be reported as validated is:
1. In Eclipse, Click on Window > Preferences.. > Web and XML > XML Catalog
2. Under XML Catalog, Click on User Specified Entries and then click on Add...
3. Create the following Entries/Locations

>>>>> [Replace pathto with the actual location of the JAR on your local machine.]
>>>>>> jar:file:pathto/spring-modules-cache.jar!/org/springmodules/cache/config/springmodules-cache.xsd
>>>>>> KEY TYPE Schema Location

>>>>> jar:file:pathto/spring-modules-cache.jar!/org/springmodules/cache/config/jboss/springmodules-jboss.xsd
>>>>> KEY TYPE Schema Location
>>>>> KEY http://www.springmodules.org/schema/cache/springmodules-jboss.xsd


4. Refresh the XML file in the Project Explorer and... all XML validation errors have gone!