Thomas Sundberg

May 10, 2012

Using Hibernate Second level cache in JBoss 5.1

Enabling second level cache in a JBoss when you use JPA 1 is actually relative easy. Understanding how to set its properties is also not to difficult to do. Finding how to specify which configuration file to use when setting the additional configurations is more difficult.

When you search the net, you will eventually find that there is a property called hibernate.cache.jbc.configs that should be defined in your persistence.xml and that should point to a file with the configurations. Chasing down how to do this for a few hours eventually drove to me start browsing the code. I realised then that the property actually was called hibernate.cache.region.jbc2.configs.

The default value for hibernate.cache.region.jbc2.configs is org/hibernate/cache/jbc2/builder/jbc2-configs.xml, a file found in the hibernate-jbosscache2.jar. The way to override this default setting is to set the value to either a resource in JBoss class path or a file on the file system. Pointing to a hardcoded file seems to be a bit risky since you actually can’t be sure where your final application will be deployed. A better solution is to make sure that the configuration is located in the JBoss class path and set the property to the name of the class path entry.

How do you add something to the class path in a JBoss 5.1? One easy way is to add a file to you server’s conf directory. This is typical something similar to server/default/conf if you use the default server in your JBoss.

A sample persistence.xml may look like this:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
             version="1.0">
    <persistence-unit name="my-persistence-unit"
                      transaction-type="JTA">
        <jta-data-source>java:/Space</jta-data-source>
        <properties>
            <property name="hibernate.dialect"
                      value="org.hibernate.dialect.HSQLDialect"/>
            <property name="hibernate.hbm2ddl.auto"
                      value="create-drop"/>
            <property name="hibernate.jdbc.batch_size"
                      value="50"/>
            <property name="hibernate.jdbc.batch_versioned_data"
                      value="true"/>
            <property name="jboss.depends.1"
                      value="jboss.system:service=ThreadPool"/>

            <property name="hibernate.cache.use_second_level_cache"
                      value="true"/>
            <property name="hibernate.cache.use_query_cache"
                      value="true"/>
            <property name="hibernate.cache.region.factory_class"
                      value="org.hibernate.cache.jbc2.MultiplexedJBossCacheRegionFactory"/>
            <property name="hibernate.cache.region.jbc2.configs"
                      value="jbc2-configs.xml"/>
        </properties>
    </persistence-unit>
</persistence>

Next step is to optimize the jbc2-configs.xml settings. This is dependant on your application and its typical use and therefore left as an exercise to the reader to deal with :)

Resources

About these ads

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Customized Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 39 other followers

%d bloggers like this: