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 / August 2007

Tip: Looking for answers? Try searching our database.

servlet jdbc

Thread view: 
warth33@hotmail.com - 06 Aug 2007 19:22 GMT
Hello

I have a web app that is composed by a lot of pages, page1, page2,
page3. Everypage is a servlet. The servlet will eventually redirect to
a jsp.

I am using jndi datasource, thats the first time. I managed
configuring server.xml, web.xml.

Now, in the servlet page1, in its init method, I have code like this:

Context env = (Context) new InitialContext().lookup("java:comp/env");
pool = (DataSource) env.lookup("jdbc/TestDB");

Later, in the doget, I do database operations. So it works so long.

But I am a little bit concerned about the other servlets. How can
servlet named page2, servlet named page3, and so on access the
database? Do they also need to have its own init method, and they also
need the code I showed above? maybe it has to be this way, but it
seems like you do the same job several times.

Isnt there a way to make all the webapp share the Connection pooling
that was created by a single servlet start?
Manish Pandit - 06 Aug 2007 19:51 GMT
On Aug 6, 11:22 am, wart...@hotmail.com wrote:
> Hello
>
[quoted text clipped - 20 lines]
> Isnt there a way to make all the webapp share the Connection pooling
> that was created by a single servlet start?

One solution could be to have something like a ConfigurationLoader
which can create the datasource/pool and keep it as a static variable
accessible to all servlets. The configuration loader's initialization
can be called via the contextlistener's contextInitialized() method.
You can put any global initialization stuff in this class as well.

Something like:

public class ApplicationConfiguration{

  public static DataSource dataSource = null;

  public static initialize(){
     dataSource = ...
   }

  public static void getDataSource(){
    if(dataSource==null) initialize();
    return dataSource;
  }

}

ServletContextListener:

public void contextInitialized(ServletContextEvent event){
  ApplicationConfiguration.initialize();
}

-cheers,
Manish
Manish Pandit - 06 Aug 2007 19:53 GMT
oops!

>    public static DataSource dataSource = null;

private static DataSource dataSource = null;

>    public static initialize()

public static void initialize()

-cheers,
Manish


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



©2009 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.