• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Connection Object

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Connection

I am maintaining a project on servlet database is mysql.

I have a connection instance variable con.

I am using the same connection in many methods. (The code is below just typed one of the methods for explanation)

Is this the correct way to use the connection object in all the methods

or should i have seperate connections in all the methods ?

I have observed that the page does not get properly loaded if i use the instance variable con in all the methods.


But if i open and close connections in all the methods there will be lot of overhead so can anyone suggest a better way...

public class DCRDoctorData extends HttpServlet
{

Connection con;

public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
con = ConnectionHandler.getConnection(dbname);

add1();

}

private void add1()
{
Statement stmt = con.createStatement(); // using the instance variable

}
}
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You probably want to use Connection Pooling, get a connection everytime you need it and close it in finaly block when you are through using it.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic