Creating JNDI Data Source in Tomcat to Connect Oracle Data Source

Hi All,

In this article I will show how to create a JNDI data source in Tomcat.

When building a J2EE based application,  the daunting task we see first is the DB connectivity management. And then the Connection Pool implementation comes into the practise.  But there is always lots of issues observed by configuring data sources in web application servers.

The normal practise which every development team does is to provide the data source configuration(user/password, DB URL etc), add the JDBC drivers, define various settings for pool management.

Tomcat allows us to define this configuration context wise or application reference wise.

To define the Web Server Context wise. Simply go to [TOMCAT_HOME]/conf/server.xml, and add the the below configuration.

<Context docBase=”myapp” path=”/myapp” reloadable=”true” source=”org.eclipse.jst.j2ee.server:myapp”>
<Resource auth=”Container” connectionCacheName=”TestCache” connectionCacheProperties=”{MaxStatementsLimit=0, MinLimit=0, InitialLimit=0, ValidateConnection=true, ConnectionWaitTimeout=600, MaxLimit=10000}” connectionCachingEnabled=”true” driverclassname=”oracle.jdbc.driver.OracleDriver” factory=”oracle.jdbc.pool.OracleDataSourceFactory” name=”DataSourceName” scope=”Shareable” type=”oracle.jdbc.pool.OracleDataSource” url=”jdbc:oracle:thin:@HOST_IP:PORT:SID” password=”pa$$w0rd” user=”pooluser”/>
</Context>

To Define the configuration for application scope,
Simply create Context.xml and add the below configuration.

<Context>
<Resource name=”jdbc/DataSourceName” auth=”Container”
type=”oracle.jdbc.pool.OracleDataSource”
driverClassName=”oracle.jdbc.driver.OracleDriver”
url=”jdbc:oracle:thin:@HOST_IP:PORT:SID” password=”pa$$w0rd” user=”pooluser”
maxActive=”8″
/>
</Context>

path – this defines the name of the application
connectionCacheProperties- defines the variuos connection pool settings
driverclassname – defines the driver manager class name
name – DataSourceName [JNDI Name]
URL – DB host URL
user – connection pool user
password – password
type – defines the type of data source

Once the configuration is added,  add the Context.xml into META-INF folder of Applications .WAR file.
Now in Your JSP/Servlet/Data Access Layer add the below code to get the Connection from Data Source via JNDI lookup.

DataSource ds = (DataSource) ic.lookup(“java:comp/env/jdbc/DataSourceName”);
Connection c = ds.getConnection();

[NOTE] This only works with Tomcat, as every Web Application server have their own implementation for the same.


Thanks
R Vashi

Advertisements

2 thoughts on “Creating JNDI Data Source in Tomcat to Connect Oracle Data Source

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s