Unlike JBoss AS 6 there is no “$PROFILE/lib” present in JBoss AS 7 where we can place our JDBC Drivers in order to add/register the JDBC Driver. Also in JBoss AS 7 we don’t need to create a separate DataSource file as “*-ds.xml” file.

In JBoss AS 7 the DataSource configuration is placed wither inside the “Jboss-as-7.0.1.Final/standalone/configuration/standalone.xml” (if you are running a JBoss standalone profile), or the datasource information is placed inside the “boss-as-7.0.1.Final/domain/configuration/domain.xml” file.Here in this demonstration we will see how to install/register a JDBC Driver in JBoss AS7 and then how to create a DataSource from Admin-Console.

You can have a look at the below article for creating Oracle Datasource
How to create Oracle DataSource And Install Oracle JDBC Driver in JBoss AS7

You will find a Demo Web Application and the steps to create MySQL DataSource in the following repo:

https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/DataSourceDemo

WAR Download link: https://github.com/jaysensharma/MiddlewareMagicDemos/raw/master/DataSourceDemo/build/DataSourceDemo.war

In the attached WAR you will see the following:

1). DataSource Injection using @Resource
2). DataSource Referencing using in web.xml
3). DataSource Manual Old style lookup

There are two options to install/Register a JDBC Driver to JBoss AS 7.

  1. Installing a JDBC driver as a deployment
  2. Installing a JDBC driver as a module

Installing a JDBC driver as a deployment

First we need to check which Jdbc Driver are we using? Is it a JDBC 4-compliant driver or a Non-JDBC 4 -compliant driver? Any JDBC 4-compliant driver will automatically be recognized and installed into the system by name and version. A JDBC 4-compliant JAR is identified using the Java service provider mechanism. It contains a text a file named “META-INF/services/java.sql.Driver”, which contains the name of the class(es) of the Drivers which exist in that JAR.

If the driver is already JDBC4-comliant then you can directly deploy it inside the “jboss-as-7.0.1.Final/standalone/deployments” based on the mode which you are using to run your JBoss AS.

Dealing with Non JDBC 4 -compliant Drivers:

If your JDBC Driver is NOT JDBC 4-compliant then we can update the driver using “jar” utility by adding the “META-INF/services/java.sql.Driver” inside it. as following:

  1. Create a directory somewhere in your file system like “/home/userone/testDriver”
  2. Place your Non-JDBC 4 compliant driver in this directory “/home/userone/testDriver” suppose your Driver Jar name is “YourJdbcDriver.jar”
  3. Now create “META-INF/services” directory inside “/home/userone/testDriver”
  4. Create a file with name “java.sql.Driver” inside “/home/userone/testDriver/META-INF/services” directory and then add the fully qualified name of your JDBC Driver class in this file.
  5. Use the jar utility with -u (means update) option to add the META-INF directory with the above contents in it as following
jar  -uf  YourJdbcDriver.jar  META-INF/services/java.sql.Driver

Now you can take your JDBC Driver and then place it inside the “${JBOSS_AS7}/standalone/deployments”.
The advantage of deploying the JDBC Driver as a deployment in “domain” mode is that the deployments are automatically propagated to all servers to which the deployment applies, so the administrator need not to worry about the the JDBC Driver distribution every time.

Installing MySQL JDBC driver as a module

This is another option to install the JDBC Driver as a module. Which requires that we define a new module for our JDBC Driver inside “${JBOSS_AS7}/modules” directory as following:
To install the MySQL JDBC Driver (mysql-connector-java-x.x.xx-bin.jar) then we will need to do the following steps:

  1. Create a directory “MySQL/jdbc/main” inside the “jboss-as-7.0.1.Final/modules” directory.
  2. Paste your “mysql-connector-java-x.x.xx-bin.jar” MySQL Jdbc Driver inside “jboss-as-7.0.1.Final/modules/MySQL/jdbc/main” directory.
  3. Create a file “module.xml” inside “jboss-as-7.0.1.Final/modules/oracle/jdbc/main” as following:
  4. <?xml version="1.0" encoding="UTF-8"?>
    <module xmlns="urn:jboss:module:1.0" name="MySql.jdbc">
        <resources>
            <resource-root path="mysql-connector-java-x.x.xx-bin.jar"/>
        </resources>
        <dependencies>
            <module name="javax.api"/>
            <module name="javax.transaction.api"/>
        </dependencies>
    </module>
    
  5. Now open your “jboss-as-7.0.1.Final/standalone/configuration/standalone.xml” file or “jboss-as-7.0.1.Final/domain/configuration/domain.xml” file and then add the driver declaration tag referring to your module as following, by default you will see the driver declaration tag already contains the declaration for <driver name=”h2″ module=”com.h2database.h2″>:
  6.                 <drivers>
                        <driver name="MySqlJDBCDriver" module="MySql.jdbc" />   <!-- This line has been added -->
                        <driver name="h2" module="com.h2database.h2">
                            <xa-datasource-class>
                                org.h2.jdbcx.JdbcDataSource
                            </xa-datasource-class>
                        </driver>
                    </drivers>
    

    Here we declared the <driver name=”MySqlJDBCDriver” module=”MySql.jdbc”/>

  7. Create a DataSource in your JBoss AS 7 and then in the Driver section you can refer to this Module name “MySQL.jdbc”

Creating a DataSource from JBoss AS 7 Admin Console

  1. After installing the JDBC Driver as mentioned above start your JBoss AS 7 and then login to the admin-console from URL: “http://localhost:9990/console” (as admin/admin credentials)
  2. From left hand panel of the console click on “Connectors—>DataSources”
  3. Creating MySql DataSource in AS7

  4. In the right side panel you will see a button “Add” click on this button.
  5. In Wizard “Step 1/3: Datasource Attributes”
    Enter the DataSource Name as “MySqlDS”
    Enter JNDI Name as “java:/MySqlDSJNDI” or “java:jboss/MySqlDSJNDI” (NOTE the valid DataSource name should start with either java:/ or with java:jboss/ prefix)
  6. DataSource name & JNDI Name

  7. Now in the next Section “Step 2/3: JDBC Driver” you will see all the installed drivers details. in above case as we already registered oracle driver as a module “MySqlJDBCDriver” in previous section so we will be able to see the details as following:
  8. Registered Jdbc Drivers List

  9. In next section provide the Database url (jdbc:mysql://localhost:3306/jboss_test) ,username and password
  10. Specifying DataSource properties

  11. Now you just have to enable the newly created datasource and if everything is configured properly you would see it green as shown in below snap-shot:
  12. DataSource Details

    10:50:32,330 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-7) JBAS010400: Bound data source [java:/MySqlDS]
    

    For More Details visit:
    https://docs.jboss.org/author/display/AS7/Developer+Guide
    http://community.jboss.org/wiki/DataSourceConfigurationinAS7#Installing_a_JDBC_driver_as_a_deployment

    NOTE: JBoss AS7 is a Community release and we always recommend you to use the RedHat Supported JBoss Enterprise Application Platform, There are various reasons behind this. Please refer to the Video: http://www.youtube.com/watch?v=iNFV0r9v14g&feature=player_detailpage

    Thanks
    MiddlewareMagic Team