Page 68: The Datasource creation using the Admin Console was missing the JDBC Driver installation step. That can be done via the CLI as follows:
In book copies delivered after the 12th March 2014 the section named “Configuring a Datasource using the Admin Console” has been replaced by the following one in order to avoid using multiple management instruments at the same time:
Manual Datasource configuration
Actually this is not the quickest way to configure a Datasource but we decided to include it as first option as it is a good learning exercise that includes manual module creation and understanding of where changes are applied in the server configuration. It can be broken down in the following steps:
- JDBC Module creation
- Datasource and Driver definition in the server configuration file
Let’s see each step in detail:
JDBC Module creation
As we have learnt, the application server modules are installed in the application server’s modules folder. In order to install the JDBC Driver as module you need to perform the following steps:
- Create a file path structure under the JBOSS_HOME/modules directory. For example, in order to install a MySQL JDBC driver, create a directory structure as follows: JBOSS_HOME/modules/com/mysql/main.
- Copy the JDBC driver JAR into the main subdirectory.
- In the main subdirectory, create a module.xml file containing the following definitions (just adapt the JDBC driver name to your case):
<?xml version="1.0" encoding="UTF-8"?> <module xmlns="urn:jboss:module:1.0" name="com.mysql"> <resources> <resource-root path="mysql-connector-java-5.1.24-bin.jar"/> </resources> <dependencies> <module name="javax.api"/> <module name="javax.transaction.api"/> </dependencies> </module>
Done with the JDBC module installation, check that you have the following filesystem structure under your modules tree:
Now restart the application server and move
to the next section, where we will be configuring the data source into the application server.
Configuring the Datasource and JDBC Driver
As it is, we have just a module named “com.mysql”installed on our application server which barely loads a library named mysql-connector-java-5.1.24.jar. In order to configure a Datasource we will add a definition of the Datasource and of the JDBC driver into your server configuration file.
Therefore, open the standalone.xml file (or whatever configuration you want to install it to) and include the following elements:
<datasources> . . . . <datasource jndi-name="java:/jboss/datasources/MySQLDS" pool-name="MySQLPool" enabled="true"> <connection-url>jdbc:mysql://localhost:3306/mysqlschema</connection-url> <driver>mysql</driver> <security> <user-name>jboss</user-name> <password>jboss</password> </security> </datasource> <drivers> . . . . <driver name="mysql" module="com.mysql"/> </drivers> </datasources>
As you can see we have nested into the datasources element a definition for the Datasource that is bound under the JNDI name “java:/jboss/datasources/MySQLDS”. Next, we have included the connection-url which points to a MySQL server on localhost running a “mysqlschema”. Next element is the driver name which references the “mysql” driver defined into the “drivers” section. Last mandatory element that we have included is the user-name and password information that is nested within the security section.
Reload your server configuration or restart the server in order to apply the changes.