This post covers the steps to configure a MySQL datasource in JBoss Application Server 7. This datasource can then be referenced via JNDI to connect your application to the underlying Database. I presume you already have a JBoss AS 7 up and running in your development environment. If not, do that first.
Download the MySQL JDBC database driver
- Download the latest JDBC driver for MySQL from http://dev.mysql.com/downloads/connector/j/
- Save the downloaded zip file and extract the connector jar.
Add the MySQL connector Driver as a Module in Server
- Goto: <JBoss-AS-7-Home>/modules/com
- Create folders mysql/main and copy the above downloaded JAR file(mysql-connector-java-5.1.21-bin.jar) in it.
- So your folder structure should now look like:<JBoss-AS-7-Home>/modules/com/mysql/main/mysql-connector-java-5.1.21-bin.jar
- Now that the Driver/Connector JAR is present, we need to define it for the Application Server.
- Goto: <JBoss-AS-7-Home>/modules/com/mysql/main folder where you copied the JAR file and create another file module.xml
- Paste the following code into it and save.
<?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.21-bin.jar"/> </resources> <dependencies> <module name="javax.api"/> </dependencies> </module>
Since I’m using mysql-connector-java-5.1.21-bin.jar version so I’ve kept the <resource-root path = “mysql-connector-java-5.1.21-bin.jar”/> in case you have a different version then give the same in the path attribute above, highlighted in the code-snippet.
Add the Driver reference in the Server
- Goto: <JBoss-AS-7-Home>/standalone/configuration/standalone.xml
- In the standalone.xml file add the following line in the <drivers> node.
<driver name="mysql" module="com.mysql"/>
This configures the support for a MySQL datasource in your JBoss Application server.
If you wish you can add your datasource in the standalone.xml file itself. But I prefer to keep the datasource file in my Application.
Creating the Datasource
- Create an xml file your-datasource.xml under your WEB-INF folder.
- This is a sample datasource file. my-sample-ds.xml
<?xml version="1.0" encoding="UTF-8"?> <datasources xmlns="http://www.jboss.org/ironjacamar/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd"> <!-- The datasource is bound into JNDI at this location. We reference this in META-INF/persistence.xml --> <datasource jndi-name="java:jboss/datasources/your-ds" pool-name="sample-pool" enabled="true" use-java-context="true" jta="true"> <connection-url>jdbc:mysql://your-server-address/your-db-name</connection-url> <driver>mysql</driver> <security> <user-name>your-username</user-name> <password>your-password</password> </security> </datasource> </datasources>
Referencing the datasource in your application
- Now that we have everything, we just need to refer this in our application.
- We can do this from persistence.xml if you’re using JPA standards.
- This is a sample persistence.xml which I keep under META-INF directory.
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="primary"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:jboss/datasources/your-ds</jta-data-source> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties> <!-- Properties for Hibernate --> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.use_sql_comments" value="true" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> </properties> </persistence-unit> </persistence>
I used Hibernate above as my default JPA provider.
So this was all about creating and configuring a MySQL datasource in JBoss AS 7.