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.
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:
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?
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.
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??
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