Wednesday, November 28, 2012

What is Hibernate Caching?

In a typical application, you perform lot of operations like instantiate objects, load object from the database and so on. Sometime in multiuser application you may face a situation in handling multiple call of databases.

Hibernate offers caching functionality which is designed to reduces the amount of necessary database access. This is a very powerful feature if used correctly. It increases your application performance and works between your application and the database as it avoids the number of database hit as many as possible.

Hibernate Cache Types :

Hibernate uses different types of caches. Each type of cache is used for different purposes. Let us first have a look at this cache types.
  • First level cache
  • Second level cache
  • Query level cache

1. First level cache :

First-level cache is the session cache and is always Associates with the Session object. Hibernate uses this cache by default. The Session object keeps an object under its own cache before committing to the database. Here, it processes one transaction after another one, means wont process one transaction many times. Mainly it reduces the number of SQL queries it needs to generate within a given transaction. That is instead of updating after every modification done in the transaction, it updates the transaction only at the end of the transaction.

2. Second level cache :

Second-level cache is an optional cache and is always associates with the Session Factory object. The second-level cache can be configured on a per-class and per-collection basis and mainly responsible for caching objects across sessions. While running the transactions, in between it loads the objects at the Session Factory level, so that those objects will available to the entire application, don’t bounds to single user. Since the objects are already loaded in the cache, whenever an object is returned by the query, at that time no need to go for a database transaction. In this way the second level cache works.

Hibernate supports four open-source cache implementations named EHCache (Easy Hibernate Cache), OSCache (Open Symphony Cache), Swarm Cache, and JBoss Tree Cache.

Each cache has different performance, memory use, and configuration possibilities.
S.N.Cache NameDescription 
1EHCacheIt can cache in memory or on disk and clustered caching and it supports the optional Hibernate query result cache. 
2OSCacheSupports caching to memory and disk in a single JVM, with a rich set of expiration policies and query cache support. 
3warmCacheA cluster cache based on JGroups. It uses clustered invalidation but doesn't support the Hibernate query cache 
4JBoss CacheA fully transactional replicated clustered cache also based on the JGroups multicast library. It supports replication or invalidation, synchronous or asynchronous communication, and optimistic and pessimistic locking. The Hibernate query cache is supported

3. Query level cache :

Hibernate also implements a cache for query resultsets that integrates closely with the second-level cache. This is an optional feature and requires two additional physical cache regions that hold the cached query results and the timestamps when a table was last updated. This is only useful for queries that are run frequently with the same parameters.

Useful java Keytool Command

Generate a Java keystore and key pair :

keytool -genkey -alias mycert -keyalg RSA -keystore keystore.jks -keysize 1024
Generate a keystore and self-signed certificate :

 keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360 -keysize 2048
keytool command to view certificate details from keyStore :

keytool -list -v -keystore keystore.jks

Check a particular keystore entry using an alias:
keytool -list -v -keystore keystore.jks -alias mydomain

keytool command option is -printcert which prints details of a certificate stored in .cer file :
keytool -printcert -file test.cer

Export a certificate from a keystore:
keytool -export -alias mydomain -file mydomain.crt -keystore keystore.jks
 keytool -export -alias mydomain -keypass keypass -keystore keystore.jks -storepass jkspass -rfc -file keytool_crt.pem

Note: "keytool -export" command uses DER format by default. The "-rfc" option is to change it to PEM (RFC 1421) format.

Friday, November 2, 2012

How to install on Cent OS

The Basics - What is mod_jk?

The mod_jk connector is an Apache HTTPD module that allows HTTPD to communicate with Apache Tomcat instances over the AJP protocol.


1. Download the latest apache connector from
2. Untar the download by
         tar zxvf <filename>
3. Goto native directory of connector
         cd <connector dir>/native/
4. Run the scripts
Note: If you get any issue like "autocong" not installed then install following things:
       yum install autoconf
       yum install libtool

5. You need "httpd-devel" tools to build it. So make sure you have already installed it by
         yum list installed | grep httpd-devel    else install it "yum install httpd-devel"
6. From the native directory Run
      ./configure --with-apxs=/usr/sbin/apxs


Congrats!! Now you can see the module under your native directory.