Tuesday, August 9, 2011

JBoss AS 7 and an Oracle Datasource

Here are step by step directions on how I configured my development instance of JBoss to communicate with Oracle XE.

1. Create directory for the oracle driver deployment
$ cd $JBOSS_HOME/modules
$ mkdir -p com/oracle/ojdbc6/main
$ vi module.xml

Add the following snippet to the newly created module.xml file
<module xmlns="urn:jboss:module:1.0" name="com.oracle.ojdbc6">
  <resources>
    <resource-root path="ojdbc6.jar"/>
  </resources>
  <dependencies>
    <module name="javax.api"/>
  </dependencies>
</module>

Copy the ojdbc6.jar to $JBOSS_HOME/modules/com/oracle/ojdbc6/main

Next, create the driver definition in the standalone configuration file
$ vi $JBOSS_HOME/standalone/configuration/standalone.xml

Look for <subsystem xmlns="urn:jboss:domain:datasources:1.0"> - this is where the datasource and driver configuration will go. Within this subsystem, look for the <drivers> section and add the following:
<driver name="oracle" module="com.oracle.ojdbc6">
  <xa-datasource-class>
    oracle.jdbc.OracleDriver
  </xa-datasource-class>
</driver>

Then create the datasource configuration, also in the standalone configuration file. The following block will go just below the <datasources> element.
<datasource jndi-name="WorkCenterDS" pool-name="OracleDS" enabled="true" jta="true" use-java-context="true" use-ccm="true">
  <connection-url>jdbc:oracle:thin:@localhost:1521:oradb1</connection-url>
  <driver>oracle</driver>
  <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
  <pool>
    <prefill>true</prefill>
    <use-strict-min>false</use-strict-min>
    <flush-strategy>FailingConnectionOnly</flush-strategy>
  </pool>
  <security>
    <user-name>user1</user-name>
    <password>1234</password>
  </security>
</datasource>

Restart JBoss and look for the following lines in your log file to determine if the deployment succeeded.

DEBUG [org.jboss.as.connector.deployer.dsdeployer] (MSC service thread 1-4) Adding datasource: java:/WorkCenterDS
INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-6) Bound data source [java:WorkCenterDS]

8 comments:

  1. Thanks for posting this. I looked yesterday for 2 hours and no one else mentioned creating the modules/com/oracle/ojdbc6/main folder and moving ojdbc6.jar there.
    Could you post your persistence.xml file? I'm getting a null pointer and some persistence errors.

    ReplyDelete
  2. Glad it helped. The placement of the jar file was what tripped me up for a bit as well. I actually don't use JPA/CMP at this point. I'm still using good old fashioned hibernate mapping files so I wouldn't be much help there.

    ReplyDelete
  3. Very helpful article.

    First Note: you should change your jndi-name so that it starts with java:/ or java:jboss/ (see item #5 here: http://planet.jboss.org/post/how_to_create_an_manage_datasources_in_as7 ). My deployed war, which uses hibernate, could not connect until I changed the jndi-name.

    Second Note: Although you do not go into detail about hibernate configurations, others might find this note (http://javalabor.blogspot.com/2011/08/oracle-datasource-in-jboss-as-7.html) about Oracle11gDialect here helpful.

    A question: how did you go about selecting the transaction-isolation and pool elements and values? Are these oracle optimizations?

    ReplyDelete
  4. Very helpful! Thanks very much.

    ReplyDelete
  5. @Erik
    I'll definitely check out the jndi-name, however it was working as is. As for the transaction-isolation and connection pool settings, I didn't spend much time with these as this was merely my development environment; read-committed is the default isolation level for oracle. As for the pool settings, the prefill value is unnecessary since I don't have a min-pool-size set [default: 0]

    Here's a link that describes all the datasource configuration options in detail.

    http://docs.jboss.org/ironjacamar/userguide/1.0/en-US/html/deployment.html#deployingds_descriptor

    ReplyDelete
  6. Is it ok if I will put a link of your wonderful article on our companys blog (with additional informations)?

    ReplyDelete
  7. @Bernhard
    You are more than welcome to! Thanks.

    ReplyDelete
  8. Gaawddd thanks a billion for this post man !

    ReplyDelete