This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes NullPointerException running web applications. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "NullPointerException running web applications." Watch "NullPointerException running web applications." New topic
Author

NullPointerException running web applications.

Qussay Najjar
Ranch Hand

Joined: Jan 18, 2008
Posts: 53
Hello everybody..
First of all, I'm sorry if this wasn't the place to post such topic, but honestly I didn't find the place to post it in, so feel free moving it please.
My problem is that I have a fully working system using Servlets + JSF + JSP + JDBC.
I deployed my project as .war file and running it through TomCat container on our server, it works fine for about two days or one day, then it stops, and gives NullPointerException whenever you request anything from it.
and for my surprise it just works fine after a restart.
I changed the server and the container to glassfish, but also having the same after two days, and again i restart the container and it works..
you know am doubting that the connection with db is stopping or something, because I create the connection using ServletContextListener at the loading of my application, so maybe it's getting idol or something.. !!
I don't know..
kindly inform me with some solutions for this..
thanks in advance for your help..


Qussay NAJJAR
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

Qussay,
The first step is to identify the problem. Do you have a specific line of code returning null? Can you post that line?

If you think the problem is at the connection level, do you have any logging (try/catch) around where you get the connection? Does it offer any output?

Thinking of suggestions before being sure of the problem is a bit premature.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Qussay Najjar
Ranch Hand

Joined: Jan 18, 2008
Posts: 53
Originally posted by Jeanne Boyarsky:

Thinking of suggestions before being sure of the problem is a bit premature.

I Know that, but can you tell me where to find the log files for the container glassfish, i can't find them, sure I'd find something helpful..
thanks..
Qussay Najjar
Ranch Hand

Joined: Jan 18, 2008
Posts: 53
Here's a part of my code, excuse me for not posting it from the first time:


With this code the connection is created the first thing the application loads, and in my servlets I do the next:



I tried surrounding these lines with try/catch(NullPointerException)
but the same problem is going on, it's not even catching the exception.

if you need more explanation please tell me..
I appreciate your time..
Qussay Najjar
Ranch Hand

Joined: Jan 18, 2008
Posts: 53
Hi guys,
I'm still stuck in this one, really confusing me..
this is new I've found in the log file of glassfish container..


I don't know why the listener is stopping, does it stop after a while ?? or the connection attribute is off ??
any clue would be appreciated really..
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi Qussay,

looks like your connection attribute is removed from the servlet context before your servlet gets destroyed.

You could write a class that implements javax.servlet.ServletContextAttributeListener to check that, and probably close your connection there.

I'm not sure why you want to keep your connection open for 2 days, why not return it to a connection pool? How many concurrent users do you have that use this one connection?

Herman
Qussay Najjar
Ranch Hand

Joined: Jan 18, 2008
Posts: 53
Originally posted by Herman Scheltinga:
hi Qussay,

looks like your connection attribute is removed from the servlet context before your servlet gets destroyed.

You could write a class that implements javax.servlet.ServletContextAttributeListener to check that, and probably close your connection there.

Herman


Thanks for reply,
Will honestly I learnt this way of creating the connection and closing it from a respected book, but as you said the attribute is being removed before closing it, and that's what leading to NullPointerException.


I'm not sure why you want to keep your connection open for 2 days, why not return it to a connection pool? How many concurrent users do you have that use this one connection?


will I have about 350 to 400 people using this System so they might be using it at the same time, or differently (no one knows), so I need to keep this connection always (as I think, for now..)
but can you please explain me what do you exactly mean by "return it to a connection pool", honestly don't know it..like is it another way for creating and destroying the connection ??..
your help is really appreciated ,, thanks..

Qussay >>
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi Qussay,



One connection will give problems if one user just did an update (but has not yet committed), and another user wants to rollback because of an exception. This will also rollback the update of the first user (if you use only one connection).

It's better to give each request a connection to the database (if needed), and to close the connection as soon as possible.
Because getting a connection takes some time, it's better to get a connection from a connection pool. The connection pool has some real connections to the database, the application gets connections from the pool.

There's basically 2 ways:
-the application maintains it's own pool (see http://commons.apache.org/dbcp/)
-the application uses a connection pool from the server (that's the best way)

There's a lot of articles about "Tomcat connection pooling", here's just one http://www.onjava.com/pub/a/onjava/2006/04/19/database-connection-pooling-with-tomcat.html?page=1

If you need more help, just let us know.

Herman
 
Consider Paul's rocket mass heater.
 
subject: NullPointerException running web applications.
 
Similar Threads
JSP, Apache, tomcat problem
ResultSet needs connection?
Spring and DAO
JSP, Apache, tomcat problem
how to deploy a webapplication in tomcat server