Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > servlet jdbc

Reply
Thread Tools

servlet jdbc

 
 
warth33@hotmail.com
Guest
Posts: n/a
 
      08-06-2007
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?

 
Reply With Quote
 
 
 
 
Manish Pandit
Guest
Posts: n/a
 
      08-06-2007
On Aug 6, 11:22 am, (E-Mail Removed) wrote:
> 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?


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

 
Reply With Quote
 
 
 
 
Manish Pandit
Guest
Posts: n/a
 
      08-06-2007
oops!

> public static DataSource dataSource = null;


private static DataSource dataSource = null;

> public static initialize()


public static void initialize()

-cheers,
Manish

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to parse the jdbc driver name from the jdbc .jar file Bruce Java 4 03-25-2006 12:01 PM
stand-alone JMS, other JDBC operations, and transactions ( ActiveMQ + JOTM + JDBC operations ) Jesus M. Salvo Jr. Java 2 02-11-2006 06:33 PM
oracle.jdbc.OracleDriver vs oracle.jdbc.driver.OracleDriver Betty Java 1 05-21-2005 05:15 PM
Re: jdbc help:sun.jdbc.odbc.JdbcOdbcDriver Keith Wansbrough Java 0 08-16-2004 07:31 PM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM



Advertisments