0

I have a problem. I want to connect to database using JDBC, I have Tomcat server. For this I use connection pool.

According to Internet tutorials I've written: context.xml:

<?xml version="1.0" encoding="UTF-8"?>

<Context antiJARLocking="true" path="/Server" docBase="dbcp" debug="5"
         reloadable="true" crossContext="true">

    <Resource name="jdbc/TestDB" auth="Container"
          type="javax.sql.DataSource" removeAbandoned="true"
          removeAbandonedTimeout="30" maxActive="100"
          maxIdle="30" maxWait="10000" username="root"
          password="newpass"
          driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://localhost:3306/delta_server"/>

</Context>

web.xml :

<resource-ref>
      <description>DB Connection Pooling</description>
      <res-ref-name>jdbc/TestDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>

and I try to connect...

Connection conn=null;
DataSource ds;
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:comp/env");
ds = (DataSource) envContext.lookup("jdbc/TestDB");
conn = ds.getConnection();

But I get a mistake: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

What to do???

user1379574
  • 689
  • 4
  • 11
  • 23
  • Exact Tomcat version? The path attribute is not valid in a context.xml file. The debug attribute is almost certainly for an older version of Tomcat than you are using. Connecting to the database as root is poor security. Where (Servlet, JSP, Applet, etc.) is the code you are trying to access the database from located? – Mark Thomas Jun 22 '12 at 07:14
  • I have version 7.0.22. About connection as root - I agree, so it is temporary solution. Later I'm going to change it. The code is in class which is used by web service. – user1379574 Jun 22 '12 at 07:21
  • I' ve added the following lines of code Hashtable env = new Hashtable(); env.put("java.naming.factory.initial", "org.apache.naming.java.javaURLContextFactory"); env.put("java.naming.factory.url.pkgs","org.apache.naming"); env.put("java.naming.factory.url.pkgs.prefixes","org.apache.naming" ); env.put("java.naming.provider.url","org.apache.naming "); Context initContext = new InitialContext(env); but now there is anther problem .. javax.naming.NameNotFoundException: Name java:comp is not bound in this Context – user1379574 Jun 22 '12 at 10:14
  • Also, how do you launch Tomcat? – Christopher Schultz Jun 22 '12 at 20:22
  • How exactly is this code executed? Is this happening in an application created thread? It sounds like the code is executing outside of the web application's class loader. – Mark Thomas Jun 23 '12 at 08:12
  • the code is executed in a class used to access mysql database from web service...if I understand your question correctly.. – user1379574 Jun 27 '12 at 07:47
  • Full stack trace of the NoInitialContextException? – Christopher Schultz Jun 29 '12 at 11:43
  • Jul 02, 2012 11:29:50 AM com.db.SessionContextDAO getConnection SEVERE: null javax.naming.NameNotFoundException: Name java: is not bound in this Context Just this, this is the full stacktrace – user1379574 Jul 02 '12 at 08:31

0 Answers0