Question

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)

Was it helpful?

Solution

The database url seems broken to me.

try:

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

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top