gpt4 book ai didi

javax.naming.NameNotFoundException : Name [comp/env] is not bound in this Context. Java 调度程序无法找到 [comp] 错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:53:37 25 4
gpt4 key购买 nike

我想做的是在一段时间后更新我的数据库。所以我正在使用 java 调度程序和连接池。我不知道为什么,但我的代码只能工作一次。它将打印:

init success
success
javax.naming.NameNotFoundException: Name [comp/env] is not bound in this Context. Unable to find [comp].
at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
at org.apache.naming.NamingContext.lookup(NamingContext.java:168)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:158)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at test.Pool.main(Pool.java:25) ---> line 25 is Context envContext = (Context)initialContext.lookup("java:/comp/env");

我不知道为什么它只能工作一次。如果我没有在没有 java 调度程序的情况下运行它,我已经测试过它并且它工作正常。没有任何错误。如果我使用调度程序运行它,不知道为什么会出现此错误。

希望有人能帮助我。

我的连接池代码:

public class Pool {
public DataSource main() {
try {
InitialContext initialContext = new InitialContext();
Context envContext = (Context)initialContext.lookup("java:/comp/env");
DataSource datasource = new DataSource();
datasource = (DataSource)envContext.lookup("jdbc/test");
return datasource;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}

我的 web.xml:

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<listener>
<listener-class> package.test.Pool</listener-class>
</listener>
<resource-ref>
<description>DB Connection Pooling</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

上下文.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<Context path="/project" reloadable="true">
<Resource auth="Container"
defaultReadOnly="false"
driverClassName="com.mysql.jdbc.Driver"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
initialSize="0"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
jmxEnabled="true"
logAbandoned="true"
maxActive="300" maxIdle="50"
maxWait="10000"
minEvictableIdleTimeMillis="300000"
minIdle="30"
name="jdbc/test"
password="test"
removeAbandoned="true"
removeAbandonedTimeout="60"
testOnBorrow="true"
testOnReturn="false"
testWhileIdle="true"
timeBetweenEvictionRunsMillis="30000"
type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/database?noAccessToProcedureBodies=true"
username="root"
validationInterval="30000"
validationQuery="SELECT 1"/>
</Context>

我的 java 调度器

public class Scheduler extends HttpServlet{

public void init() throws ServletException
{
System.out.println("init success");
try{
Scheduling_test test = new Scheduling_test();
ScheduledExecutorService executor = Executors.newScheduledThreadPool(100);
ScheduledFuture future = executor.scheduleWithFixedDelay(test, 1, 60 ,TimeUnit.SECONDS);
}catch(Exception e){
e.printStackTrace();
}
}

Schedule_test

    public class Scheduling_test extends Thread implements Runnable{
public void run(){
Updating updating = new Updating();
updating.run();
}
}

正在更新

public class Updating{

public void run(){
ResultSet rs = null;
PreparedStatement p = null;
StringBuilder sb = new StringBuilder();
Pool pool = new Pool();
Connection con = null;
DataSource datasource = null;
try{
datasource = pool.main();
con=datasource.getConnection();
sb.append("SELECT * FROM database");
p = con.prepareStatement(sb.toString());
rs = p.executeQuery();
rs.close();
con.close();
p.close();
datasource.close();
System.out.println("success");
}catch (Exception e){
e.printStackTrace();
}
}

最佳答案

此错误表明您的jdbc 资源未注册!您将 context.xml 放在哪里?

context.xml 文件必须在 war 文件的 META-INF 目录中。它不得位于classes 目录jar 文件中。

META-INF 目录和 context.xml 放在包含 webapproot 的目录中> 在您的源文件夹树中。

关于javax.naming.NameNotFoundException : Name [comp/env] is not bound in this Context. Java 调度程序无法找到 [comp] 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12213246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com