Glassfish JDBC/数据库查找失败 [英] Glassfish jdbc/database lookup failed

查看:117
本文介绍了Glassfish JDBC/数据库查找失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我不要因为找不到答案而问一个重复的问题. 我收到此错误:

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:在SerialContext中查找'jdbc/osclassDB'失败

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

This is what I did: I set up a JDBC Connection Pool and a JDBC Resource pointing to that pool (both in Glassfish).

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

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

<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>

然后我尝试在 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

但是当我打电话给Servlet时,它会抛出NamingException并告诉我Lookup failed for 'jdbc/osclassDB' in SerialContext

But when I call the Servlet it throws me the NamingException and tells me that the Lookup failed for 'jdbc/osclassDB' in SerialContext

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

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

推荐答案

解决了问题:

第一个,方法是添加一个 sun-web.xml ,该链接链接 web.xml 中的资源引用到实际的 jndi-name (我在Glassfish上设置的名称).通常,这不是必须的(Oracle说),但是无论如何我还是这样做了,

1st by adding a sun-web.xml that links the resource reference from the web.xml to an actual jndi-name (the one I setup on Glassfish). Normally, that shouldn't be necessary (says Oracle) but I did it anyways

第二个我遗漏了" jdbc ".在 servlet web.xml sun-web.xml 中,现在将其简称为"osclassDB"(我的资源名称)的"jdbc/osclassDB"

2nd I left out the "jdbc". In the servlet, the web.xml and the sun-web.xml it is now just called "osclassDB" (my resource name) instead of "jdbc/osclassDB"

现在看起来像这样:

web.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

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

在servlet中

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

确实没有必要 sun-web.xml

这篇关于Glassfish JDBC/数据库查找失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