• Post Reply Bookmark Topic Watch Topic
  • New Topic

Opening and closing Database Connections in a servlet .  RSS feed

 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello ,

I have a servlet that connects to Database and return results based on search parameters and displays them in a View(JSP) .In this servlet
i had written a finally method which closes the conn , stmt and resultset.

After seeing the results the user might again perform a search operation again calls the servlet which does the same thing.

Now my Question is that Will it not be a performance issue opening and closing connections for each operation??

I don't know whether my question is a valid one or not also??

Any help can i get with this.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's one of the reasons why multi-threaded environments (like web apps) use DB connection pools.
 
Anurag Blore
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer,

What is the best way to open/close the connection If I am using only a standalone java program? Its not used in either web/application server.

Thanks,
anurag
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What is the best way to open/close the connection If I am using only a standalone java program? Its not used in either web/application server.

You could keep the connection open and have it time out after a certain period of inactivity.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the application is inherently multi-threaded it may still make sense to use a connection pool. Most desktop apps don't have more than one DB access going on at the same time, though.
 
Tim Holloway
Bartender
Posts: 18715
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a simple standalone Java app, just invoke the classic JDBC open/close methods directly.

There are some plug-in database connection pools available. The default one supplied with Tomcat these data is the Apache "dbcp" pool facility. You can easily use dbcp in any standalone Java app as well - in fact one of my current projects doe so. A connection pool is useful for standalone apps that are long-running and/or extensively multi-threaded. Which is basically what Tomcat itself is.

There is a significant overhead to establishing a network link to a database and building up the Java support objects, so in general either use pooled connections or at least don't close/reopen connections.

I also recommend using a framework such as Spring to assist you. It provides a good decoupling mechanism and its various persistent data mechanisms are constructed to help you avoid problems like Connection leakage by offloading a lot of the "grunt code" onto itself.
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great explanation from you experts .

Thank you very much.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!