Friday, December 7, 2007

JBoss: Configuring Data Sources

Configuring data sources in JBoss is quite simple but documentation may be a little difficult to find. Hopefully, this will demonstrate HOW simple it really is.

First, let's say that you have a web application (or some other application) that is expecting a data source to be present in JNDI. Maybe you have designed the application that needs to support multiple databases (kudos to you). Or maybe the application will be deployed into multiple environments and you do not want to force clients to configure the data source your way with your connection information. Whatever the reason, the data source configured within the container and handed to your application.

Next comes the real easy steps. To configure the data source in jboss, you need to perform 2 steps.

  1. Drop the JDBC driver jar file into the JBoss classpath. If you're running the 'default' server under JBoss, drop the jar file in $JBOSS_HOME/server/default/lib. JBoss will pick it when the server starts. Here are 2 links to JDBC driver jars:

    • PostgreSql
    • SQL Server
    • Oracle drivers are located under the Oracle installation directory. For 10g, look under C:\oracle\product\10.2.0\db_1\jdbc\lib.

  2. Create the data source XML file in the deploy directory. Once again, if you're running the 'default' server, create the XML file in $JBOSS_HOME/server/default/deploy. Upon startup, JBoss will load this DS file and configure the data source according to the XML file. Sample XML files for the different DBs are located in the $JBOSS_HOME/docs/examples/jca directory.

    • Sample Oracle DS

      <datasources>
      <local-tx-datasource>
      <jndi-name>COP-DS</jndi-name>
      <connection-url>jdbc:oracle:thin:@localhost:1521:ORCL</connection-url>
      <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
      <user-name>user</user-name>
      <password>password</password>
      <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name>
      <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
      <min-pool-size>2</min-pool-size>
      <max-pool-size>5</max-pool-size>
      <metadata>
      <type-mapping>Oracle9i</type-mapping>
      </metadata>
      </local-tx-datasource>
      </datasources>


    • Sample SQL Server DS

      <datasources>
      <local-tx-datasource>
      <jndi-name>COP-DS</jndi-name>
      <connection-url>jdbc:jtds:sqlserver://localhost:1433/cop</connection-url>
      <driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
      <user-name>affor</user-name>
      <password>password</password>
      <check-valid-connection-sql>select 1</check-valid-connection-sql>
      <min-pool-size>2</min-pool-size>
      <max-pool-size>10</max-pool-size>

      <metadata>
      <type-mapping>MS SQLSERVER2000</type-mapping>
      </metadata>
      </local-tx-datasource>
      </datasources>



BEWARE: The data source file must be name with '-ds.xml'. That's a dash and NOT an underscore. That little character cost me a few hours of troubleshooting only to be identified by another person.

For more detailed information or for other DB configurations, visit the JBoss wiki on data sources.

7 comments:

jlorenzen said...

<xml>In order to view eXtensible Markup Language in your blog escape less than and greater than signs like so http://www.theukwebdesigncompany.com/articles/entity-escape-characters.php.</xml>

Jeffrey S. Hair said...

Is there a way to post an entire XML document without having to place greater than & less than signs around every element?

One XML document with multiple elements...ouch.

Chad Sturtz said...

The only way i've ever posted xml is by escaping the < and > signs as James mentioned.

On a side note, put your xml inside a <pre> tag. This will allow the xml to maintain it's formatting when it shows up on the blog.

Jeffrey S. Hair said...

Thanx Chad! I've updated the blog to include an XML sample datasource.

Bob said...
This comment has been removed by the author.
Bob said...
This comment has been removed by the author.
Bob said...

Hi, I am new to JBoss and was wondering; under what scenario would one require the use of xa-datasource instead of local-tx-datasource?