JNDI Connection在Tomcat上有效,但在Websphere上无效

时间:2018-10-23 12:50:33

标签: java sql-server tomcat websphere jndi

我使用与SQL Server的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>

连接类-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:

inserir a descrição da imagem aqui

1 个答案:

答案 0 :(得分:2)

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