Skip to content

Instantly share code, notes, and snippets.

@shaik2many
Created August 26, 2016 03:49
Show Gist options
  • Save shaik2many/13b7002d8a0325d9a540aa475eb0db3f to your computer and use it in GitHub Desktop.
Save shaik2many/13b7002d8a0325d9a540aa475eb0db3f to your computer and use it in GitHub Desktop.
http://www.journaldev.com/2513/tomcat-datasource-jndi-example-java
-------------
Step1: runtime/conf/context.xml
<Context>
<Resource name="APP_DS" auth="Container" type="javax.sql.DataSource"
maxTotal="60"
maxIdle="20"
maxWaitMillis="10000"
username="username" password="password" driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@host:1766:sid"/>
</context>
Step2: myapp/conf/web.xml
<web-app>
<resource-ref>
<description>oracle Datasource example</description>
<res-ref-name>APP_DS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<web-app>
Step3:
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/MyLocalDB");
(or)
ctx = new InitialContext();
Context initCtx = (Context) ctx.lookup("java:/comp/env");
DataSource ds = (DataSource) initCtx.lookup("jdbc/MyLocalDB");
===========================
Tomcat DataSource JNDI:
Apache Tomcat provide three ways to configure DataSource in JNDI context.
Option1: Application context.xml – This is the easiest way to configure DataSource, all we need is a context.xml file in META-INF directory. We have to define Resource element in the context file and container will take care of loading and configuring it. The approach is simple but it has some drawbacks;
Since the context file is bundled with the WAR file, we need to build and deploy new WAR for every small configuration change. Same issue comes if your application works in distributed environment or your application needs to be deployed in different testing environments such as QA, IT, PROD etc.
The datasource is created by container for the application usage only, so it can’t be used globally. We can’t share the datasource across multiple applications.
If there is a global datasource (server.xml) defined with same name, the application datasource is ignored.
Option2: Server context.xml – If there are multiple applications in the server and you want to share DataSource across them, we can define that in the server context.xml file. This file is located in apache-tomcat/conf directory. The scope of server context.xml file is application, so if you define a DataSource connection pool of 100 connections and there are 20 applications then the datasource will be created for each of the application. This will result in 2000 connections that will obviously consume all the database server resources and hurt application performance.
Option3: server.xml and context.xml – We can define DataSource at global level by defining them in the server.xml GlobalNamingResources element. If we use this approach, then we need to define a ResourceLink from context.xml file of server or application specific. This is the preferred way when you are looking to share a common resource pool across multiple applications running on the server. Regarding resource link, whether to define it at server level context xml file or application level depends on your requirement.
================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment