java - Tomcat conection pooling for oracle DB using Jndi -
for project want use tomcat connection pooling oracle db using jndi , getting exception while executing below code:
this exception getting:
javax.naming.namenotfoundexception: name java: not bound in context @ org.apache.naming.namingcontext.lookup(namingcontext.java:803) @ org.apache.naming.namingcontext.lookup(namingcontext.java:159) @ javax.naming.initialcontext.lookup(initialcontext.java:392) @ com.iton.dbcp.dbutil.getconnection(dbutil.java:28) @ com.iton.dbcp.testservlet.doget(testservlet.java:35) @ javax.servlet.http.httpservlet.service(httpservlet.java:689) @ javax.servlet.http.httpservlet.service(httpservlet.java:802) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(appl icationfilterchain.java:252) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationf ilterchain.java:173) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrapperv alve.java:213) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextv alve.java:178) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.j ava:126) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.j ava:105) @ org.apache.catalina.core.standardenginevalve.invoke(standardengineval ve.java:107) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.jav a:148) @ org.apache.coyote.http11.http11processor.process(http11processor.java :869) @ org.apache.coyote.http11.http11baseprotocol$http11connectionhandler.p rocessconnection(http11baseprotocol.java:664) @ org.apache.tomcat.util.net.pooltcpendpoint.processsocket(pooltcpendpo int.java:527) @ org.apache.tomcat.util.net.leaderfollowerworkerthread.runit(leaderfol lowerworkerthread.java:80) @ org.apache.tomcat.util.threads.threadpool$controlrunnable.run(threadp ool.java:684) @ java.lang.thread.run(thread.java:662)
these files using connection pool
i placed below configuration in /conf/server.xml :
<resource name="jdbc/pooldb" auth="container" type="javax.sql.datasource" maxactive="5" maxidle="2" maxwait="1000" factory="org.apache.tomcat.jdbc.pool.datasourcefactory" driverclassname="oracle.jdbc.driver.oracledriver" url="jdbc:oracle:thin:@localhost:1521:pooldb"/>
dbutil1.java:
package com.iton.dbcp; import java.sql.connection; import java.sql.sqlexception; import javax.naming.context; import javax.naming.initialcontext; import javax.naming.namingexception; import javax.sql.datasource; public class dbutil1 { public static connection getconnection() throws namingexception, sqlexception { context initcontext = new initialcontext(); context envcontext = (context) initcontext.lookup("java:/comp/env"); datasource ds = null; connection conn = null; try { ds = (datasource) envcontext.lookup("jdbc/pooldb"); } catch (exception e) { e.printstacktrace(); } if (envcontext == null) { system.out.println("envcontext null"); } if (ds == null) { system.out.println("datasource null"); } if (ds != null){ conn = ds.getconnection("pooldb","pooldb"); } return conn; } }
testservlet.java:
package com.iton.dbcp; import java.io.ioexception; import java.sql.connection; import java.sql.sqlexception; import java.sql.statement; import javax.naming.namingexception; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; public class testservlet extends httpservlet { private static final long serialversionuid = 1l; /** * @see httpservlet#httpservlet() */ public testservlet() { super(); // todo auto-generated constructor stub } /** * @see httpservlet#doget(httpservletrequest request, httpservletresponse response) */ protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { connection con = null; try { con = dbutil.getconnection(); system.out.println("opened conncetion:::::::::: "+con); statement stmt = con.createstatement(); stmt.executeupdate("create table iton(sno varchar(10), name varchar(10))"); stmt.close(); dbutil.closeconncetion(con); system.out.println("closeddddddddddddddd: "+con); } catch (namingexception e) { e.printstacktrace(); } catch (sqlexception e) { e.printstacktrace(); } }
web.xml: http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="webapp_id" version="2.5"> testapp
<description>oracle test app</description> <resource-ref> <description>oracle datasource example</description> <res-ref-name>jdbc/pooldb</res-ref-name> <res-type>javax.sql.datasource</res-type> <res-auth>container</res-auth> </resource-ref> <servlet> <servlet-name>abscd</servlet-name> <servlet-class>com.iton.dbcp.testservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>abscd</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
in /conf/context.xml:
<!-- contents of file loaded each web application --> <context> <!-- default set of monitored resources --> <watchedresource>web-inf/web.xml</watchedresource> <!-- uncomment disable session persistence across tomcat restarts --> <!-- <manager pathname="" /> --> <resourcelink global="jdbc/pooldb" name="jdbc/pooldb" type="javax.sql.datasource"/> </context>
please resolve issue
context envcontext = (context) initcontext.lookup("java:/comp/env");
should be:
context envcontext = (context) initcontext.lookup("java:comp/env");
the java environment context java:comp/env. remove / before comp , should go.
Comments
Post a Comment