Friday, 30 August 2013

JBoss AS 7.1.1.Final Administration. MySQL DataSource creation

Jboss AS 7.1.1 MySQL configuration

This article tries to explain how to create a new DataSource managed by the JEE-container.

The following steps must be followed in order to make available the MySql database system to persist data from beans deployed in the EBJ-Container.

Several steps are needed:

1) Download the MySql jdbc driver. In this case , the version that has been taken into account is the 5.1.26.

2) Add the new DataSource modifying the standalone.xml configuration file located in JBOSS_HOME/standalone/configuration. For doing so, add this elemental datasource element

<datasource jndi-name="java:/PruebaDS" pool-name="PruebaDS">

    <connection-url>jdbc:mysql://localhost:3306/greetingdb</connection-url>

            <driver>mysql</driver>

                    <security>

                        <user-name>root</user-name>

             </security>

 </datasource>

 Elemental explanations : the datasource is accessed by container-managed beans or external clients via JNDI. His name is “java:/PruebaDS”.

<connection-url> : It is the connection string needed if you want mysql to be your persistence database system. In this case, my database is called “greetingdb”.

<driver>: it refers to the .jar file that is installed as a JBOSS module (see later).

<security>: It specify the user of the database.

As well, the driver reference must be reflected in the previous file. 

<driver name="mysql" module="com.mysql"/>

The whole section of the file defining this new datasource is the following:

<subsystem xmlns="urn:jboss:domain:datasources:1.0">

            <datasources>

                <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">

                    <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>

                    <driver>h2</driver>

                    <security>

                        <user-name>sa</user-name>

                        <password>sa</password>

                    </security>

                </datasource>

                <datasource jndi-name="java:/PruebaDS" pool-name="PruebaDS">

                    <connection-url>jdbc:mysql://localhost:3306/greetingdb</connection-url>

                    <driver>mysql</driver>

                    <security>

                        <user-name>root</user-name>

                    </security>

                </datasource>

                <drivers>

                    <driver name="h2" module="com.h2database.h2">

                        <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>

                    </driver>

                    <driver name="mysql" module="com.mysql"/>

                </drivers>

            </datasources>

        </subsystem>

 

You can see two datasource defined with their respective drivers.

3) Now, it is the moment to install the new module in the JBOSS Application Server. It is quite simple. The steps are the following:

·         In the JBOSS_HOME/modules create the file structure /com/mysql/main (JBOSS_HOME/modules/com/mysql/main/

·         In this directory, copy the jar previously downloaded.

·         Create a file called module.xml and insert the following content:

<module xmlns="urn:jboss:module:1.0" name="com.mysql">

   <resources>

     <resource-root path="mysql-connector-java-5.1.26-bin.jar"/>

   </resources>

   <dependencies>

      <module name="javax.api"/>

      <module name="javax.transaction.api"/>

    </dependencies>

</module>

This file refers the jar file and the dependencies needed to make it available.

4) Start the JBOSS server and check if the datasource is well deployed:

 

13:44:20,984 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-4) JBAS010400: Bound data source [java:/PruebaDS]

Some like this you might find in the logging trace of the server once it is run.

 

 

 

No comments:

Post a Comment