• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JDBC Exception in JNDI lookup

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:



 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I even have the classes12.jar in the lib folder of my Tomcat installation.
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make sure the jar file for the connector is in your classpath. e.g. tomcat/lib/odbc...jar
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks to me like the DataSource configuration was accepted, but then later when it is trying to create the Connection object, it fails.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was my typo there....I checked the lookup names and they all point to MyDB. So that is not the cause.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any ideas??
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jothi, see if this thread helps https://coderanch.com/t/490267/Tomcat/t-get-connection-from-DataSource
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome! Now I see the value of DTDs...
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic