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 Servlets and the fly likes Servlets + database connection problem.. 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 » Java » Servlets
Bookmark "Servlets + database connection problem.." Watch "Servlets + database connection problem.." New topic
Author

Servlets + database connection problem..

Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Hi. Here's my situation.
I'm using DBConnectionBroker (www.javaexchange.com) as connection pooling for my web application. You initialize the broker with, say, 10 connections and everytime you need to perform database actions you getConnection, use it and then freeConnection. Simple.
The initialization of this broker variable takes place in the first servlet that hits the application. So what I do is initialize the broker and put this variable in the context so I can use it throughout my web application. And it is working fine. but...
it is working fine but only if you call the index page at the very beginning.
http://www.mysite.com.
if I type this a servlet (my InitController servlet) will initialize my broker, put this variable in the broker and it is ready to be used. BUT, if I type something like this at the very beginning:
http://www.mysite.com/aboutUs.jsp
if aboutUs.jsp needs connection to the database, it throws a null pointer exception because my broker has never been initialized.
So, what's the solution for this problem? Is someone here that is using javaexchange as connection pooling?
thankssss


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Kaustubh Patil
Ranch Hand

Joined: Aug 13, 2001
Posts: 164
I do not know what kind of application you have. But if the application needs some login, usually its better to initialise and put the connection pool in context during the login.
Otherwise if you are using an MVC architucture /Struts what you can do is, before calling any presentation (jsp) call an action class that initialises the connection pool, if already not. So every time a query is fired it will always find the connection .I think this is most simple and elegent solution.
Kaustubh.


Kaustubh. Mumbai, India.
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Hi, thnx for your response.
Originally posted by kaustubh patil:
I do not know what kind of application you have. But if the application needs some login, usually its better to initialise and put the connection pool in context during the login.

It doesn't need login.

Otherwise if you are using an MVC architucture /Struts what you can do is, before calling any presentation (jsp) call an action class that initialises the connection pool, if already not......

So, if I have www.mysite.com/aboutUs.jsp
and this page requires database connection, How do I call this action class before reaching aboutUs.jsp??
thanks
Steve Granton
Ranch Hand

Joined: Jan 13, 2002
Posts: 200
Hi,
If you need something to be initialised when the web application starts then you should do the following:
1) Place the code to be run at startup in the init() method of the servlet.
2) In the deployment descriptor (web.xml) set the servlet to load-at-startup and the init() method will be run before the container accepts any client requests.
Take a look at the Servlet Specification for further information on load-at-startup and the servlet life cycle
Cheers,
Steve
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
thank you guys.
<load-on-startup> was what I needed.
Premkumar N
Greenhorn

Joined: Apr 21, 2001
Posts: 22
Btw ... regarding cPool... why aren't u using java's connectionpool ?

Cheers,
Premkumar


<a href="mailto:pondyprem@yahoo.com" rel="nofollow">pondyprem@yahoo.com</a> <br />Sr.Programmer Analyst ( Bioinformatics)<br />Monsanto Entrprises
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Originally posted by Premkumar N:
Btw ... regarding cPool... why aren't u using java's connectionpool ?

Cheers,
Premkumar

Hi.. which one are you referring to?
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Are you using the Servlet 2.3 API? If so, you can use a ServletContextListener to initialize your connection pool. Override the contextInitialized() method and do the initialization there. This is the new way (the old way was to force servlet initialization via a load-on-startup tag) to initialize your application.


James Carman, President<br />Carman Consulting, Inc.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Servlets + database connection problem..
 
Similar Threads
Multiple Contexts, Single Project/App
How to install war application
keeping the directory path hidden
Basic question
DB connection from servlets