trouble with JNDI Data Source in Tomcat

10,667

Solution 1

The database url seems broken to me.

try:

jdbc:oracle:thin:@theserver:1521/mydb

Solution 2

"The Network Adapter could not establish the connection"

This is your clue. It cannot reach the database. Check your server and port are correct, check they are reachable from your machine.

Solution 3

I'm responding to comments in Sebastian answer.

When I see org.apache.commons.dbcp, it means Tomcat is using the built-in Apache Commons Database Connection Pooling library instead of the library in the Oracle JDBC driver.

The Oracle JDBC driver not being in the common directory is usually the first problem I try to solve. It appears this is not your problem.

Second, when the application starts, if there was a problem creating the JNDI datasource Tomcat may use the Commons library. This may be your case because of your wrong database URL. Correcting context.xml or the copied/renamed file in the conf/Catalina/localhost directory may not fix the problem without stopping and restarting Tomcat. So, I'd recommend stopping and restarting Tomcat and seeing if this corrects the problem.

One final note, the link you provide is for setting up the Common's library. It took me a while to figure this out. Below is an example of one of my JNDI Oracle datasource definitions.

  <Resource auth="Container" name="jdbc/mydb" 
    type="oracle.jdbc.xa.client.OracleXADataSource"
    driverClassName="oracle.jdbc.driver.OracleDriver"
    factory="oracle.jdbc.pool.OracleDataSourceFactory"
    url="jdbc:oracle:thin:@theserver:1521:mydb" 
    connectionCachingEnabled="true"
    connectionCacheProperties="{InactivityTimeout=1800,PropertyCheckInterval=300,MaxStatementsLimit=125,ValidateConnection=true}"
    implicitCachingEnabled="true"/>

I am not sure if the type matters in your case and I believe the user and password are still attributes of the Resource tag. What I want to point out is the attribute connectionCacheProperties. Here is where specify you specify the maximum and minimum connections. Go to Connection Cache Properties for more info on this attribute.

Hope this helps.

Share:
10,667
mcgyver5
Author by

mcgyver5

Updated on June 26, 2022

Comments

  • mcgyver5
    mcgyver5 almost 2 years

    I've been having a hard time making a JNDI data source work. Following instructions at http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html I'm connecting to oracle with Tomcat5.5 I can connect fine if I use straight JDBC connection in code.

    Here is what I have: in my META-INF/context.xml:

    <Resource name="jdbc/mydb" auth="Container"
              type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
              url="jdbc:oracle:thin:theserver:1521/mydb"
              username="user" password="password" maxActive="20" maxIdle="10"
    />
    

    here is what is in web.xml:

    <resource-ref>
      <description>please work</description>
      <res-ref-name>jdbc/mydb</res-ref-name>
        <res-type>
        javax.sql.DataSource
        </res-type>
        <res-auth>Container</res-auth>
      </resource-ref>
    

    here is code:

       Connection conn = null;
        try{
        InitialContext ic = new InitialContext();
        DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/mydb");
        conn = ds.getConnection();
        } catch ....... etc.
    

    I've tried many different configurations and started a new, simple project to ensure that no extra jar files conflicted or anything like that, but .

    can anyone see anything that doesn't look right?

    the error on the server indicates a NullPointerException when I attempt to use the conn object. excuse me, it first offers: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Io exception: The Network Adapter could not establish the connection)

  • mcgyver5
    mcgyver5 over 14 years
    thanks. this was the problem locally. However, when I deploy to server, I get a different issue, which I'll specify below
  • mcgyver5
    mcgyver5 over 14 years
    The problem on the server is now org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null' some other threads indicate I need to set path and docBase in my context element? Can't make that work.
  • nos
    nos over 14 years
    Where is the oracle driver jar file placed on the server ?
  • mcgyver5
    mcgyver5 over 14 years
    nos, it is in TOMCAT_HOME/common/lib. huh. it is also in TOMCAT_HOME/webapps/MyTestApp/WEB-INF/lib
  • mcgyver5
    mcgyver5 over 14 years
    wow. what a pain. Judging from the thread theserverside.com/discussions/thread.tss?thread_id=25459 this kind of thing has been causing pain for 5 years now. My issue was that even though I had changed context.xml several times, the server stored old context data so that the resource name was not correct. it is in TOMCAT_HOME/conf/Catalina/localhost/XXXXX.xml where XXXXX is the name of my application
  • Sylar
    Sylar over 14 years
    Right, Tomcat copies your META-INF/context.xml from your .war file to /catalina/yourhost/yourapp.xml at deployment time and does not overwrite it if it is already there, it keeps the old file. So for clean undeployment you have to delete the .war file, the exploded .war file directory, and the context configuration file from /catalina/yourhost/. I think when you undeploy the application from within the tomcat manager application, all these steps are done for you automatically.