aspose file tools*
The moose likes Tomcat and the fly likes JDBC Exception in JNDI lookup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "JDBC Exception in JNDI lookup" Watch "JDBC Exception in JNDI lookup" New topic
Author

JDBC Exception in JNDI lookup

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9242
    
    1

Guys,

I have a Datasource configured in the tomcat config.xml and the appropriate resource-ref elements in the web.xml of my web app. When I tried to run my app by trying to connect to the database, I get the following error:



Any ideas as to what is going on??

Here is my context.xml:



My web.xml snippet:




SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9242
    
    1

I even have the classes12.jar in the lib folder of my Tomcat installation.
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 475
    
    3
Just an idea, haven't tried this out myself: Can you change driverClassName to "oracle.jdbc.OracleDriver" and try. The FAQ mentions that "oracle.jdbc.driver.*" is a deprecated package from a couple of years now...
Misha Ver
Ranch Hand

Joined: Mar 03, 2008
Posts: 470
Jothi Shankar Kumar wrote:I even have the classes12.jar in the lib folder of my Tomcat installation.


Which Tomcat version you are using? Is it on Window, Linux?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15624
    
  15

Is this a duplicate? I responded to a post just like this very recently, and my recommendation was to check the URL very carefully.

And, for the record, an old Oracle driver class is probably OK here, since classes12.jar is an old driver jar. It was superseded in Oracle 9i.


Customer surveys are for companies who didn't pay proper attention to begin with.
Michael Peremsky
Greenhorn

Joined: Nov 07, 2008
Posts: 10
Make sure the jar file for the connector is in your classpath. e.g. tomcat/lib/odbc...jar
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9242
    
    1

Tim Holloway wrote:Is this a duplicate? I responded to a post just like this very recently, and my recommendation was to check the URL very carefully.

And, for the record, an old Oracle driver class is probably OK here, since classes12.jar is an old driver jar. It was superseded in Oracle 9i.


I saw your other post and I guess the database url what I have configured is correct. Which driver jar file should I have to use? classes12.jar or ojdbc.jar?? I'm using Oracle 10g and Tomcat 5.5.28 and JDK 1.4 as my environment.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9242
    
    1

Karthik Shiraly wrote:Just an idea, haven't tried this out myself: Can you change driverClassName to "oracle.jdbc.OracleDriver" and try. The FAQ mentions that "oracle.jdbc.driver.*" is a deprecated package from a couple of years now...


Even tried this but still the same exception.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9242
    
    1

It looks to me like the DataSource configuration was accepted, but then later when it is trying to create the Connection object, it fails.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9310
    
109

It's a NullPointerException while creating a connection from datasource:



As Tim said, it looks like some issue with the URL and driver class being used.

de.dpwn.fcap.core.service.user.impl.UserServiceImpl.sayHallo(UserServiceImpl.java:29)


Post the code which is doing the lookup of the datasource and obtaining the connection.

[My Blog] [JavaRanch Journal]
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9242
    
    1

I had a look at the BasicDataSource.java and like you just said, it looks like the url is null as printed in the exception.

This is how I do the lookup for the DataSource:

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9310
    
109

Jothi Shankar Kumar wrote:


You are looking up a completely different datasource and not the one whose configurations you posted:





You should be using :

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9242
    
    1

That was my typo there....I checked the lookup names and they all point to MyDB. So that is not the cause.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9242
    
    1

Any ideas??
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9242
    
    1

I tried the same sample using plain JDBC and it worked. I simply cannot understand why it would not work with Tomcat DBCP??

Tried the following...


Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9242
    
    1

Do we have to configure this in the server.xml or in the context.xml??? This is really getting annoying to make DBCP configured in Tomcat.
Misha Ver
Ranch Hand

Joined: Mar 03, 2008
Posts: 470
Jothi Shankar Kumar wrote:Do we have to configure this in the server.xml or in the context.xml??? This is really getting annoying to make DBCP configured in Tomcat.


For Tomcat 5, unlike Tomcat 4.x, it is NOT recommended to place <Context> elements directly in the server.xml file. This is because it makes modifying the Context configuration more invasive since the main conf/server.xml file cannot be reloaded without restarting Tomcat.


http://tomcat.apache.org/tomcat-5.5-doc/config/context.html
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9310
    
109

Jothi, see if this thread helps http://www.coderanch.com/t/490267/Tomcat/t-get-connection-from-DataSource
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9242
    
    1

I do not understand the cause for this exception. It says the root cause is a NullPointerException...



Any idea as to where I could find the ...23 more???

And below is the method that I'm executing:

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15624
    
  15

Now do you see why I hate this message?

Just a moment... Just a moment...

WHY is the stack tracking indicating a NullPointerException in the jdbc-odbc bridge driver??? This was supposed to be an native Oracle connection!!!
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 475
    
    3
Hi Jothi,

Please change <resource> to <Resource> in your context.xml and check if it works. I get this error when I use <resource> because it's not a valid tag in context.xml, but <Resource> is. I think the JNDI lookup works because it's created out of the web.xml <resource-ref>, but then Tomcat simply ignores the erroneous <resource> tag and is unable to find the associated Resource.

