Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

database connection help

 
Sanjit Kumar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");

try{
//Class.forName("org.gjt.mm.mysql.Driver");
Class.forName("com.mysql.jdbc.Driver").newInstance();

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
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
UseAMeaningfulSubjectLine
and
UseRealWords
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.
-Ben
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have removed the "plzzzzz" from the topic title for you.
 
Philip Shanks
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sanjit,

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!
 
Sanjit Kumar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sanjit,

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic