Using Hibernate Second level cache in JBoss 5.1

Filed under: J2EE, Java, — Tags: Hibernate, JBoss, JPA, Second Level Cache — Thomas Sundberg — 2012-05-10

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.

Resources



(less...)

Pages

About
Events
Why

Categories

Agile
Automation
BDD
Clean code
Continuous delivery
Continuous deployment
Continuous integration
Cucumber
Culture
Design
DevOps
Executable specification
Git
Gradle
Guice
J2EE
JUnit
Java
Javascript
Kubernetes
Linux
Load testing
Maven
Mockito
New developers
Pair programming
PicoContainer
Presentation
Programming
Public speaking
Quality
React
Recruiting
Requirements
Scala
Selenium
Software craftsmanship
Software development
Spring
TDD
Teaching
Technical debt
Test automation
Tools
Web
Windows
eXtreme Programming

Authors

Thomas Sundberg
Adrian Bolboaca

Archives

Meta

rss RSS