Saturday, December 26, 2015

Creating a MySQL datasource for JBoss 7.1

In this tutorial we’ll create a MySQL datasource for JBoss Application Server 7.1. I recommend you to read the article listed in the “Useful article” section, because a MySQL driver is required for our datasource.
There are two ways for setting up a datasource. The first one is to set in the applications server’s standalone.xml, and the second is to set it externally, in a separated file. We’ll discuss both of them.

Datasource in standalone.xml

<xa-datasource jndi-name="java:jboss/datasources/HDBSample" pool-name="HDBSample">
          <driver>mysql</driver>
          <xa-datasource-property name="URL">jdbc:mysql://localhost:3306/hdbsample</xa-datasource-property>
          <xa-pool>
               <min-pool-size>10</min-pool-size>
               <max-pool-size>20</max-pool-size>
               <prefill>true</prefill>
          </xa-pool>
          <security>
               <user-name>root</user-name>
               <password></password>
          </security>
       </xa-datasource>

Save it and start the appserver. If we did it well, the following line will appear in the log:

13:26:02,898 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-7) JBAS010400: Bound data source [java:jboss/datasources/HDBSample]

External datasource

This type of datasource is much better than managing them in the standalone xml. You can add and remove datasources during runtime, and if you handle more than one, they can be in separated *-ds.xml files. And(!) they are portable: you don’t have to copy texts, just copy the complete file from the source appserver deployment directory into the other one.

Open a text editor, and copy the next few lines and save it with the name like “hdbsample-ds.xml”:

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
 <xa-datasource jndi-name="java:jboss/datasources/HDBSample" pool-name="HDBSample">
          <driver>mysql</driver>
          <xa-datasource-property name="URL">jdbc:mysql://localhost:3306/hdbsample</xa-datasource-property>
          <xa-pool>
               <min-pool-size>10</min-pool-size>
               <max-pool-size>20</max-pool-size>
               <prefill>true</prefill>
          </xa-pool>
          <security>
               <user-name>root</user-name>
               <password></password>
          </security>
       </xa-datasource>
</datasources>

Normally we copy the datasource files into the JBoss deployments directory, when the AS is not running. But now, for demonstrating the runtime behavior, we copy it when it’s running. Start your JBoss instance, and when it’s finished loading, copy the previously created xml file into the deployments directory and watch the log messages. If you did it correctly, the console’ll write a log message like this:

13:41:24,386 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-8) JBAS015876: Starting deployment of "hdbsample-jboss7-ds.xml"
13:41:24,581 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-2) JBAS010400: Bound data source [jboss/datasources/HDBSample]
13:41:24,693 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018559: Deployed "hdbsample-jboss7-ds.xml"

You can check it in the web admin console. As you can see, an external datasource is shown only in the deployment list:
If you delete or cut it into another directory, the console’ll write this:

13:42:49,820 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-6) JBAS010409: Unbound data source [jboss/datasources/HDBSample]
13:42:49,820 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-6) JBAS015877: Stopped deployment hdbsample-jboss7-ds.xml in 26ms
13:42:49,870 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018558: Undeployed "hdbsample-jboss7-ds.xml"

Useful article:

Configure and use VSCode for Java web development

Embarking on Java web development often starts with choosing the right tools that streamline the coding process while enhancing productivity...