{"id":12,"date":"2011-07-12T16:36:40","date_gmt":"2011-07-12T16:36:40","guid":{"rendered":"https:\/\/zorq.net\/b\/?p=12"},"modified":"2012-04-17T08:27:09","modified_gmt":"2012-04-17T08:27:09","slug":"adding-a-mysql-datasource-to-jboss-as-7","status":"publish","type":"post","link":"https:\/\/zorq.net\/b\/2011\/07\/12\/adding-a-mysql-datasource-to-jboss-as-7\/","title":{"rendered":"Adding a MySQL datasource to JBoss AS 7"},"content":{"rendered":"<p>This post assumes you are using a Unix like system with a bash shell and have already downloaded and extracted the JBoss AS 7 application server.<\/p>\n<h2>Retrieve the MySQL Connector<\/h2>\n<p>The connector can be downloaded from: <a title=\"MySQL Connector J\" href=\"http:\/\/dev.mysql.com\/downloads\/connector\/j\/\" target=\"_blank\">http:\/\/dev.mysql.com\/downloads\/connector\/j\/<\/a>. The current version at the time of writing is 5.1.17, further sections of this guide assume that version number.<\/p>\n<p>Unzip the connector to your downloads folder and save the location of the main JAR in a variable which we can refer to later:<\/p>\n<pre lang=\"bash\">cd ~\/Downloads\r\nunzip mysql-connector-java-5.1.17.zip\r\nexport MYSQL_JAR=~\/Downloads\/mysql-connector-5.1.17\/mysql-connector-java-5.1.17-bin.jar<\/pre>\n<h2>Add a Module to AS 7<\/h2>\n<p>AS 7 uses a module system to provide isolation in class loading. We need to create a new module which contains the MySQL Connector J JAR. Move to the the AS installation directory and create the folder structure for the new module:<\/p>\n<pre lang=\"bash\">\r\nexport JBOSS_HOME=~\/Development\/jboss-as-web-7.0.0.Final\r\ncd $JBOSS_HOME\r\nmkdir -p modules\/com\/mysql\/main<\/pre>\n<p>Copy the driver jar to the new directory and move to that directory:<\/p>\n<pre lang=\"bash\">cp $MYSQL_JAR $JBOSS_HOME\/modules\/com\/mysql\/main\r\ncd $JBOSS_HOME\/modules\/com\/mysql\/main<\/pre>\n<p>Define the module in XML. This is the key part of the process:<\/p>\n<pre lang=\"bash\">vi module.xml<\/pre>\n<p>If the version of the jar has changed, remember to update it here:<\/p>\n<pre lang=\"xml\">\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n\r\n<module xmlns=\"urn:jboss:module:1.0\" name=\"com.mysql\">\r\n  <resources>\r\n    <resource-root path=\"mysql-connector-java-5.1.17-bin.jar\"\/>\r\n  <\/resources>\r\n  <dependencies>\r\n    <module name=\"javax.api\"\/>\r\n  <\/dependencies>\r\n<\/module>\r\n<\/pre>\n<p>The new module directory should now have the following contents:<\/p>\n<pre lang=\"bash\">\r\nmodule.xml\r\nmysql-connector-java-5.1.17-bin.jar\r\n<\/pre>\n<h2>Create a Driver Reference<\/h2>\n<p>Now the module has been created, we need to make a reference to it from the main application server configuration file:<\/p>\n<pre lang=\"bash\">\r\ncd $JBOSS_HOME\/standalone\/configuration\r\nvi standalone.xml\r\n<\/pre>\n<p>Find the &#8216;drivers&#8217; element and add a new driver to it:<\/p>\n<pre lang=\"xml\">\r\n<drivers>\r\n    <driver name=\"mysql\" module=\"com.mysql\"\/>\r\n    <driver name=\"h2\" module=\"com.h2database.h2\">\r\n        <xa-datasource-class>\r\n            org.h2.jdbcx.JdbcDataSource\r\n        <\/xa-datasource-class>\r\n    <\/driver>\r\n<\/drivers>\r\n<\/pre>\n<p>The &#8216;h2&#8217; driver is part of the default JBoss configuration. The new driver that needs adding is named &#8216;mysql&#8217;.<\/p>\n<h2>Add the Datasource<\/h2>\n<p>Go into the configuration directory and open the main configuration file:<\/p>\n<pre lang=\"bash\">\r\ncd $JBOSS_HOME\/standalone\/configuration\r\nvi standalone.xml\r\n<\/pre>\n<p>Find the datasources element and add a new datasource inside:<\/p>\n<pre lang=\"xml\">\r\n<datasource\r\n        jndi-name=\"java:\/mydb\" pool-name=\"my_pool\"\r\n        enabled=\"true\" jta=\"true\"\r\n        use-java-context=\"true\" use-ccm=\"true\">\r\n    <connection-url>\r\n        jdbc:mysql:\/\/localhost:3306\/mydb\r\n    <\/connection-url>\r\n    <driver>\r\n        mysql\r\n    <\/driver>\r\n    <security>\r\n        <user-name>\r\n            root\r\n        <\/user-name>\r\n        <password>\r\n            \r\n        <\/password>\r\n    <\/security>\r\n    <statement>\r\n        <prepared-statement-cache-size>\r\n            100\r\n        <\/prepared-statement-cache-size>\r\n        <share-prepared-statements\/>\r\n    <\/statement>\r\n<\/datasource>\r\n<\/pre>\n<h2>Start the Application Server<\/h2>\n<p>Now try running the application server. Make sure you run initially as a user which has write access to the modules directory where you placed the MySQL connector JAR. This is because the appication server seems to generate an index of the directories inside the JAR.<\/p>\n<pre lang=\"bash\">\r\ncd $JBOSS_HOME\r\nbin\/standalone.xml\r\n<\/pre>\n<p>Hopefully, amongst the application server&#8217;s console output you should see the following:<\/p>\n<pre lang=\"bash\">\r\n20:31:33,843 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-1) Bound data source [java:\/mydb]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This post assumes you are using a Unix like system with a bash shell and have already downloaded and extracted the JBoss AS 7 application server. Retrieve the MySQL Connector The connector can be downloaded from: http:\/\/dev.mysql.com\/downloads\/connector\/j\/. The current version at the time of writing is 5.1.17, further sections of this guide assume that version [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[22,21,4],"class_list":["post-12","post","type-post","status-publish","format-standard","hentry","category-jboss","tag-java","tag-jboss","tag-mysql"],"_links":{"self":[{"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/posts\/12","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/comments?post=12"}],"version-history":[{"count":26,"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/posts\/12\/revisions"}],"predecessor-version":[{"id":65,"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/posts\/12\/revisions\/65"}],"wp:attachment":[{"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/media?parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/categories?post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zorq.net\/b\/wp-json\/wp\/v2\/tags?post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}