Configuring a data source with MySQL database

The default data source in WildFly 8.1 is configured with the H2 database engine. There are several options available for a database. The top four most commonly used relational databases are Oracle database, MySQL database, SQL Server, and PostgreSQL Server. Oracle database and SQL Server are designed for enterprise level applications and are not open source. Oracle database offers more features to facilitate system and data maintenance. It also offers features to prevent system and data failure as compared to SQL Server. MySQL and PostgreSQL are open source databases with comparable features and designed primarily for small scale applications. We will use MySQL database. Some of the reasons to choose MySQL are discussed at http://www.mysql.com/why-mysql/topreasons.html.

We will configure a datasource with the MySQL database for use in the EJB 3.x application for object/relational mapping. Use the following steps to configure a datasource:

  1. First, we need to create a module for MySQL database. For the MySQL module, create a module.xml file in the %JBOSS_HOME%/modules/mysql/main directory; the mysql/main subdirectory is also to be created. The module.xml file is listed in the following code snippet:
    <module xmlns="urn:jboss:module:1.1" name="mysql" slot="main">
      <resources>  
        <resource-root path="mysql-connector-java-5.1.33-bin.jar"/>
      </resources>
      <dependencies>
        <module name="javax.api"/>
      </dependencies>
    </module>
  2. Copy the mysql-connector-java-5.1.33-bin.jar (MySQL JDBC JAR) file from C:Program Files (x86)MySQLConnector.J 5.1 to the %JBOSS_HOME%/modules/mysql/main directory. The MySQL mysql-connector-java JAR file version specified in module.xml must be the same as the version of the JAR file copied to the /modules/mysql/main directory.
  3. Add a <datasource/> definition for the MySQL database to the <datasources/> element and a <driver/> definition to the <drivers/> element in the %JBOSS_HOME%/standalone/configuration/standalone.xml file within the <subsystem xmlns="urn:jboss:domain:datasources:2.0"> </subsystem> element. The <password/> tag in the <datasource/> configuration tag is the password configured when the MySQL database is installed. The datasource class for the MySQL driver is a XA datasource, which is used for distributed transactions:
    <subsystem xmlns="urn:jboss:domain:datasources:2.0">
      <datasources>
        <datasource jndi-name="java:jboss/datasources/MySQLDS" pool-name="MySQLDS" enabled="true" use-java-context="true">
          <connection-url>jdbc:mysql://localhost:3306/test</connection-url>
          <driver>mysql</driver>
          <pool>
            <min-pool-size>10</min-pool-size>
            <max-pool-size>20</max-pool-size>
            <prefill>true</prefill>
          </pool>
          <security>
            <user-name>root</user-name>
            <password>mysql</password>
          </security>
        </datasource>
        <drivers>
          <driver name="mysql" module="mysql">
            <driver-class>com.mysql.jdbc.Driver</driver-class>
            <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
          </driver>
        </drivers>
      </datasources>
    </subsystem>
  4. If the server is running after modifying the standalone.xml configuration file, restart WildFly 8.x server. The MySQL datasource gets deployed. To start or restart the WildFly server, double-click on the C:wildfly-8.1.0.Finalinstandalone batch file.
  5. Log in to the WildFly 8 Administration Console with the URL: http://localhost:8080. Click on Administration Console, as shown in the following screenshot:
    Configuring a data source with MySQL database
  6. In the login dialog box, specify the username and password for the user added with the add-user.bat script.
  7. Select the Runtime tab in Administration Console. The MySQL datasource is listed as deployed in Datasources Subsystems, as shown in the following screenshot. Click on Test Connection to test the connection:
    Configuring a data source with MySQL database
  8. If a connection with the MySQL database is established, a Successfully created JDBC connection message will get displayed:
    Configuring a data source with MySQL database

In the next section, we will create entities for the EJB 3.x application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset