Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
edd@mredd.co.uk - 12 Jun 2006 19:22 GMT
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
Andy Flowers - 12 Jun 2006 22:17 GMT
> Hi,
>
[quoted text clipped - 38 lines]
>
> 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();
...
EdD - 13 Jun 2006 19:48 GMT
Thanks Andy, will take a look and try to obtain the necessary info from
that.

Cherrio,

Edd


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.