1.4.2.1. Configure the distributed cache

1.4.2.1. Configure the distributed cache

To avoid round trips to the database, you can use a cache for your entities. JBoss EJB 3.0 is implemented by Hibernate, which has support for a second-level cache. The Hibernate setup used for the JBoss EJB 3.0 implementation uses JBoss Cache as its underlying cache implementation. The cache provides the following functionalities.

JBoss Cache service for EJB 3.0 entity beans is configured in a TreeCache MBean (see Section 2.2, “JBossCache Configuration”) in the deploy/ejb3-entity-cache-service.xml file. The name of the cache MBean service is jboss.cache:service=EJB3EntityTreeCache. Below is the contents of the ejb3-entity-cache-service.xml file in the standard JBoss distribution. Again, we omitted the JGroups configuration element ClusterConfig.

<server>
    <mbean code="org.jboss.cache.TreeCache" 
            name="jboss.cache:service=EJB3EntityTreeCache">
        
        <depends>jboss:service=Naming</depends>
        <depends>jboss:service=TransactionManager</depends>

        <!-- Configure the TransactionManager -->
        <attribute name="TransactionManagerLookupClass">
            org.jboss.cache.JBossTransactionManagerLookup
        </attribute>

        <attribute name="IsolationLevel">REPEATABLE_READ</attribute>
        <attribute name="CacheMode">REPL_SYNC</attribute>

        <!--Name of cluster. Needs to be the same for all clusters, 
            in order to find each other -->
        <attribute name="ClusterName">EJB3-entity-cache</attribute>

        <attribute name="ClusterConfig">
            ... ...
        </attribute>

        <attribute name="InitialStateRetrievalTimeout">5000</attribute>
        <attribute name="SyncReplTimeout">10000</attribute>
        <attribute name="LockAcquisitionTimeout">15000</attribute>

        <attribute name="EvictionPolicyClass">
            org.jboss.cache.eviction.LRUPolicy
        </attribute>

        <!--  Specific eviction policy configurations. This is LRU -->
        <attribute name="EvictionPolicyConfig">
            <config>
                <attribute name="wakeUpIntervalSeconds">5</attribute>
                <!--  Cache wide default -->
                <region name="/_default_">
                    <attribute name="maxNodes">5000</attribute>
                    <attribute name="timeToLiveSeconds">1000</attribute>
                </region>
            </config>
        </attribute>
    </mbean>
</server>
                

As we discussed in Section 2.2, “JBossCache Configuration”, JBoss Cache allows you to specify timeouts to cached entities. Entities not accessed within a certain amount of time are dropped from the cache in order to save memory. If running within a cluster, and the cache is updated, changes to the entries in one node will be replicated to the corresponding entries in the other nodes in the cluster.

Now, we have JBoss Cache configured to support distributed caching of EJB 3.0 entity beans. We still have to configure individual entity beans to use the cache service.