Cheers
Karthik
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9242
    
    1

Karthik Shiraly wrote:Hi Jothi,

Please change <resource> to <Resource> in your context.xml and check if it works. I get this error when I use <resource> because it's not a valid tag in context.xml, but <Resource> is. I think the JNDI lookup works because it's created out of the web.xml <resource-ref>, but then Tomcat simply ignores the erroneous <resource> tag and is unable to find the associated Resource.

Cheers
Karthik


Man... Thank you really very very much. It was my mistake to have it small (like <resource>) and I changed it to (<Resource>) and it worked.
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 475
    
    3
You're welcome! Now I see the value of DTDs...
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9310
    
109

Jothi Shankar Kumar wrote:

Man... Thank you really very very much. It was my mistake to have it small (like <resource>) and I changed it to (<Resource>) and it worked.


That is really weird for 2 reasons:

1) The xml validation should have failed. But I guess, that's disabled by default?
2) With that wrongly configured "resource", Tomcat was able to return a DataSource instance to the code, but wasn't able to create a connection on a call to getConnection(). Instead I would have expected the lookup of the datasource to fail.

Anyway, good to know that it's resolved.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9242
    
    1

Jaikiran Pai wrote:
Jothi Shankar Kumar wrote:

Man... Thank you really very very much. It was my mistake to have it small (like <resource>) and I changed it to (<Resource>) and it worked.


That is really weird for 2 reasons:

1) The xml validation should have failed. But I guess, that's disabled by default?
2) With that wrongly configured "resource", Tomcat was able to return a DataSource instance to the code, but wasn't able to create a connection on a call to getConnection(). Instead I would have expected the lookup of the datasource to fail.

Anyway, good to know that it's resolved.


Do you know how to get this xml validation enabled??
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9242
    
    1

So I'm just putting up the complete list of items that one should do to get JDBC connection pooling working in Tomcat 5.5.28, Oracle 10g, JDK 1.4

Under "apache-tomcat-5.5.28\conf", edit the context.xml to look lie this as below:



Next in the web.xml of your project (for me it is MYPROJECT) add the following lines:



Next, in your code, do the following to get a reference to the connection pooled Connection object:


Hope it works for everyone else who would require Connection Pooling configured in Tomcat.

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9310
    
109

Jothi Shankar Kumar wrote:
Do you know how to get this xml validation enabled??


There's a xmlValidation attribute in the Host element of server.xml:


which is by default set to false. Try setting those to true.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15624
    
  15

I don't think that will help. I believe it's for indicating validation of the web.xml files in WARs.

The problem with validating server.xml is that not all of the elements in server.xml are rigidly defined. Notable offenders include option attributes for the database connection pool definition.

DTDs are all-or-nothing, which is why things like server.xml and Ant build files don't validate (and have no formal DTDs). XML schemas were designed (among other reasons) to help out in this regard, but unless I missed something, there's no xsd for Tomcat's server.xml.

Failing machine validation, I'm afraid you're just going to have to eyeball it.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9310
    
109

Tim Holloway wrote:I don't think that will help. I believe it's for indicating validation of the web.xml files in WARs.



I see. I wasn't aware of that.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9242
    
    1

Obtaining the Connection from the JNDI tree is now fine. I have another question here...What do I do with the obtained Connections after I have performed my work??? Do I have to explicitly close them by myself??
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15624
    
  15

Jothi Shankar Kumar wrote:Obtaining the Connection from the JNDI tree is now fine. I have another question here...What do I do with the obtained Connections after I have performed my work??? Do I have to explicitly close them by myself??


Most definitely! If you don't, they'll leak.

The actual Connection is wrapped by the pool manager. The wrapping version of the connection returns the connection to the pool, so if you don't explicitly close, you'll exhaust the pool.

The Connection itself isn't actually closed, since the whole point of a Connection Pool is to keep connections ready to use with minimal overhead.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9242
    
    1

So the Connection object that I obtain by calling dataSource.getConnection() actually returns me a kind of a proxy to the actual Connection object. How will the original Connection object be notified that I have closed the proxy Connection object???
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15624
    
  15

Jothi Shankar Kumar wrote:So the Connection object that I obtain by calling dataSource.getConnection() actually returns me a kind of a proxy to the actual Connection object. How will the original Connection object be notified that I have closed the proxy Connection object???


It won't, and it won't care. The Connection that's being proxied will remain open and will be handed out again and again. Which makes it important that you set your connections to known states before using them. You might be able to depend on the Connection pool resetting some values, but unless you've specifically declared options, don't count on it. Especially for auto-commit.

I think, however, that an attempt to close a pooled connection while it's actively in use is going to throw an exception. It should, at any rate.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC Exception in JNDI lookup
 
Similar Threads
Cannot load JDBC driver for mysql in Servlet
A question about creating a Connection pool that uses Oracle JDBC
Cannot create JDBC driver of class '' for connect URL 'null'
resouce config problem
Connection pooling issue in Apache 6.0