简体   繁体   中英

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

In my project I have my datasource info in the web.xml file and the context.xml

context.xml has the following info

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

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

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.

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. I noticed that in your WEB.XML you define your resource-ref, but you don't mention the anything about the servlet parameters.

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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