Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 23 Next »

Integrating BTM with Hibernate

Hibernate can be integrated straight with any JTA transaction manager.

The biggest added value (omitting the fact that you can use Hibernate and two databases) is Hibernate's Current Session context management with JTA. You do not have to take care about opening nor closing Session as Hibernate will automatically bind them to the JTA transaction's lifecycle. You just have to make sure JTA transactions are properly started and ended.


JTA datasources

Hibernate cannot directly create a BTM PoolingDataSource. You will have to create them yourself (either via the API or the Resource Loader) then bind them to a JNDI server.

API way: Creating the datasources

As you can expect, you will need to create one PoolingDataSource per database. Say that you want to use two Embedded Derby databases, and configure them via the BTM API. Here is what your code would look like:

Datasource's unique name and JNDI location correspondence


It is usually a good idea to set a datasource's unique name to the same value as its JNDI location although this is not mandatory.

API way: Binding the datasources to JNDI

You have to bind the datasources to some JNDI server. You can use any one you wish, including the one embedded in your application server.

In the case you want to run Hibernate in a standalone application, you can use Tomcat's JNDI server as it is very easy to use it in a standalone J2SE application.

Just create a file at the root of your classpath. It should only contain this line:

Then add these two jar files to your classpath:

They are available in any Tomcat 5.5 distribution, just look into the TOMCAT_HOME/common/lib folder.

This will configure Tomcat's JNDI server as the default implementation. It is a simple intra-VM server that implements the most important JNDI features. You can now just create a InitialContext with the no-args constructor to have access to it.

Finally, here is the code to bind the datasources:

Resource Loader way: Creating the datasources

You can use BTM's Resource Loader instead of the BTM API. It is usually a good idea when you want to create a fully standalone application as you can get rid of the datasources creation, JNDI binding and shutdown code.

Create a file in the current directory containing these properties:

Resource Loader JNDI binding


When you configure the Resource Loader to bind datasources to JNDI (by setting thee property to true) it will bind them using the unique name as the JNDI location.

In your application code, you will have to configure BTM to use the resource loader:

This has the exact same behavior as creating the PoolingDataSource objects and binding them to JNDI yourself. It is just more convenient.

Hibernate Session factories

You need to configure exactly one SessionFactory per datasource.

Datasource JNDI location

You have to tell Hibernate where to get the BTM datasource via JNDI. Add a connection.datasource property and set its value to the JNDI location of your datasource:

Current session context

You have to set current_session_context_class to jta.

Transaction factory class

You have to set transaction.factory_class to org.hibernate.transaction.JTATransactionFactory.

Transaction manager lookup class

You have to set transaction.manager_lookup_class to an implementation of TransactionManagerLookup that you have to create yourself. Here is an example implementation if this interface:

Then add this property:


Here is what the hibernate_testDS1.cfg.xml file will look like for the first datasource. Some other mandatory properties also have to be added, like the dialect, cache.provider_class and of course the required object mappings.

And here is the hibernate_testDS2.cfg.xml for the second datasource:

  • No labels