gpt4 book ai didi

java - JNDI 连接适用于 Tomcat,但不适用于 Websphere

转载 作者:行者123 更新时间:2023-11-28 22:39:24 25 4
gpt4 key购买 nike

我使用与 SQL Server 的 JNDI 连接来连接我的数据库,直到今天我总是使用 tomcat 应用服务器在本地机器上运行我的应用程序:

上下文.xml

<Resource
url="jdbc:sqlserver://1.1.1.1:1434;databaseName=myDB"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
username="admin"
password="admin"
maxWait="10000"
maxIdle="30"
maxActive="100"
type="javax.sql.DataSource"
auth="Container"
name="jdbc/dataquality_sqlServer"/>

Web.xml

<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/dataquality_sqlServer</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

连接类 - GerenciadorDeConexao

public void startDB() {

Context envContext = null;
DataSource dataSource = null;


try {
Context ctx = new InitialContext();
dataSource = (DataSource) ctx.lookup("java:/comp/env/jdbc/dataquality_sqlServer");
System.out.println("conectado com sucesso!JNDI");
conexao = dataSource.getConnection();
conexao.setAutoCommit(false);

} catch (Exception e){
e.printStackTrace();
//return null;
}finally {
if(envContext != null){
try{
envContext.close();
} catch (NamingException e){
System.out.println();
e.printStackTrace();
}
}
}
}

在我的 tomcat 中一切正常,但现在我正在迁移到 Websphere 8.5 服务器。由于某种原因,它在 websphere 中不起作用,我想知道 websphere 中是否有额外的步骤。我收到以下错误:

日志

[10/22/18 9:50:10:469 BRST] 000001c0 SystemErr     R javax.naming.NameNotFoundException: Name  not found in context "java:".
[10/22/18 9:50:10:469 BRST] 000001c0 SystemErr R at com.ibm.ws.naming.ipbase.NameSpace.getParentCtxInternal(NameSpace.java:1970)
[10/22/18 9:50:10:469 BRST] 000001c0 SystemErr R at com.ibm.ws.naming.ipbase.NameSpace.retrieveBinding(NameSpace.java:1377)
[10/22/18 9:50:10:470 BRST] 000001c0 SystemErr R at com.ibm.ws.naming.ipbase.NameSpace.lookupInternal(NameSpace.java:1220)
[10/22/18 9:50:10:472 BRST] 000001c0 SystemErr R at com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpace.java:1142)
[10/22/18 9:50:10:472 BRST] 000001c0 SystemErr R at com.ibm.ws.naming.urlbase.UrlContextImpl.lookupExt(UrlContextImpl.java:1436)
[10/22/18 9:50:10:472 BRST] 000001c0 SystemErr R at com.ibm.ws.naming.java.javaURLContextImpl.lookupExt(javaURLContextImpl.java:477)
[10/22/18 9:50:10:472 BRST] 000001c0 SystemErr R at com.ibm.ws.naming.java.javaURLContextRoot.lookupExt(javaURLContextRoot.java:485)
[10/22/18 9:50:10:472 BRST] 000001c0 SystemErr R at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:370)
[10/22/18 9:50:10:472 BRST] 000001c0 SystemErr R at org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:161)
[10/22/18 9:50:10:472 BRST] 000001c0 SystemErr R at javax.naming.InitialContext.lookup(InitialContext.java:428)
[10/22/18 9:50:10:472 BRST] 000001c0 SystemErr R at DAO.GerenciadorDeConexao.iniciar(GerenciadorDeConexao.java:30)

GerenciadorDeConexao 中的 30 行是:

dataSource = (DataSource) ctx.lookup("java:/comp/env/jdbc/dataquality_sqlServer");

网络空间:

inserir a descrição da imagem aqui

最佳答案

尝试查找 java:comp/env/jdbc/dataquality_sqlServer(而不是 java:/comp/env)

关于java - JNDI 连接适用于 Tomcat,但不适用于 Websphere,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52949543/

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