JBossCacheHibernateTransactionsStandaloneExample

NOTE: In spite of the example working, transaction management is not working appropriately when it comes to both Hibernate and JBossCache participating of the same transaction. The reason is because at the time of writing this wiki, Hibernate did not allow to inject a JTA Transaction Manager which is not bound to JNDI. In the example, Hibernate creates a JDBC transaction and JBossCache a JTA transaction which are not linked together. To be able to make a Hibernate and JBossCache participant of the same JTA transaction in a standalone environment, customer code needs to be added to Hibernate to get around the JNDI/JTA coupling.

 

Attached to this wiki, you can find a standalone application which uses Hibernate, JBoss Cache as second level cache and JBoss Transaction as the transaction manager. For the example, the latest releases (Hibernate 3.2.2, JBoss Cache 1.4.1.SP2 and JBoss Transactions 4.2.2) where used as we're continuously working on the integration of these projects. A

 

For the shake of making easier to illustrate the example, you need to run this example with Java 5 so that you inspect the cache using JConsole. Further details bellow.

 

To build up this example, which is based in the Hibernate tutorial, the JBoss Cache and Hibernate wiki and the Improving Perfomance chapter from the Hibernate documentation have been used to provide the explanation to the configuration settings chosen.

 

Here're the instructions:

 

  1. execute: build.bat/sh (compiles the source code)

  2. execute clean-start-hs.bat/sh (creates a new Hypersonic DB instance, clearing data from any previous instances)

  3. in a console instance, execute: run.bat/sh -Dbind.address=127.0.0.1 -Dhibernate.hbm2ddl.auto=create (hibernate sys property set so that db schema's created. just do it first time you star the db, afterwards not needed till you restart db)

  4. in another console instance, execute: run.bat -Dbind.address=127.0.0.1

  5. hit 0 to store an Event in the db in any of the instances

  6. hit 1 in the same console to list the events in the database. You should see from the messages in the console how this event is put in the second level cache

  7. open two JConsole instances, one for each running instance. See UseJDK5JConsole

  8. go to the MBeans section and find the jboss.cache:service=TreeCache-Cluster

  9. click on printDetails() operation in both jconsole instances and see how they contain the same information, which refers to the instance that has been loaded into the second level cache which has also been replicated to the second Hibernate/JBossCache instance. You should also be able to see the query cache populated too.

 

You can continue the example to see how collection cache works:

 

  1. hit 2 so that an event is created with a person created inside the participants collection help by Event.

  2. hit 3 and you'll be able to query all the events and their participants.

  3. now check the cache instances via JConsole. See UseJDK5JConsole

 

Note:

 

When stopping a running instance of the example, the following WARN message is thrown:

 

15:43:31,718  WARN TreeCache:195 - Error in stop jboss.cache:service=TreeCache-Cluster

 

javax.management.InstanceNotFoundException: jboss.system:service=ServiceController

 

        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1010)

 

...

 

This warning and the stacktrace it logs can be ignore. We're working to fix the issue