aspose file tools*
The moose likes Servlets and the fly likes Connection class related Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Connection class related Query" Watch "Connection class related Query" New topic
Author

Connection class related Query

Manish Sridharan
Ranch Hand

Joined: Jul 19, 2005
Posts: 64
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


Manish S.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Dec 10, 2003
Posts: 17
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

Joined: Dec 13, 2004
Posts: 429
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;
}

}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connection class related Query
 
Similar Threads
CONNECTION POOL in WAS 3.5
WebSphere 3.5 Connectio pool ??
Unable to create Data Source in Tomcat
connection object
Name is not bound in this context