This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connection class related Query

 
Manish Sridharan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys !

I have a Problem related to Connection class object.

I have created the separate class for connection . this class uses the data source connection. here is the description of the class


public class DBConnect
{

// public static ServletContext Context=null;
public static InitialContext initctx=null;
public static Context ctx=null;
public static DataSource ds=null;
public static Connection con=null;
// public
/** Creates a new instance of DBConnect */
static
{
try{
initctx= new InitialContext() ;
ctx=(Context)initctx.lookup("java:comp/env");
ds=(DataSource)ctx.lookup("jdbc/TestDB");
con=ds.getConnection();
}catch(Exception dbc){System.out.println("DBConnect Exception generated : "+dbc);}
System.out.println("Connection is Established");
}

}

Now The Problem is I am using 4-5 servlets , which use this con object to create statement object. and i close this con object in destroy method of the each servlet i m using.

So please tell me it is the correct way of doing it . creating the con object in the static form is right way of doing it .

Please help me in this regard
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will be in trouble soon in a minute, as you are using a single connection. Simultaneous request are going to be handled by your container. Hence having a single connection would definitely be painful in coming hours when there is several parallel users connected.

You can get the connection when required or if you want to perform well then please read about connection pooling. If you are not using Tomcat, very unlikely, see how your application container provide this pooling mechanism. For Tomcat see:

DBCP docs for Tomcat 5.5.x.

DBCP docs for Tomcat 5.0.x.

cheers.
 
Harish Madhavan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suggest to have an utility class which creates connection on request and which will be released by the caller once the work is done.Putting close in destroy method is a very design.

Harish
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly, you could just do the following:

public class DBConnect
{

public static Connection getConnection() {
ServletContext Context=null;
InitialContext initctx=null;
Context ctx=null;
DataSource ds=null;
Connection con=null;
try{
initctx= new InitialContext() ;
ctx=(Context)initctx.lookup("java:comp/env");
ds=(DataSource)ctx.lookup("jdbc/TestDB");
con=ds.getConnection();
}catch(Exception dbc){System.out.println("DBConnect Exception generated : "+dbc);}
System.out.println("Connection is Established");
return con;
}

}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic