Labels

.NET Job Questions About Java Absract class Abstract class Abstract Class and Interface Aggregation ajax aop apache ofbiz Apache ofbiz tutrial Association authentication autocad basics batch Binary Tree bootstrap loader in java build Builder design pattern C++ Job Questions caching CallableStatement in java certifications Chain of responsibility Design pattern charts check parentheses in a string Classes classloader in java classloading concept code quality collage level java program Composition concurrency Concurrency Tutorial Converting InputStream to String Core Java core java concept core java interview questions Core Java Interview Questions Core Java Questions core java tutorial CyclicBarrier in Java data structures database Database Job Questions datetime in c# DB Db2 SQL Replication deserialization in java Design Patterns designpatterns Downloads dtd Eclipse ejb example/sample code exception handling in core java file handling injava File I/O vs Memory-Mapped Filter first program in spring flex Garbage Collection Generics concept in java grails groovy and grails Guice Heap hibernate Hibernate Interview Questions how-to IBM DB2 IBM DB2 Tutorial ide immutable Interceptor Interface interview Interview Questions for Advanced JAVA investment bank j2ee java JAVA Code Examples Java 7 java changes java class loading JAVA Classes and Objects Java Classloader concept Java classloading concept java cloning concept java collection Java collection interview questions Java Collections java concurrency Java CountDownLatch java definiton Java design pattern Java EE 5 Java EE 6 Java Exceptions Java file Java Garbage Collection Java generics Java Glossary java hot concept java immutable concept Java Interface Java interview Question java interview question 2012 java interview question answer Java Interview Questions Java Interview Questions and Answers java interview topic java investment bank Java Job Questions java multithreading java multithreading concept java new features Java Packages java proxy object java questions Java Serialization Java serialization concept java serialization interview question java session concept java string Java Swings Questions java synchronization java threading Java Threads Questions java tutorial java util; java collections; java questions java volatile java volatile interview question Java Wrapper Classes java.java1.5 java.lang.ClassCastException JavaNotes javascript JAX-WS jdbc JDBC JDBC Database connection jdk 1.5 features JDK 1.5 new features Concurrent HashMap JMS interview question JMS tutorial job JSESSIONID concept JSESSIONID interview Question JSF jsp JSP Interview Question JSP taglib JSTL with JSP Junit Junit Concept Junit interview question.Best Practices to write JUnit test cases in Java JVM Linux - Unix tutorial Marker Interfaces MD5 encryption and decryption messaging MNC software java interview question musix NCR java interview question Networking Job Questions news Object Serialization Objects ojdbc14.jar OOP Oracle Oracle SQL Query for two timestamp difference orm own JavaScript function call in Apache ofbiz Packages Palm Apps patterns pdf persistence Portal Portlet Spring Integration Prime number test in java programs Rails Reboot remote computers REST Ruby Sample application schema SCJP security Senior java developer interviews servlet3 servlets session tracking singleton design pattern Spring Spring 2.5 Framework spring ebook Spring framework concept spring MVC spring pdf Spring Security Spring Security interview questions SQL SQL performance SQL Query to create xml file Sql Query tuning ssis and ssrs StAX and XML string concept string immutable string in java strings struts Struts2 Struts2 integration synchronization works in java Technical Interview testing tips Tomcat top Tutorial Volatile in deep Volatile working concept web Web Developer Job Questions web services weblogic Weblogic Application Server websphere what is JSESSIONID xml XML parsing in java XML with Java xslt


Tuesday, 9 July 2013

JBoss Cache – Hibernate Second level cache.


Hibernate is an open source O-R mapping tool which is being used extensively for domain modeling as well as bridge between the Object Oriented and Relational design paradigm. It is basically used to map the Object oriented domain entities to a corresponding relational container.
Hibernate’s architecture offer caching of data at 2 levels:
1.    Within a session aka. Transaction
2.    Outside the session.
Caching within the session is provided using the “Session” API in hibernate which maintains a transaction scoped cache of persistent data abstracted within itself.
Hibernate provides a plug and play architecture to integrate a second level cache to provide caching across transactions i.e. Session.
In this case study we will use a scenario to configure JBoss TreeCache as hibernate second level cache.
JBoss Cache has dependency on the following libraries which should be in the classpath of the runtime environment where the cache is being configured i.e. for JBoss 4.2 “..\jboss-4.2.3.GA\server\default\lib”.
1.      jbosscache-core.jar
2.      commons-logging.jar
3.      jboss-common-core.jar
4.      jgroups.jar
5.      Hibernate-Jbosscache2.jar

