Home > Articles > Programming > Java

Using Multiple Databases with JBoss

  • Print
  • + Share This
JBoss is an extremely powerful J2EE application server, but with that power comes complexity. Marcus Zarra breaks down how databases, and therefore datasources, are configured in JBoss.
Like this article? We recommend

Like this article? We recommend

Configuring a Datasource

The default installation of JBoss comes with a total of three configurations: all, default, and minimal. When you first start the JBoss server without any parameters, it naturally runs the default configuration. To run another configuration, the startup script must be executed with the -c parameter as follows:

${JBOSS.ROOT}/bin/run.sh –c minimal

Starting JBoss in this manner instructs it to use the minimal server configuration instead of the default. This technique is extremely useful when you're attempting to alter the configuration of JBoss itself. You can make your changes in a different configuration than the default, and roll back to the default if necessary. Before messing around with datasources, though, make a copy of the default configuration, so you can work safely with the copy. (For this article, I'll call my copy example, giving me four configurations in the server directory: all, default, minimal, and example.)

The first step in changing the default datasource in JBoss is to configure a new one. All database configurations in JBoss are defined in the deploy directory under the server directory where you're working (in this case, server/example/deploy). The default database used by JBoss is the pure Java database Hypersonic. (For more information on this excellent open-source database, consult this SourceForge page.) The configuration for this database is located in the file hsqldb-ds.xml. This XML configuration file describes how to connect to the Hypersonic database. With all the comments removed from the file, we have the following configuration information:

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
 <local-tx-datasource>
  <jndi-name>DefaultDS</jndi-name>
   <connection-url>
    jdbc:hsqldb:${jboss.server.data.dir}${/}hypersonic${/}localDB
   </connection-url>
   <driver-class>org.hsqldb.jdbcDriver</driver-class>
   <user-name>sa</user-name>
   <password></password>
   <min-pool-size>5</min-pool-size>
   <max-pool-size>20</max-pool-size>
   <idle-timeout-minutes>0</idle-timeout-minutes>
   <track-statements/>
   <security-domain>HsqlDbRealm</security-domain>
   <depends>jboss:service=Hypersonic,database=localDB</depends>
  </local-tx-datasource>
  <mbean code="org.jboss.jdbc.HypersonicDatabase"
   name="jboss:service=Hypersonic,database=localDB">
   <attribute name="Database">localDB</attribute>
   <attribute name="InProcessMode">true</attribute>
  </mbean>
</datasources>

Most of these parameters should be familiar to anyone who has worked with JDBC. The first section under the root is the <local-tx-datasource>. This is where you define the database and how to connect to it: driver, URL, username, and password. You also define how many connections should be in the connection pool.

This section contains two interesting parameters:

  • <security-domain> allows you to configure the username and password for the database in the login-config.xml file instead of here in the datasource configuration.

  • <depends> specifies that this service will not start until the service on which it depends has started. This parameter allows you to write services that a database may need to run properly. The <depends> tag also instructs JBoss on the order in which items need to be shut down. The tag in this example states that the mbean must be started before the database and shut down after the database.

Once the datasource is defined, other portions of the server can utilize it. Any other service or configuration that wants to use this datasource needs to reference it by its JNDI name. In this case, the JNDI name is DefaultDS.

  • + Share This
  • 🔖 Save To Your Account