wood burning stoves 2.0
The moose likes Servlets and the fly likes database connection help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "database connection help" Watch "database connection help" New topic

database connection help

Sanjit Kumar
Ranch Hand

Joined: Dec 04, 2006
Posts: 35
Hi to everyone,

i am having following problem.please help me out.

I am retrieving context-param values in contextInitialized() method.
Those values are : databaseUrl(bdUrl), database driver, userName, passWord

Then created one Connection instance named "con".

My question is : Will the connection "con" be alive for all the resources in the corresponding web application.
The code for the contextInitialized given below:

private ServletContext context = null;
Connection con = null;

public void contextInitialized(ServletContextEvent sce){
this.context = sce.getServletContext();

String driver = context.getInitParameter("driver");
String url = context.getInitParameter("dbUrl");
String user = context.getInitParameter("login");
String pass = context.getInitParameter("password");


con = DriverManager.getConnection(url, user, pass);

}catch(Exception ex)
System.out.println("Error in connection :"+ex.toString());

[ February 07, 2007: Message edited by: Bear Bibeault ]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Sanjit Kumar,
Welcome to JavaRanch!

In an effort to help you get the most from our forums, we've compiled a
list of tips for asking questions here. You can find the list in our
FAQ section here.
In particular please see:
Abbreviations such as "u" or "ur" in
place of "you" and "you are" or "you're" confound language translation software making
it hard for our non-English speaking members to read your posts.
"plz" is not a word in the English language.

Again, welcome to JavaRanch and good luck with your question.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

I have removed the "plzzzzz" from the topic title for you.

[Asking smart questions] [About Bear] [Books by Bear]
Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189

I think that a much better approach is to let your servlet container handle DB connection pooling for you. Compared with the approach you outlined, you should see improved response times, better memory management, and you won't have to worry about where you can and cannot access your connections.

You didn't specify what container you are using (Tomcat, Jetty, etc), so I can't give you any specifics, but you can get started by googling (sorry Bear, that isn't a word either!) for "{name of your container} jdbc pool datasource". Your JDBC driver (com.mysql.jdbc.Driver, if you are using a recent version) has supported this for several releases.

The trade-off is that when you deploy your application to the server, you will have to do a bit of (relatively simple) server configuration, and you will have to make your JDBC drivers available to the server rather than to your web app.

Hope this helps!

Philip Shanks, SCJP - Castro Valley, CA
My boss never outsources or has lay-offs, and He's always hiring. I work for Jesus! Prepare your resume!
Sanjit Kumar
Ranch Hand

Joined: Dec 04, 2006
Posts: 35
Thanks Philip for clearing my doubt.
Please guide me how to implement connection pooling in my application and what additional jar file i will have to include to make it working.

I am using following tools and software:
Application Server : jboss-4.0.5.GA
Editor : Eclipse-3.2.1
Java SDK : jre1.6.0
DataBase : MySQL

All are open source softwares.
Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189

I do not have the time to take you through it step-by-step, and I think you can get all that you need with a bit of googling. I strongly recommend that you read your product literature, so that is the first of a few links that I recommend. If you are new to this, "RTFM" is an acronym that you better become familiar with.

Getting Started with JBoss 4.0: Chapter 8. Using other Databases

Configuring JBoss 4.0 JDBC Connectivity

Using Multiple Databases with JBoss

Good luck.
I agree. Here's the link: http://aspose.com/file-tools
subject: database connection help
It's not a secret anymore!