Jboss Cache is also dependent on the javax.transaction package which will be available either through the JEE jars in the application server classpath or through hibernate jars.

One instance of cache is identified in the configuration as “Cache Region”. The cache regions can be load balanced / clustered using the application server’s load balancing attributes. We can create more than one cache regions for a given application depending upon the characteristics of the data being cached. i.e. Domain entities, Files, demographics information etc. Advantage of segregating the data amongst different cache regions is that we can register different initialization as well as invalidation policies for each one of them.

Using Jboss Cache as a second level cache with hibernate, we can cache following entities:
1.      Entities
2.      Collections
3.      Query results
We need to be careful with query results while configuring a cluster as replication of query results across cluster is very costly. Hence we should have good reasons for configuring the query results across cluster.

Before proceeding forward we need to be aware of the dependencies between hibernate versions as well as JBoss cache version. For all hibernate versions above 3.3.x the recommended JBoss Cache versions to be used are 3.x. JBoss AS 5.x and above supports Cache version 3.x and hibernate version 3.3.x. In any other scenario proper dependencies will have to be verified and dependent jars needs to be copied to the AS classpath to make it work.

Best way to register JBoss cache in the Application Server is via mbeans. We need to provide an xml file to register JBoss cache within application server as mbean i.e. in the directory “..\jboss-4.2.3.GA\server\default\deploy” for JBoss AS 4.2.x . This will also provide a name to the cache service. Example xml is provided below:

<server>

   <mbean code="org.jboss.cache.jmx.CacheJmxWrapper"
          name="jboss.cache:service=TreeCache">

      <depends>jboss:service=Naming</depends>
      <depends>jboss:service=TransactionManager</depends>
.
.
</server>

As you can see a jmx wrapper is being used to register the cache with the application server. In this case it is identified with the name “jboss.cache:service=TreeCache”. The configuration file is also communicating to the application server about the dependencies of this service on the other services running within application server context. For example JBoss cache will be using the transaction manager which is running within the application server and which is accessible through the naming service.

After the Jboss Cache service is registered with the application server next step is to make hibernate aware of the running Jboss cache service which can be used as its second level cache.

Hibernate Session factory needs to be provided with certain options to enable the second level caching using  already started Jboss cache within Application server context.

Following parameters needs to be provided to Session factory:

hibernate.cache.use_second_level_cache
true
This option enables hibernate session factory to use second level caching mechanism
hibernate.cache.region.factory_class
org.hibernate.cache.jbc2.MultiplexedJBossCacheRegionFactory
This option initializes the Cache region factory to be used to handle caching in hibernate
hibernate.treecache.mbean.object_name
jboss.cache:service=TreeCache
This option is to connect to the TreeCache instance configured through mbean in application server.
hibernate.cache.use_query_cache
true
This option enables caching of query results
hibernate.cache.region.jbc2.query.localonly
true
This option stops the replication of query results across cache clusters i.e. query results are cached locally
Another and more object oriented version of Tree Cache has been release named as POJOCache. This cache sits atop Jboss Tree cache and is mainly responsible for the caching of plain old java objects. In this version POJO’s are inserted into the cache which in turn:
1.      Keeps track of the modifications on POJO’s
2.      Replicate the POJO’s across cache clusters
3.      Participate in transactions to persist data into DB.
4.      AOP can be used for configuration of POJO’s and cache.         

1.4.                  References

JBoss TreeCache Reference manual

JBoss Hibernate Configuration Guide.




Sample  TreeCache XML File configuration


