| Author |
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"); 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
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: 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
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56157
|
|
|
I have removed the "plzzzzz" from the topic title for you.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Philip Shanks
Ranch Hand
Joined: Oct 15, 2002
Posts: 189
|
|
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!
|
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
|
|
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.
|
 |
 |
|
|
subject: database connection help
|
|
|