JNDI 连接适用于 Tomcat,但不适用于 Websphere
JNDI Connection works on Tomcat, but doesn't work on Websphere
我使用与 SQL 服务器的 JNDI 连接来连接我的数据库,直到今天我总是 运行 我的应用程序在本地机器上使用 tomcat 应用程序服务器:
Context.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>
连接 Class - 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");
Websphere:
尝试查找 java:comp/env/jdbc/dataquality_sqlServer(而不是 java:/comp/env)
我使用与 SQL 服务器的 JNDI 连接来连接我的数据库,直到今天我总是 运行 我的应用程序在本地机器上使用 tomcat 应用程序服务器:
Context.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>
连接 Class - 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");
Websphere:
尝试查找 java:comp/env/jdbc/dataquality_sqlServer(而不是 java:/comp/env)