Thomas Sundberg

May 10, 2012

Using Hibernate Second level cache in JBoss 5.1

This post has been migrated to

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=""
    <persistence-unit name="my-persistence-unit"
            <property name="hibernate.dialect"
            <property name=""
            <property name="hibernate.jdbc.batch_size"
            <property name="hibernate.jdbc.batch_versioned_data"
            <property name="jboss.depends.1"

            <property name="hibernate.cache.use_second_level_cache"
            <property name="hibernate.cache.use_query_cache"
            <property name="hibernate.cache.region.factory_class"
            <property name="hibernate.cache.region.jbc2.configs"

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🙂


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: Logo

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

Create a free website or blog at

%d bloggers like this: