Java 使用Jndi的oracle数据库Tomcat连接池
在我的项目中,我想使用tomcat连接池,用于oracle DB,使用jndi并在执行以下代码时获取异常: 这是我得到的一个例外:Java 使用Jndi的oracle数据库Tomcat连接池,java,oracle,tomcat,connection-pooling,Java,Oracle,Tomcat,Connection Pooling,在我的项目中,我想使用tomcat连接池,用于oracle DB,使用jndi并在执行以下代码时获取异常: 这是我得到的一个例外: javax.naming.NameNotFoundException: Name java: is not bound in this Context at org.apache.naming.NamingContext.lookup(NamingContext.java:803) at org.apache.naming.NamingConte
javax.naming.NameNotFoundException: Name java: is not bound in this Context
at org.apache.naming.NamingContext.lookup(NamingContext.java:803)
at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at com.iton.dbcp.DBUtil.getConnection(DBUtil.java:28)
at com.iton.dbcp.TestServlet.doGet(TestServlet.java:35)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.p
rocessConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpo
int.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFol
lowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
ool.java:684)
at java.lang.Thread.run(Thread.java:662)
这些是我用于连接池的文件
我将以下配置放在/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"/>
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\u id”version=“2.5”>
特斯塔普
<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>
Oracle测试应用程序
Oracle数据源示例
jdbc/pooldb
javax.sql.DataSource
容器
abscd
com.iton.dbcp.TestServlet
abscd
/*
在/conf/context.xml中:
<!-- The contents of this file will be loaded for each web application -->
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<ResourceLink global="jdbc/pooldb" name="jdbc/pooldb" type="javax.sql.DataSource"/>
</Context>
WEB-INF/WEB.xml
请解决此问题此错误:
javax.naming.NameNotFoundException:Name java:未在此上下文中绑定
是由这一行引起的:
Context-envContext=(Context)initContext.lookup(“java:/comp/env”);//不正确
问:如果将代码更改为initContext.lookup(“java:comp/env”);
,会发生什么
问:您是否有可能无意中更改了默认上下文
此链接可能有助于:
确保useNaming为“true”:
最后,
我认为您缺少server.xml条目。 首先,需要在Tomcat中配置JNDI数据源,方法是将资源声明添加到server.xml文件中,该文件位于Tomcat安装的/conf目录中 输入并指定应用程序的值
<Resource name="jdbc/your datasource" auth="Container"
type="javax.sql.DataSource" removeAbandoned="true"
removeAbandonedTimeout="30" maxActive="100"
maxIdle="30" maxWait="10000" username=""
password=""
driverClassName="oracle driver"
url="database connection string"/>
</Context>
应该是:
Context envContext = (Context) initContext.lookup("java:comp/env");
Java环境上下文是Java:comp/env。在comp之前删除该/即可。Thaks for ur reply我删除了斜杠(/),但它不起作用
Context envContext = (Context) initContext.lookup("java:/comp/env");
Context envContext = (Context) initContext.lookup("java:comp/env");