<?xml version="1.0" encoding="UTF-8"?>
<server>
   <mbean code="org.jboss.cache.jmx.CacheJmxWrapper"  name="jboss.cache:service=TreeCache">
      <depends>jboss:service=Naming</depends>
      <depends>jboss:service=TransactionManager</depends>
      <!--
          Configure the TransactionManager
      -->
      <attribute name="TransactionManagerLookupClass">org.jboss.cache.transaction.GenericTransactionManagerLookup
      </attribute>
      <!--
          Isolation level : SERIALIZABLE
                            REPEATABLE_READ (default)
                            READ_COMMITTED
                            READ_UNCOMMITTED
                            NONE
      -->
      <attribute name="IsolationLevel">REPEATABLE_READ</attribute>
      <!--
           Valid modes are LOCAL
                           REPL_ASYNC
                           REPL_SYNC
                           INVALIDATION_ASYNC
                           INVALIDATION_SYNC
      -->
      <attribute name="CacheMode">REPL_SYNC</attribute>
      <!--
      Just used for async repl: use a replication queue
      -->
      <attribute name="UseReplQueue">false</attribute>
      <!--
          Replication interval for replication queue (in ms)
      -->
      <attribute name="ReplQueueInterval">0</attribute>
      <!--
          Max number of elements which trigger replication
      -->
      <attribute name="ReplQueueMaxElements">0</attribute>
      <!-- Name of cluster. Needs to be the same for all TreeCache nodes in a
           cluster in order to find each other.
      -->
      <attribute name="ClusterName">test-JBossCache-Cluster</attribute>

      <attribute name="ClusterConfig">
         <config>
            <UDP mcast_addr="228.10.10.10"
                 mcast_port="45588"
                 tos="8"
                 ucast_recv_buf_size="20000000"
                 ucast_send_buf_size="640000"
                 mcast_recv_buf_size="25000000"
                 mcast_send_buf_size="640000"
                 loopback="false"
                 discard_incompatible_packets="true"
                 max_bundle_size="64000"
                 max_bundle_timeout="30"
                 use_incoming_packet_handler="true"
                 ip_ttl="2"
                 enable_bundling="false"
                 enable_diagnostics="true"
                 use_concurrent_stack="true"
                 thread_naming_pattern="pl"
                 thread_pool.enabled="true"
                 thread_pool.min_threads="1"
                 thread_pool.max_threads="25"
                 thread_pool.keep_alive_time="30000"
                 thread_pool.queue_enabled="true"
                 thread_pool.queue_max_size="10"
                 thread_pool.rejection_policy="Run"
                 oob_thread_pool.enabled="true"
                 oob_thread_pool.min_threads="1"
                 oob_thread_pool.max_threads="4"
                 oob_thread_pool.keep_alive_time="10000"
                 oob_thread_pool.queue_enabled="true"
                 oob_thread_pool.queue_max_size="10"
                 oob_thread_pool.rejection_policy="Run"/>

            <PING timeout="2000" num_initial_members="3"/>
            <MERGE2 max_interval="30000" min_interval="10000"/>
            <FD_SOCK/>
            <FD timeout="10000" max_tries="5" shun="true"/>
            <VERIFY_SUSPECT timeout="1500"/>
            <pbcast.NAKACK use_mcast_xmit="false" gc_lag="0"
                           retransmit_timeout="300,600,1200,2400,4800"
                           discard_delivered_msgs="true"/>
            <UNICAST timeout="300,600,1200,2400,3600"/>
            <pbcast.STABLE stability_delay="1000" desired_avg_gossip="50000"
                           max_bytes="400000"/>
            <pbcast.GMS print_local_addr="true" join_timeout="5000"
                        join_retry_timeout="2000" shun="false"
                        view_bundling="true" view_ack_collection_timeout="5000"/>
            <FRAG2 frag_size="60000"/>
            <pbcast.STREAMING_STATE_TRANSFER use_reading_thread="true"/>
            <!-- <pbcast.STATE_TRANSFER/> -->
            <pbcast.FLUSH timeout="0"/>
         </config>
      </attribute>
      <!--
       Whether or not to fetch state on joining a cluster
       NOTE this used to be called FetchStateOnStartup and has been renamed to be more descriptive.
      -->
      <attribute name="FetchInMemoryState">true</attribute>
      <!--
          The max amount of time (in milliseconds) we wait until the
          state (ie. the contents of the cache) are retrieved from
          existing members in a clustered environment
      -->
      <attribute name="StateRetrievalTimeout">15000</attribute>
      <!--
          Number of milliseconds to wait until all responses for a
          synchronous call have been received.
      -->
      <attribute name="SyncReplTimeout">15000</attribute>
      <!-- Max number of milliseconds to wait for a lock acquisition -->
      <attribute name="LockAcquisitionTimeout">10000</attribute>
      <!--
         Indicate whether to use region based marshalling or not. Set this to true if you are running under a scoped
         class loader, e.g., inside an application server. Default is "false".
      -->
      <attribute name="UseRegionBasedMarshalling">true</attribute>
   </mbean>
</server>

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...