简体   繁体   English

为Ingres数据库创建JNDI连接

[英]Create JNDI Connection for Ingres Database

I have a Java project and I've been trying to create a JNDI connection for my Ingres database but have been unsuccessful. 我有一个Java项目,我一直在尝试为我的Ingres数据库创建一个JNDI连接但是没有成功。 I'm not sure if there is something specific to ingres that needs to be included but after quite a bit of research I haven't been able to get things to work. 我不确定是否需要包含某些特定于ingres的内容,但经过相当多的研究后,我无法让事情发挥作用。

In my project I have my datasource info in the web.xml file and the context.xml 在我的项目中,我在web.xml文件和context.xml中有我的数据源信息

context.xml has the following info context.xml具有以下信息

<Context>
<Resource name="jdbc/myDB" auth="Container" type="javax.sql.Datasource"   
  username="myUser" password="password" driverClassName="com.ingres.jdbc.IngresDriver"
  url="databaseURL" maxActive="8" maxIdle="4" maxWait="100" />

 </Context>

My web.xml has the following info 我的web.xml有以下信息

<web-app>
<resource-ref>
    <description>Project Descrip</description>
    <res-ref-name>jdbc/myDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>
</web-app>

In my java code I'm trying to get my connection using the following four lines 在我的java代码中,我试图使用以下四行来获取我的连接

Context initContext = new InitialContext();
Context envContext = (Context) initContent.lookup("java:comp/env");
Datasource ds = (DataSource) envContext.lookup("jdbc/myDB");

return ds.getConnection();

After the third line is executed I get an exception that says: NamingException - Cannot create resource instance 执行第三行后,我得到一个异常,说明:NamingException - 无法创建资源实例

I have found dozens of posts with this same exception and have tried the suggested solutions with no luck. 我找到了几十个有相同例外的帖子,并尝试了建议的解决方案,没有运气。 I'm using a Tomcat 7 server and have made sure to include the necessary ingres jar (iijdbc.jar) to my WEB-INF/lib folder and to my tomcat lib folder. 我正在使用Tomcat 7服务器,并确保将必要的ingres jar(iijdbc.jar)包含到我的WEB-INF / lib文件夹和我的tomcat lib文件夹中。

Any help or suggestions would be greatly appreciated 任何帮助或建议将不胜感激

I don't know much about Ingres, but if attempting to make a datasource is similar to JBOSS and DB2 or MySQL. 我对Ingres知之甚少,但如果尝试创建数据源类似于JBOSS和DB2或MySQL。 I noticed that in your WEB.XML you define your resource-ref, but you don't mention the anything about the servlet parameters. 我注意到你在WEB.XML中定义了你的resource-ref,但你没有提到有关servlet参数的任何内容。

<servlet>
    <description>Servlet Description</description>
    <display-name>MyServlet</display-name>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>servlet.MyServlet</servlet-class>

    <init-param>
        <param-name>MyDB</param-name>
        <param-value>java:comp/env/jdbc/MyDB</param-value>
     </init-param>

     <load-on-startup>1</load-on-startup>
</servlet>

Then you would need to code in your sevlet: 然后你需要在你的sevlet中编码:

InitialContext initialContext = new InitialContext();
DataSource dataSource = (DataSource)initialContext.lookup(this.getInitParameter("MyDB"));

Hope this helps. 希望这可以帮助。

Can you try adding global="jdbc/myDB" attribute as in your Resource tag as follows: 您可以尝试在Resource标记中添加global="jdbc/myDB"属性,如下所示:

<Context>
  <Resource name="jdbc/myDB" auth="Container" type="javax.sql.Datasource"   
    username="myUser" password="password" driverClassName="com.ingres.jdbc.IngresDriver"
    url="databaseURL" maxActive="8" maxIdle="4" maxWait="100" global="jdbc/myDB"/>
</Context>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM