Today we've struggled for more then three hours figuring out how to use Mysql on our JBoss server. I am going to describe steps by step because I found this very confusing.
Lets get started. I assume you have downloaded and extracted JBoss 7.1 AS
There are two ways how you can use Mysql in JBoss. We are going the easier one because in the second one you have to edit 1400lines long XML (the bloody hell!!).
- download Mysql Java Connector
- extract .jar to separete folder (for example mysql-connector-java-5.1.22-bin.jar)
- in that folder create directory META-INF and inside that one create services and there create file called java.sql.Driver (so it will look META-INF/services/java.sql.Driver)
- into that driver write only this line:
- go back to the folder with .jar and run this command (replace name of your /jar accordingly):
jar -uf mysql-connector-java-5.1.22-bin.jar META-INF/services/java.sql.Driver
- go to your JBoss bin folder and run script add-user.bat (add-user-sh on unix)
- choose a) Management User (mgmt-users.properties) -> ENTER -> fill username -> fill password -> repeat password
- in your browser open http://localhost:9990/console/ and login using that username/password
- in administration console switch to Runtime (upper right corner) and then choose Manage Deployments. There click on the Add Content button and choose path the .jar extracted above. At last click on the enable button. Finally it should look like this:
- switch to Profile (upper right corner http://localhost:9990/console/App.html#datasources) and open Connector / Datasources and click on the Add button
- specify name and JNDI name - you will fill this up in your persistent.xml. Remember that JNDI name must begin with java:/ ... so for example java:/testDS
- in second step choose mysql-connector-java-5.1.22-bin.jar connector.
- connection URL will look like: jdbc:mysql://localhost/database_name
- fill also your mysql username and mysql password and click on Done (leave security domain blank).
- now click on the Enable button to enable datasource
- in your persistent.xml you can use created datasource like this:
<?xml version="1.0" encoding="UTF-8"?>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.archive.autodetection" value="class" />