Java Glassfish jdbc/数据库查找失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21137208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:51:23  来源:igfitidea点击:

Glassfish jdbc/database lookup failed

javaservletsjdbcglassfishweb.xml

提问by GameDroids

I hope I am not asking a duplicate question just because I was unable to find an answer. I am getting this error:

我希望我不会因为找不到答案而问重复的问题。我收到此错误:

javax.naming.NamingException: Lookup failed for 'jdbc/osclassDB' in SerialContext

javax.naming.NamingException:在 SerialContext 中查找“jdbc/osclassDB”失败

This is what I did: I set up a JDBC Connection Pooland a JDBC Resourcepointing to that pool (both in Glassfish).

这就是我所做的:我设置了一个JDBC 连接池和一个指向该池的JDBC 资源(都在 Glassfish 中)。

Then I told my web.xmlthat there is a JDBC Resource:

然后我告诉我的web.xml有一个 JDBC 资源:

<resource-ref>
    <res-ref-name>jdbc/osclassDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>                
</resource-ref>

And then I tried using that resource in a Servlet:

然后我尝试在Servlet 中使用该资源

  Connection connection = null;

  try {        
     InitialContext initialContext = new InitialContext();
     //Context dbContext = (Context) initialContext.lookup("java:comp/env");

     DataSource dataSource = (DataSource) initialContext.lookup("jdbc/osclassDB");
     connection = dataSource.getConnection();

     if (connection == null) {
         throw new SQLException("Error establishing connection!");
     }
     // some queries here
  } 
  // catch and finally close connection

But when I call the Servletit throws me the NamingExceptionand tells me that the Lookup failed for 'jdbc/osclassDB' in SerialContext

但是当我调用Servlet它时,它抛出了我NamingException并告诉我Lookup failed for 'jdbc/osclassDB' in SerialContext

What am I doing wrong here?is it the web.xml? did I miss something? Thank you for your help!

我在这里做错了什么?是 web.xml 吗?我错过了什么?感谢您的帮助!

采纳答案by GameDroids

Solved the problem:

解决了问题:

1stby adding a sun-web.xmlthat links the resource referencefrom the web.xmlto an actual jndi-name(the one I setup on Glassfish). Normally, that shouldn't be necessary (says Oracle) but I did it anyways [Edit:as it turns out, it is really not necessary! ]

第一次加入了sun-web.xml中链接的资源引用web.xml中到实际JNDI名(一个我安装Glassfish上)。通常,这不应该是必要的(甲骨文说),但我还是这样做了[编辑:事实证明,这真的没有必要!]

2ndI left out the "jdbc". In the servlet, the web.xmland the sun-web.xmlit is now just called "osclassDB" (my resource name) instead of "jdbc/osclassDB"

第二,我遗漏了“ jdbc”。在servletweb.xmlsun-web.xml 中,它现在只是称为“osclassDB”(我的资源名称)而不是“jdbc/osclassDB”

now it looks like this:

现在它看起来像这样:

web.xml

网页.xml

<resource-ref>
    <res-ref-name>osclassDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>                
</resource-ref>

sun-web.xml

sun-web.xml

<resource-ref>
    <res-ref-name>osclassDB</res-ref-name>
    <jndi-name>osclassDB</jndi-name>  
</resource-ref> 

in the servlet

在 servlet 中

Context dbContext = (Context) initialContext.lookup("java:comp/env");
DataSource dataSource = (DataSource) dbContext.lookup("osclassDB");
connection = dataSource.getConnection();

[Edit:]the sun-web.xmlis really not necessary

[编辑:]sun-web.xml中真的没必要