{"id":67,"date":"2014-07-25T13:00:27","date_gmt":"2014-07-25T13:00:27","guid":{"rendered":"http:\/\/www.solewing.org\/blog\/?p=67"},"modified":"2014-07-25T13:21:13","modified_gmt":"2014-07-25T13:21:13","slug":"wildfly-datasource-deployment","status":"publish","type":"post","link":"http:\/\/www.solewing.org\/blog\/2014\/07\/wildfly-datasource-deployment\/","title":{"rendered":"Wildfly Datasource Deployment"},"content":{"rendered":"<p>One of the things I really like about <a title=\"Wildfly Web Site\" href=\"http:\/\/wildfly.org\">Wildfly<\/a> and its immediate predecessors is the ability to deploy JDBC drivers and datasource configurations without needing to edit the configuration or restart the container.<\/p>\n<h2>JDBC Driver Deployments<\/h2>\n<p>The ability to treat a JDBC driver as a deployment depends on the Java\u00a0<a title=\"ServiceLoader Javadoc\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/ServiceLoader.html\">service loader<\/a>\u00a0mechanism that was introduced with JDK 6 and incorporated in JDBC 4. \u00a0Virtually every database vendor has a JDBC4-compliant driver that can be auto-deployed in Wildfly by simply dropping the JAR file for the driver into the container&#8217;s\u00a0<em>deployments<\/em>\u00a0directory. \u00a0Once the JDBC driver has been deployed, one or more datasources can be configured that make use of it.<\/p>\n<h2>DataSource Deployments<\/h2>\n<p>The ability to deploy datasource configurations is enabled\u00a0by <a title=\"IronJacamar Web Site\" href=\"http:\/\/www.ironjacamar.org\">IronJacamar<\/a>\u00a0which Wildfly uses for its Java Connector Architecture (JCA) support. \u00a0 An IronJacamar datasource configuration is specified in an XML file that defines\u00a0the JNDI name for the datasource along with all of the configuration needed to access and manage the underlying database resource.<\/p>\n<p>Here&#8217;s an example (non-XA) datasource configuration for a MySQL datasource. As you can see, most of the element and attribute values in the file are fairly intuitive.<\/p>\n<p>[xml]<br \/>\n<?xml version=\"1.0\" encoding=\"UTF-8\"?><br \/>\n<datasources xmlns=\"http:\/\/www.jboss.org\/ironjacamar\/schema\"><br \/>\n  <datasource jndi-name=\"java:jboss\/datasources\/myDataSource\" pool-name=\"myDataSource\" enabled=\"true\" use-ccm=\"false\"><br \/>\n    <connection-url>jdbc:mysql:\/\/localhost:3306\/my_database<\/connection-url><br \/>\n    <driver-class>com.mysql.jdbc.Driver<\/driver-class><br \/>\n    <driver>mysql-connector-java-5.1.29.jar<\/driver><br \/>\n    <security><br \/>\n      <user-name>my_username<\/user-name>\n      <password>my_password<\/password>\n    <\/security><br \/>\n    <validation><br \/>\n      <valid-connection-checker class-name=\"org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker\"\/><br \/>\n      <stale-connection-checker class-name=\"org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLStaleConnectionChecker\"\/><br \/>\n      <exception-sorter class-name=\"org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter\"\/><br \/>\n    <\/validation><br \/>\n  <\/datasource><br \/>\n<\/datasources><br \/>\n[\/xml]<\/p>\n<p>You can deploy a datasource descriptor such as the previous example, by placing it a file with name that is suffixed with <code>-ds.xml<\/code> and dropping it into the <em>deployments<\/em> directory. The example datasource shown above might be named <code>my_datasource-ds.xml<\/code>.<\/p>\n<p>By copying <code>my_datasource-ds.xml<\/code> into the deployments directory, IronJacamar will use the database driver specified in the descriptor to create a datasource object and bind it into the container&#8217;s JDNI registry with the given JNDI name. Once the datasource is deployed, your applications can access it just as they would any other JNDI resource.<\/p>\n<h2>XA DataSource Deployments<\/h2>\n<p>IronJacamar also supports XA datasources, which are important if you need to make use of transactions that involve more than one database and\/or a database and a JMS message broker.<\/p>\n<p>The configuration descriptor for an XA datasource is quite similar to the basic datasource, as you can see in the following example for an XA datasource using a PostgreSQL driver.<\/p>\n<p>[xml]<br \/>\n<?xml version=\"1.0\" encoding=\"UTF-8\"?><br \/>\n<datasources xmlns=\"http:\/\/www.jboss.org\/ironjacamar\/schema\"><br \/>\n  <xa-datasource jndi-name=\"java:jboss\/datasources\/myXADataSource\" pool-name=\"myXADataSource\" enabled=\"true\" use-ccm=\"false\"><\/p>\n<p>    <xa-datasource-property name=\"ServerName\">localhost<\/xa-datasource\n-property><br \/>\n    <xa-datasource-property name=\"PortNumber\">5458<\/xa-datasource-property><br \/>\n    <xa-datasource-property name=\"DatabaseName\">my_database<\/xa-datasource-property><br \/>\n    <xa-datasource-class>org.postgresql.xa.PGXADataSource<\/xa-datasource-class><\/p>\n<p>    <driver>postgresql-9.1-902.jdbc4.jar<\/driver><br \/>\n    <security><br \/>\n      <user-name>my_username<\/user-name>\n      <password>my_password<\/password>\n    <\/security><br \/>\n    <validation><br \/>\n      <valid-connection-checker class-name=\"org.jboss.jca.adapters.jdbc.extensio\nns.postgres.PostgreSQLValidConnectionChecker\"\/><br \/>\n      <validate-on-match>true<\/validate-on-match><br \/>\n      <background-validation>false<\/background-validation><br \/>\n      <stale-connection-checker class-name=\"org.jboss.jca.adapters.jdbc.extensio\nns.postgres.PostgreSQLStaleConnectionChecker\"\/><br \/>\n      <exception-sorter class-name=\"org.jboss.jca.adapters.jdbc.extensions.postg\nres.PostgreSQLExceptionSorter\"\/><br \/>\n    <\/validation><br \/>\n  <\/xa-datasource><br \/>\n<\/datasources><br \/>\n[\/xml]<\/p>\n<p>Note that the property names specified by <code>&lt;xa-datasource-property&gt;<\/code> are driver specific \u2014 a driver from another database vendor would use different property names.<\/p>\n<h2>References<\/h2>\n<ul>\n<li><a href=\"http:\/\/www.ironjacamar.org\/documentation.html\">IronJacamar Documentation<\/a><\/li>\n<li><a href=\"https:\/\/docs.jboss.org\/author\/display\/WFLY8\/Developer+Guide\">Wildfly Developer Guide<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>One of the things I really like about Wildfly and its immediate predecessors is the ability to deploy JDBC drivers and datasource configurations without needing to edit the configuration or restart the container. JDBC Driver Deployments The ability to treat a JDBC driver as a deployment depends on the Java\u00a0service loader\u00a0mechanism that was introduced with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[21,20,19],"class_list":["post-67","post","type-post","status-publish","format-standard","hentry","category-javaee","tag-datasource","tag-jdbc","tag-wildfly-2"],"_links":{"self":[{"href":"http:\/\/www.solewing.org\/blog\/wp-json\/wp\/v2\/posts\/67","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.solewing.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.solewing.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.solewing.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.solewing.org\/blog\/wp-json\/wp\/v2\/comments?post=67"}],"version-history":[{"count":4,"href":"http:\/\/www.solewing.org\/blog\/wp-json\/wp\/v2\/posts\/67\/revisions"}],"predecessor-version":[{"id":71,"href":"http:\/\/www.solewing.org\/blog\/wp-json\/wp\/v2\/posts\/67\/revisions\/71"}],"wp:attachment":[{"href":"http:\/\/www.solewing.org\/blog\/wp-json\/wp\/v2\/media?parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.solewing.org\/blog\/wp-json\/wp\/v2\/categories?post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.solewing.org\/blog\/wp-json\/wp\/v2\/tags?post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}