Pages

Saturday, May 15, 2010

JNDI Resource Configuration in Tomcat 5.5

JNDI Resource Configuration to maintain database connections. Follow the the steps below :

1. Open Context.xml file, which is located under tomcat/conf/context.xml
2. Add Following Resource configuration under tag.



3. Open your applications web.xml file and add resource-reference in that.


DB Connection
jdbc/test
javax.sql.DataSource
Container


4. Look at the following code to lookup JNDI

private static InitialContext ctx = null;
private static DataSource ds = null;
try
{
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/test");
if(ds != null){
log.info("DbUtils: datasource setup successfully.");
}
}catch (Exception e)
{
System.out.println("Error is .. "+e.getMessage());
}


5. Finally, deploy your application and use it.

No comments:

Post a Comment