Can't access Servlet properties from plain java class...

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
 
A

Andy Flowers

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
Take a look at the DataSource interface, and connection pools, supported by J2EE.

See http://java.sun.com/j2ee/1.4/docs/tutorial/doc/Resources3.html#wp80235 for
some details.

Here's a snippet to point you to areas to search for

Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();
....
 
E

EdD

Thanks Andy, will take a look and try to obtain the necessary info from
that.

Cherrio,

Edd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top