E
edd
Hi,
I hope you'll excuse the newbie question, I have recently started
learning to create JSPs and Servlets. I'm generally getting along quite
well but have run in to a problem.
I have a DBConnectionManager class, which I use to return a
java.sql.Connection object, which provides access to a DB.
I instantiate the DBConnectionManager class when the web app boots up
using a Web Application listener, this then returns the instance of the
java.sql.Connection which is then added to the ServletContext so that
other servlets can access it during subsequent requests.
I would like to be able to access the java.sql.Connection instance from
plain old java classes which run within the same application, however I
don't seem to be able to access it unless I am in an actual Servlet. My
initial thought was that I could just instantiate another instance of
the DBConnectionManager class but all the initialisation parameters are
also in the ServletContext so they are also inaccessible from plain old
classes (e.g. non servlets).
I suspect that I have misunderstood the servlet paradigm somehow and I
am using a silly design. I realise that I could just turn everything in
to a Servlet but didn't think that was a very clever or appropriate
solution.
So if anyone could point out where I am going wrong it would be greatly
appreciated. I guess the answer I'm really looking for is a good basic
design for a DB connection in a web application. To be honest I'm not
even sure if it's a good idea to share one instance to all servlets as
I imagine this would create concurrency bottlenecks when multiple
sessions are trying to access the DB at once. So any thoughts/ design
patterns you have will be appreciated.
For Info: I am running:
Sun Java System Application Server 9 Beta (build b32e)
JDK 1.6.0-beta2
Netbeans 5.5 Preview
Many thanks,
Edd
I hope you'll excuse the newbie question, I have recently started
learning to create JSPs and Servlets. I'm generally getting along quite
well but have run in to a problem.
I have a DBConnectionManager class, which I use to return a
java.sql.Connection object, which provides access to a DB.
I instantiate the DBConnectionManager class when the web app boots up
using a Web Application listener, this then returns the instance of the
java.sql.Connection which is then added to the ServletContext so that
other servlets can access it during subsequent requests.
I would like to be able to access the java.sql.Connection instance from
plain old java classes which run within the same application, however I
don't seem to be able to access it unless I am in an actual Servlet. My
initial thought was that I could just instantiate another instance of
the DBConnectionManager class but all the initialisation parameters are
also in the ServletContext so they are also inaccessible from plain old
classes (e.g. non servlets).
I suspect that I have misunderstood the servlet paradigm somehow and I
am using a silly design. I realise that I could just turn everything in
to a Servlet but didn't think that was a very clever or appropriate
solution.
So if anyone could point out where I am going wrong it would be greatly
appreciated. I guess the answer I'm really looking for is a good basic
design for a DB connection in a web application. To be honest I'm not
even sure if it's a good idea to share one instance to all servlets as
I imagine this would create concurrency bottlenecks when multiple
sessions are trying to access the DB at once. So any thoughts/ design
patterns you have will be appreciated.
For Info: I am running:
Sun Java System Application Server 9 Beta (build b32e)
JDK 1.6.0-beta2
Netbeans 5.5 Preview
Many thanks,
Edd