JBoss AS 7 deployable datasources

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

 

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: 
Deployed "mysql-connector-java-5.1.18-bin.jar"

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.

jboss as 7 as7 datasource deployment

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 application

You 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:
Sample.war
¦   home.xhtml
¦
+---WEB-INF
    ¦   faces-config.xml
    ¦   web.xml
    ¦   mysql-ds.xml
    ¦
    +---classes
    ¦   ¦
    ¦   +---META-INF
    ¦           MANIFEST.MF
    ¦           persistence.xml
    ¦
    +---lib
            mysql-connector-java-5.1.18-bin.jar

Advertisement

Related articles available on mastertheboss.com

How to deploy a DataSource in jboss at application level ?

  Do you need to deploy your DataSource along with your Enterpri

JBoss Datasource HA

This tutorial has been updated for the new release of JBoss Appli

JBoss run out of Connections ?

Have you got No ManagedConnections available error message ? well

How to connect to a DataSource from a remote client?

  If you want to connect to your JBoss Connection Pool from a re

JBoss Datasource configuration

A Datasource is a Java Naming and Directory Interface (JNDI) obje

How do I get the list of Datasources available ?

JBOSS AS 4/5 Users You can use either twiddle: $ twiddle.sh que

JBoss CheatSheet for Admins