JBoss AS 7 deployable datasources
- Published: 13 April 2012
JBoss AS 7.1.1 has been released and one of its appealing news is the "resurrection" of the earlier -ds.xml datasources which can now be deployed as a standalone resource or even as part of an application.
We have to admit, we were missing it. Although configuring a datasource with AS7 just requires a few minutes work, deployable datasources are a precious quickstart resource (especially for testing purposes).
So how do we get a datasource deployed?
Step 1: At first you need an available driver
The simplest way to provide a driver is copying it into the deployments folder of your application server. Supposing we are going to install a MySQL Driver:
cp mysql-connector-java-5.1.18-bin.jar /usr/jboss-as-7.1.1.Final/standalone/deployments
Now check that the driver has been successfully deployed on the server logs:
09:32:10,400 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559:
Step 2: Deploy the datasource file
Just fill in an xml file which ends (as we used to do) in -ds.xml, for example this is a mysql-ds.xml which is suitable for MySQL database:
<datasources xmlns="http://www.jboss.org/ironjacamar/schema"> <datasource jndi-name="java:jboss/datasources/MySqlDB" pool-name="MySQLPool"> <connection-url>jdbc:mysql://localhost:3306/as7db</connection-url> <driver>mysql-connector-java-5.1.18-bin.jar</driver> <pool> <max-pool-size>30</max-pool-size> </pool> <security> <user-name>root</user-name> <password>admin</password> </security> </datasource> </datasources>
JBoss AS should log your successful datasource deployment:
09:37:45,949 INFO [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015876: Starting deployment of "mysql-ds.xml"
09:37:46,001 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-8) JBAS010400: Bound data source [jboss/datasources/MySqlDB]
09:37:46,042 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "mysql-ds.xml"
Drawbacks?At this point you might wonder if there is any pitfall when using the older -ds.xml approach ? well actually if you "bypass" the management interface and deploy the datasource in the old way, you will not be able to manage it through the CLI or Web admin interface. As you can see from this snapshot, our datasource is not enlisted through the manageable resources of the Web interface.
So you should consider using deployable datasource for development/testing purpose and rather switch to the standard AS7 approach in a production environment.
Deploying the datasource along with your applicationYou can actually deploy your datasource along with your application, just you used to do in the past.
Here's a sample Web application Structure which ships with a datasource (in the WEB-INF folder). For other a JAR archive you would rather need copying the datasource in the META-INF folder: