Tuesday, March 9, 2010

How to set datasource for Oracle in Jboss

A DataSource object is the representation of a data source in the Java
programming language. In basic terms, a data source is a facility for storing data.
It can be as sophisticated as a complex database for a large corporation or as
simple as a file with rows and columns. A data source can reside on a remote
server, or it can be on a local desktop machine. Applications access a data source
using a connection, and a DataSource object can be thought of as a factory for
connections to the particular data source that the DataSource instance represents.
The DataSource interface provides two methods for establishing a connection
with a data source.
Jboss provides a JNDI InitialContext implementation instance for each web
application running under it, in a manner that is compatible with those provided
by a Java2 Enterprise Edition application server. The J2EE standard provides a
standard set of elements in the /WEB-INF/web.xml file to reference resources;
resources referenced in these elements must be defined in an application-serverspecific
configuration.
In order to configure datasource for Oracle in Jboss I did the following steps:
1. Put Oracle's driver classes in the CLASSPATH. Copy Oracle's JDBC driver
.zip file /jdbc/lib/classes12.zip (in this case I put jdbc driver to lib) to the
server/default/lib directory.
2. Copy /docs/examples/jca/oracle-ds.xml , to /server/default/deploy dir.
3. Modify the oracle-ds.xml configuration file as per our connection
parameters like connection URL,username,password etc.
4. In web.xml making the following entry after tag.

DB Connection
jdbc/test
javax.sql.DataSource
Container

5. add a file jboss-web.xml under WEB-INF/ folder of the web-application.
This file should contain entry as:


DB Connection
jdbc/test
javax.sql.DataSource
java:/jdbc/test
Container


Notes: I am using Oracle 9i, jboss-4.2.3-GA, Jdk 1.5 for this configuration.
If we are not using Spring to lookup for jndi then use the following code to
look up:
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:/jdbc/test");
Connection conn = ds.getConnection();