Configuring a data source on JBoss 7

This post will describe the steps necessary to configure a JDBC DataSource in JBoss 7. MySQL database and JBoss 7.1.0-Final will be used as example.

Configuring MySQL JDBC driver module

The first big change on JBoss 7 compared to the previous versions is that the libraries to be deployed are no longer considered “libs”, in the sense that all the required work was to copy the .jar files to a specific folder and start using the services provided by that .jar. Any lib is now considered a JBoss module, like the server’s internal services, such as messaging. The following steps are required to configure the MySQL JDBC driver:

  • Download the MySQL JDBC driver from http://dev.mysql.com/downloads/connector/j/)
  • Create the folder com/mysql/main inside JBOSS_HOME/modules
  • Copy the JDBC driver to the folder created above
  • Create a file named module.xml inside JBOSS_HOME/modules/com/mysql/main/, with the following content (considering that mysql-connector-java-5.1.17-bin.jar is the driver used):
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="com.mysql">
	<resources>
		<resource-root path="mysql-connector-java-5.1.17-bin.jar"/>
	</resources>
	<dependencies>
		<module name="javax.api"/>
	</dependencies>
</module>

This file is responsible for registering the module within JBoss, describing the module location (com.mysql), the module file name and its dependencies.

There is still other ways to configure the driver, but this post will only discuss this option.

Having done that, the module is already available and the DataSource can be configured.

DataSource configuration

This JBoss version no longer has the old structure with configuration files widespread across the directories. Instead, there is only one file with the required configurations:
JBOSS_HOME/standalone/configuration/standalone.xml (by default). All the services, such as JMS, Logging, DataSources, and so on, are configured within this file.

In order to configure the DataSource, open the standalone.xml file, scroll down until the datasource subsystem section (<subsystem xmlns=”urn:jboss:domain:datasources:1.0″>) and add a new <datasource> inside <datasources>:

<datasource jta="true" jndi-name="java:/test-ds" pool-name="test"
	enabled="true" use-java-context="true" use-ccm="true">
	<connection-url>jdbc:mysql://localhost:3306/<databasename></connection-url>
	<driver>com.mysql</driver>
	<transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
	<pool>
		<min-pool-size>5</min-pool-size>
		<max-pool-size>30</max-pool-size>
		<prefill>true</prefill>
		<use-strict-min>false</use-strict-min>
		<flush-strategy>FailingConnectionOnly</flush-strategy>
	</pool>
	<security>
		<user-name><user></user-name>
		<password><password></password>
	</security>
	<statement>
		<prepared-statement-cache-size>32</prepared-statement-cache-size>
	</statement>
</datasource>

The <datasource>’s configuration attributes are pretty much the same of the previous versions, and more information regarding that can be found here.

Right below that, in the <drivers> section, it is necessary to add the MySQL driver (previously configured);

<driver name="com.mysql" module="com.mysql">
	<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>

At this point, the DataSource is ready to be utilized by other services and applications.

Just out-of curiosity, it is also possible to configure a DataSource using the old approach (creating a file with suffix *-ds.xml inside deployments directory) or programmatically, through the @DataSourceDefinition annotation.

That’s it, now the DataSource can be normally used by your applications.

21 thoughts on “Configuring a data source on JBoss 7

  1. Muito bom o post tive uma boa ideia de como fazer, mas estou quebrando a cabeça para criar o datasource para firebird… se poderem ajudar agradeço..

    Like

  2. Bah, Perfeito este post, amigo!
    Ficou realmente muito claro e fácil de configurar.
    Consegui fazer aqui em precisar fazer nenhum outro tipo de ajuste fora do que foi mencionado.
    Parabéns!!

    Like

  3. Oi cara, bom dia.
    No arquivo *-ds.xml o que deve estar nos campos “jndi-name” e “pool-name”? Estou usando um arquivo gerado pelo seam-gen e tendo erros de missing dependencies por causa do datasource.

    Like

Leave a comment